delayMicroseconds problem

Hey! I have been working on a sketch for controlling a microservo sg90 and an arduino uno r3. I have used the Servo.h library and all worked well. The problem came when i used delayMicroseconds().

The servo works using a PWM, with a 1ms-2ms more or less, high period and a 20ms total period.

The purpose of this sketch is to control a servo with a potenciometer. First, i read the value of the potenciometer, then i map that value to 700-2600 (that are my limits of high period). When the value is mapped, i set the pin of the servo in high the time that i got from the map and change to Low with a delaymicroseconds of the result of subtracting the map value to 20000. This sketch works, and the servo goes from 0º to 180º, but when i use a digital analyzer, i see that the total period is 3.82ms. But the High period is well and it works.

So, i don´t know where the problem is.

This is the code

int salida = 10; // pin for pwm
int pulso = 0;//pulse width
int pot = 0; // potenciometer value
void setup()
{
pinMode(salida, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
pot= analogRead(A0);  //Reading the potenciometer
pulso = map(pot,0,1023,700,2600); //mapping the value of the potenciometer
digitalWrite(salida, HIGH); // output pin to high
delayMicroseconds(pulso); // wait the time for high value
digitalWrite(salida, LOW); // output pin to low
int retpul = 20000;
retpul = retpul - pulso;
delayMicroseconds(retpul); // Pause to complete the 20ms total period.
}

How long does an analogRead take?

Check the upper limit for delayMicroseconds

How much does the timer 0 interrupt handler interfere?

dlloyd:
Check the upper limit for delayMicroseconds

It was that. If anyone has the same problem, the upper limit for delayMicroseconds is 16383 and that was my problem. I have solved it using two delayMicroseconds followed, one of 10000 and the other of 10000-pulso.
Thank you for the aid.

Common issue, most servos work fine for a longer total cycle, anything up to 50ms is almost never an issue, and i have tested an sg90 with 100ms cycle working just fine. The pulse length is what is important.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.