Sweep.ino 988 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. Demo code for Project 22 - Servo(Sweep)
  3. */
  4. #include <Servo.h>
  5. Servo myservo; // create servo object to control a servo
  6. // a maximum of eight servo objects can be created
  7. int pos = 0; // variable to store the servo position
  8. void setup()
  9. {
  10. myservo.attach(9); // attaches the servo on pin 9 to the servo object
  11. }
  12. void loop()
  13. {
  14. for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
  15. { // in steps of 1 degree
  16. myservo.write(pos); // tell servo to go to position in variable 'pos'
  17. delay(15); // waits 15ms for the servo to reach the position
  18. }
  19. for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
  20. {
  21. myservo.write(pos); // tell servo to go to position in variable 'pos'
  22. delay(15); // waits 15ms for the servo to reach the position
  23. }
  24. }