I have this code that I'm using to control a Servo and I'd like to modify it so that it gradually decreases the degrees of sweep by a set amount (say 1 degree per pass). Right now it sweeps back and forth from ZERO to 180. I can manually change that by changing the numbers but I'm guessing there's an easier way to do this. Any ideas?
I'd like the sweep range range to decrease (i.e. say zero to 180, 1 to 179, 2 to 178, etc then eventually turning around and increasing to full width passes again). Thanks in advance for ideas!
Code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}