Arduino Problem(Servo library)

Discussion in 'UDOO NEO' started by Yiwen, Aug 3, 2016.

  1. Yiwen

    Yiwen New Member

    Joined:
    Aug 3, 2016
    Messages:
    4
    Likes Received:
    0
    The following code is correct in original Arduino.I can control my motor in this way.
    But it cannot work in UDOO arduino.
    #include <Servo.h>
    #include <servo_mqx.h>
    Servo myservo;

    void setup() {
    Serial.beigin(115200);
    myservo.attach(9);
    }

    void loop() {
    myservo.writeMicroseconds(1700);
    }

    -----------------------
    Then I just modified the loop() as:
    void loop() {
    myservo.writeMicroseconds(1700);
    Serial.println(myservo.attach(9));
    }
    -----------------------
    And the motor works...

    It makes me very confused.Is there anyone can help me to figure out why this illogical way can solve my problem???
    Thank you very much!
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,262
    Likes Received:
    564
    On what Udoobuntu version are you working?
    If have no Neo close by at the moment but why include servo_mqx.h ?

    Did you try the original example that uses myservo.write instead of myservo.writeMicroseconds?
    It could be just a bug in the servo library, it is modified for the Neo.

    Code:
    /* Sweep
    by BARRAGAN <http://barraganstudio.com>
    This example code is in the public domain.
    
    modified 8 Nov 2013
    by Scott Fitzgerald
    http://www.arduino.cc/en/Tutorial/Sweep
    */
    
    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    // twelve servo objects can be created on most boards
    
    int pos = 0;    // variable to store the servo position
    
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }
    
    void loop() {
      for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
      for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }
     
    Andrea Rovai likes this.
  3. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,262
    Likes Received:
    564
    Did you solve your issue?
    Adding a serial.println() will slow down your sketch if you don't read the serial device from A9 so your workaround is not really functional unfortunately.
     
    Andrea Rovai likes this.
  4. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,262
    Likes Received:
    564
    Found the problem and fried a servo in the meantime :)

    There is an error in the servo library in /usr/share/arduino/hardware/UDOO/solox/libraries/Servo/Servo.h
    The following declarations are wrong:
    Code:
    #define MIN_PULSE_WIDTH       544ul     // the shortest pulse sent to a servo
    #define MAX_PULSE_WIDTH      2400ul     // the longest pulse sent to a servo
    #define DEFAULT_PULSE_WIDTH  1500ul     // default pulse width when servo is attached
    
    Should be:
    Code:
    #define MIN_PULSE_WIDTH       544    // the shortest pulse sent to a servo
    #define MAX_PULSE_WIDTH      2400    // the longest pulse sent to a servo
    #define DEFAULT_PULSE_WIDTH  1500    // default pulse width when servo is attached 
    In a Linux terminal do:
    sudo leafpad
    open the servo.h file and edit the lines
    save.

    If you recompile it works. At least when i connect a led to the pin I lights up and it lights better if the value is higher.
    Also the Sweep example works now. It would hang my Neo.
    What I also learned is that I should use myservo.attach(9, 1000, 2000); as 2000 is the highest it should get.
    I don't know what signal my servo got first with the Sweep example but it did one movement, got really hot and died. :-(
    It was a cheap one so don't worry.

    @Andrea Rovai Could you arrange that this is fixed in the arduino-board manager repository?
     
    JackSilb likes this.
  5. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Done. In the repository it's fixed.
     
    waltervl likes this.
  6. JackSilb

    JackSilb Active Member

    Joined:
    Nov 3, 2015
    Messages:
    100
    Likes Received:
    32
    Hi Andrea,

    When you say "Done and in the repository it's fixed."
    Do we get it with sudo apt-get update and apt-get upgrade?

    If not, how do we get it?

    Thanks,
    -Jack
     
  7. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    No, you cannot get it those ways. We are making the package and we'll update the Downloads section.
     

Share This Page