Whether I like it or not, Arduino is here to stay.
The servo library is very handy, but the example that sweeps a servo is not that great when it comes to ease of change. The code below has a few useful #defines that can be used to change the sweep behaviour.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#define DELAY 15 //milliseconds between position updates #define MAX 130 //maximum angle #define MIN 50 //minimum angle #define STEPSIZE 1 //degrees between steps #define SERVOPIN 5 //which pin is the servo connected to #define DELAY 5 //How much time should we wait between position changes #include Servo myservo; // create servo object to control a servo int pos = MIN; // variable to store the servo position void setup() { myservo.attach(SERVOPIN); // attaches the servo on pin 9 to the servo object } void loop() { for(pos = MIN; pos < MAX; pos += STEPSIZE) // 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(DELAY); // waits 15ms for the servo to reach the position } for(pos = MAX; pos>=MIN; pos-=STEPSIZE) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(DELAY); // waits 15ms for the servo to reach the position } } |