Loop doesn't execute the same times every second

Hi all,

First of all, sorry for my english.

I’ve been working with Arduino for a year more or less. Now I’ve started with servos because I’m making a new proyect. My problem is my servo goes crazy, even with the most simple example.

My program is very simple, it moves the servo to the right and when it reaches 180 degrees, it turns to the left, with 10 degrees every step every second. I put the code at the bottom.

Well, when the program run for a while, the servo stops and it restart the running with a jump or maybe it stops forever and I have to reboot the Arduino. Because of these, I’ve put a counter that counts how many times the loop was called. My results, when the servo stopped, the loop’s times reduce (120000/second → OK, 20000/second → bad). I’ve tried with Servo library but I get the same results.

Thank you in advance. Any answer will be wellcome.

int angle;
int widthPulse;
int maxPulse;
int minPulse;
int rTime;
int increment;
unsigned long lastPulse;
unsigned long lastChange;
unsigned long nTimes;

int angle2widthPulse(int angle){
return map(angle, 0, 180, 700, 2200);
}

void setup(){
Serial.begin(9600);
nTimes = 0;
increment = 10;
angle = 91;
widthPulse = angle2widthPulse(angle);
maxPulse = 2200;
minPulse = 700;
rTime = 18;
pinMode(2,OUTPUT);
lastPulse = millis();
lastChange = millis();
}

void loop(){
nTimes++;
if(millis() - lastPulse >= rTime){
digitalWrite(2, HIGH);
delayMicroseconds(widthPulse);
digitalWrite(2, LOW);
lastPulse = millis();
}
if(millis() - lastChange >= 1000){
angle = angle + increment;
if(angle > 180){
increment = -10;
angle = 170;
}else if(angle < 0){
increment = 10;
angle = 10;
}
widthPulse = angle2widthPulse(angle);
lastChange = millis();
Serial.println(nTimes/1000, DEC);
nTimes = 0;
}
}

Depending on the value of widthPulse the processing time of the loop will vary. Hence the number of passes of the loop per second will vary. This is to be expected.

How did you use the servo library? Would you show us your sample code?

Udo

Code seems OK, these questions came into my mind:

If you change the baudrate of the Serial port to 115200 does the same problems happen? Thinking the Serial.print may affect timing somehow

If you make the increments smaller 1 and -1 ? how does that affect the application? except for the smaller steps.

How much power does the servo draws? type?

Hi all,

Thank you for your replies.

I’ve tried this program with the PWM output:

int pin;
unsigned long nTimes;

void setup(){
nTimes = 0;
pin = 9;
pinMode(pin,OUTPUT);
}

void loop(){
if(nTimes == 0){
analogWrite(pin, 121);
nTimes++;
}
}

Note that the program is very easy. The behavior is:
The servo goes to the right position at the beginning. After 30 seconds, more or less, the servo goes to 0º and then goes to hundred or more degrees. And finally, it recover the right position.

Well, the new discovery is when the servo misses the position, the “L” led turn on and when the servo gets back to the right position, the “L” led turn off.

What is the meanning of the “L” led in this case?

Servos dont work that way, read about the servo lib. Then if the L led blink it means that your arduino is being reset due to voltage sag caused by the servo motor turning and drawing a lot of current

Can we assume you’re using PWM and not PPM to drive an R/C servo?
Who told you that was a good idea?