10º, 90º, 170º
This program moves the servo to one side, to the middle, and to the other side, waiting for one second at each position. servoPulse is the function which generates the pulses needed by the servo to move. Notice the required delay(20).... this means that in the rest of the program, we cant use a long delay as we would normally or else the servo would loose its position. Instead we will use a loop that calls the servoPulse function 50 times to create a delay of 1 second. (50 loops x 20ms = 1000ms = 1 second) Its not perfect but it works!
/* 10º, 90º, 170º
Moves servo to 3 different angles
with a pause of 1 second each.
*/
int servoPin = 2; // servo connected to digital pin 9
int myAngle; // angle of the servo roughly 0-180
int pulseWidth; // servoPulse function variable
void setup()
{
pinMode(servoPin, OUTPUT); // sets pin 2 as output
}
void servoPulse(int servoPin, int myAngle)
{
pulseWidth = (myAngle * 10) + 600; // determines delay
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // micro pause
digitalWrite(servoPin, LOW); // set servo low
delay(20); // refresh cycle
}
void loop()
{
myAngle = 10; // starts at 10º
for(int i=0; i<50; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
myAngle = 90; // moves to 90º
for(int i=0; i<50; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
myAngle = 170; // then moves to 170º
for(int i=0; i<50; i++) // loops 50 times
{
servoPulse(servoPin, myAngle);
}
}
Comments (0)
You don't have permission to comment on this page.