0
votes

I saw many many videos on pwn and servo motor coding but can't understand how exactly the servo motor code works in arduino. my main confusion is what is the exact purpose of delay in the below code. when we say servo.write(180); doesn't it mean go to 180 degrees? what is the use of the delay here? some milliseconds cause the servo to work properly or jitter or vibrate not move or incorrect rotations etc.

include

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(10); // waits 15ms for the servo to reach the position
  }
1

1 Answers

1
votes

The code attempts to get the servo to move relatively slowly from 0 to 180 degrees. 10 ms times 181 steps equals 1.81 seconds.

Most if not all servos are significantly faster than that if you just move straight from 0 to 180 in one go.

One problem with this code is that you don't know what the servo's initial position is. For instance, if the initial position is 180, it will quickly jump to 0 and then slowly move back to 180.

Also note that most servos are controlled with a signal every 20 ms or so, so changing the signal more often than that could lead to problems (like jitter/vibrations/noise/...).