0

I have two servos connected to an Arduino, They work fine if i write to them one by one so the wiring is all correct. I have seen a lot of examples here but none are facing the same issue. I have a simple for loop with Servo.write() on two servos (Facing each other) one going clockwise and the other going counterclockwise. I just want them to rotate at the same time and i cannot connect them to the same signal line since their movement is opposite. My Code Is:

 for(int pos = 0; pos < 90; pos += 2)  
 {      
     delay(200);        
     int pos7 = 90-pos;
     pos7 = pos7*(-1);           
     myservo6.write(pos);          
     myservo7.write(pos7);
 }

What Is Happening Both the servos go to the "same position" (if one is at 90 the other is at 0 etc.. ) Yet one goes there slow and the other goes super fast(not affected by delay). Even though my delay should affect both servos.

If i remove the delay they both go to the same position yet very fast. What i need is for them to move together slowly to the position.

  • Like most Arduino libraries, the documentation for Servo is horribly, inexcusably incomplete. You might try splitting your delay to put say 3 or 4 milliseconds of it in between the servo writes - if there is an undocumented interaction in how the pulses are actually generated, that should accommodate it. – Chris Stratton Jun 9 '17 at 15:11
  • 1
    Servo's limited documentation does however say that it accepts values in the range of 0-180; your attempt to write negative numbers to it seems improper, and may be causing an odd outcome, either horribly mistimed pulses, or perhaps no valid pulse until you reach 0 at the end and finally generate a position the "reversed" servo can begin moving to. – Chris Stratton Jun 9 '17 at 15:14
  • That was it ... i hadnt realized until u said "negative numbers" that i had it reversed ... instead of int pos7 = 90-pos; its int pos7 = pos-90; ... thank you @ChrisStratton – Daniel Minassian Jun 9 '17 at 18:59
1

The Arduino Servo class is intended to accept positive values only, in the range of 0-180.

In addition to reversing the values for one servo by subtracting from 90, your code is also negating the values by multiplying by negative one.

That results in an invalid argument to the Servo function. It's unclear exactly what happens, but likely possibilities include an invalid pulse, or no pulse at all.

At the end of your for loop, you subtract 90 from 90 producing zero, and invert it to still zero. Zero is a valid input to the Servo function, so a valid output is produced and the "reversed" servo finally has a position it can seek to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.