i want to use servos without using delay, help me please

Hi everybody
i want to use my servo without using dealy function, this is my code i don't why it doesn't work

#include<Servo.h>

unsigned long ancienne=0;

Servo sens;
int pos=0;
unsigned long compteur;

void setup(){

Serial.begin(9600);
sens.attach(4);

}
void loop (){
unsigned long current = millis();
for(pos=0;pos<=180;pos++)
if(current-ancienne>10){
ancienne=millis();
sens.write(pos);
}
for(pos=180;pos>=0;pos--)
if(current-ancienne>10){
ancienne=millis();
sens.write(pos);
}
}

thank you so much :slight_smile:

if(current-ancienne>10){

Try increasing the 10, There may not be enough time for the servo to react properly.

New control signals are only sent to the servo every 20 ms or so, so 10 ms isn't enough time regardless of the hardware.

Arianelovesarduino:
i don't why it doesn't work

What does not work?

You MUST describe what you EXPECT to happen, and what ACTUALLY happened. Then others will know what error it is you are having. Your description does not even say if it is a compiler error you are getting, or if something is smoking.

void leg1() {
servo4.write(90);  
servo4.write(0);  // you write 90 to servo4 then right away write 0 with no time for the servo to react
delay(2000);
servo2.write(90);
servo2.write(80);  // same here for servo2
delay(1000);
servo4.write(90);
servo2.write(90);
 }

The delays seem to be in the wrong place.
Once you get the servos working the way you want with delays, study the "blink without delay" example in the IDE. Blink without delay works for all kinds of timing problems, not just LEDs.

groundfungus:

void leg1() {

servo4.write(90);  
servo4.write(0);  // you write 90 to servo4 then right away write 0 with no time for the servo to react
delay(2000);
servo2.write(90);
servo2.write(80);  // same here for servo2
delay(1000);
servo4.write(90);
servo2.write(90);
}




The delays seem to be in the wrong place.
Once you get the servos working the way you want with delays, study the "blink without delay" example in the IDE. Blink without delay works for all kinds of timing problems, not just LEDs.

i tried this code with 4 leg spider and was working fine...