delay()

Just got my advanceed arduino starter kit with the demilanove board.

I did a few test and it seems to be working ok until i started working with multiple parts (LEDs, servos).

It seems that the delay() does not work all the time. I have tried it with an LED and servo, two servos, and one servo.

here is the code for the two servos:

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

void setup() {
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(5); // attaches the servo on pin 9 to the servo object
}

void loop() {
delay(4000); // waits for the servo to get there

myservo1.write(1); // sets the servo position according to the scaled value
delay(4000); // waits for the servo to get there
myservo1.write(179); // sets the servo position according to the scaled value
delay(4000); // waits for the servo to get there

myservo2.write(179); // sets the servo position according to the scaled value
delay(4000); // waits for the servo to get there
myservo2.write(1); // sets the servo position according to the scaled value
delay(4000); // waits for the servo to get there
}

it goes through once and then the servos seem to ignore the delay.

I then tried with one servo (removing the code and connections to the other servo) and it does one or two loops correctly then it seems that it ignores the delay().

All those delays are 4 seconds long - is this a really slow servo?

no, it is not a slow servo, the 4 second was for testing. After the first loop, it seems to go to 0 seconds...

OK, strip the sketch down to bare essentials - just one servo

#include <Servo.h> 

Servo myservo1;  // create servo object to control a servo 

void setup() { 
 myservo1.attach(9);  
} 

void loop() { 
 myservo1.write(1);
 delay(4000);            
 myservo1.write(179);
 delay(4000);
}

What does this do?

did try that. It worked for a couple of loops, the it seemed to ignore the delay the then work then ignore delay etc...

The problem may be due to power.

Did you try Groove's sketch with only the one servo connected?

If you did and still had problems, did your other sketches (or the distributed example servo sketches) work with the same servo attached?

The problem seemed to have been a power problem.

I was pulling power from the board/usb cable. I ran a seperate power for the servos and it seems to be woring now...

I was pulling power from the board/usb cable.

Ahh, yes, that would probably be a problem. The Arduino can only directly power the smallest of servos, and even then you have to be careful. I wouldn't recommend trying that again, or you may risk frying your Arduino.. :wink:

Hi TchnclFl, There should be no risk to the arduino board, the power is drawn from the USB hub and these usually have some sort of over-current protection.

Hi TchnclFl, There should be no risk to the arduino board, the power is drawn from the USB hub and these usually have some sort of over-current protection.

Ah that's true. I guess I've just got "don't attach motors/servos directly to Arduinos" drilled into my skull a bit too deep :).

Attaching motors directly to Arduino pins is definitely not good, but absolutely no problem attaching lots of servos directly to Arduino pins as long as servo power comes from an adequate and correctly wired power supply.

Attaching motors directly to Arduino pins is definitely not good, but absolutely no problem attaching lots of servos directly to Arduino pins as long as servo power comes from an adequate and correctly wired power supply.

Yes, I agree. :slight_smile: