Results 1 to 3 of 3

Thread: Servo Help

  1. #1
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Servo Help

    Hi all,

    I have just ordered and received on of these servos

    http://www.hobbyking.co.uk/hobbyking...c_0_075kg.html

    I have hooked it up to the board and have tried a couple of scripts. This one I saw on youtube and tied to tweak it to suit fuel gauge but it does not even move the servo. Here the script.

    Code:
    #include  //Different Servo Library which offers variable speed
    Servo fuelServo;
    
    
    int CodeIn;// used on all serial reads
    int KpinNo; 
    int Koutpin;
    String fuel;
    int fuel1;
    int val;
    
    
    
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    
    
    void setup() 
    { 
      pinMode(13, OUTPUT);
    fuelServo.attach(13); // attaches the servo on pin 9 to the servo object 
    Serial.begin(115200); 
    }
    
    
    void loop() {
     
      if (Serial.available()) {
        CodeIn = getChar();
        if (CodeIn == '=') {EQUALS();} // The first identifier is "="
        if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
        if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
        if (CodeIn == '/') {SLASH();}// The first identifier is "/" (Annunciators)
      }
    
    
    }
    
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    
    
    void EQUALS(){ // The first identifier was "="
     CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
        
             //etc etc etc
         }
    }
    
    
    void LESSTHAN(){ // The first identifier was "<"
    CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'X'://Found the second identifier Turn coordinator
           
        {
        fuel = "";
        fuel += getChar();
        fuel += getChar();
        fuel += getChar();
        int fuel1 = fuel.toInt(); // convert it to an integer (Thanks Phill)
           
      val = fuel1;            // reads the value of the data from FSX turn coordinator
      val = map(val, 0, 100, 0, 179); // scale it to use it with the servo ( between 0-256 for FSX turn coordinator,between 0 and 180 for servo) 
    
    
      Serial.println (val); //added for troubleshooting
       
        }
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
           
             //etc etc etc
         }
    }
    
    
    void QUESTION(){ // The first identifier was "?"
    CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
           
             //etc etc etc
         }
    }
    void SLASH(){ // The first identifier was "/" (Annunciator)
      //Do something
    }
    The second one is from Jim's demo codes. This one does get the servo to move but I have no idea how to set the parameters for the fuel gauge. I specifically am not sure what the +10 and *3 lines are for.



    Code:
    /*
        This code is in the public domain
        For use with "Link2fs_Multi"
        Jimspage.co.nz
        My thanks to the Guys that gave me snippets of code.
    
    
    */
    
    
    #include 
    Servo fuelServo;
    
    
    
    
    
    
    int CodeIn;// used on all serial reads
    int KpinNo;
    int Koutpin;
    String fuel;
    
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;
    
    
    void setup()
    {
      Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
    
    
      for (int KoutPin = 2; KoutPin < 70; KoutPin++)// Get all the pins ready for "Keys"
      {
        pinMode(KoutPin, INPUT);
        digitalWrite(KoutPin, HIGH);
      }
      Serial.begin(115200);
      pinMode(13, OUTPUT);// For the servo.
      fuelServo.attach(13);
    
    
    }
    
    
    void loop() {
      {
        KEYS(); //Check the "keys" section
      }
      if (Serial.available()) {
        CodeIn = getChar();
        if (CodeIn == '=') {
          EQUALS(); // The first identifier is "="
        }
        if (CodeIn == '<') {
          LESSTHAN(); // The first identifier is "<"
        }
        if (CodeIn == '?') {
          QUESTION(); // The first identifier is "?"
        }
        if (CodeIn == '/') {
          SLASH(); // The first identifier is "/" (Annunciators)
        }
      }
    
    
    }
    
    
    char getChar()// Get a character from the serial buffer
    {
      while (Serial.available() == 0); // wait for data
      return ((char)Serial.read()); // Thanks Doug
    }
    
    
    void EQUALS() {     // The first identifier was "="
      CodeIn = getChar(); // Get another character
      switch (CodeIn) { // Now lets find what to do with it
        case 'A'://Found the second identifier
          //Do something
          break;
    
    
        case 'B':
          //Do something
          break;
    
    
        case 'C':
          //Do something
          break;
      }
    }
    
    
    void LESSTHAN() {   // The first identifier was "<"
      CodeIn = getChar(); // Get another character
      switch (CodeIn) { // Now lets find what to do with it
        case 'A'://Found the second identifier
          //Do something
          break;
    
    
        case 'B':
          //Do something
          break;
    
    
        case 'X'://Found the second identifier ("X" Fuel position)
          fuel = "";
          fuel += getChar();
          fuel += getChar();
          fuel += getChar();
          int fueli = fuel.toInt(); // convert it to an integer (Thanks Phill)
          fueli = (fueli + 10);// Manual correction to suit servo zero.
          fuelServo.write(fueli * 2); // Manual fiddle to get full span of servo.
          // Note.. Using the "Map" method slows it down.
          //Serial.print(flapsi); // Used for testing
          break;
      }
    }
    
    
    void QUESTION() {   // The first identifier was "?"
      CodeIn = getChar(); // Get another character
      switch (CodeIn) { // Now lets find what to do with it
        case 'A'://Found the second identifier
          //Do something
          break;
    
    
        case 'B':
          //Do something
          break;
    
    
        case 'C':
          //Do something
          break;
      }
    }
    void SLASH() {   // The first identifier was "/" (Annunciator)
      //Do something
    }
    void KEYS()
    {
      Kstringnewstate = "";
      for (int KpinNo = 2; KpinNo < 70; KpinNo++) {
        KpinStateSTR = String(digitalRead(KpinNo));
        KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
        if (KpinStateSTR != KoldpinStateSTR)
        {
          if (KpinNo != 13) {
            Serial.print ("D");
            if (KpinNo < 10) Serial.print ("0");
            Serial.print (KpinNo);
            Serial.println (KpinStateSTR);
          }
        }
        Kstringnewstate += KpinStateSTR;
      }
      Kstringoldstate = Kstringnewstate;
    }
    Any help would be greatly appreciated.
    Many thanks
    Marc

  2. #2
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Servo Help

    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  3. #3
    75+ Posting Member
    Join Date
    Aug 2015
    Location
    Montreal
    Posts
    106
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Servo Help

    Did you hook up the servo correctly?

    You need power, ground, pwm. The power from the Arduino might not be suitable for the servo, you need another power source to prevent arduino browning out. Be sure to tie both grounds.

    If all of that fails, you'll have to supply more details to get help; how is the servo hooked to arduino? Is the INO properly sent to the board? Etc.