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.
}

