Advertisement
carcyn

Untitled

Jun 5th, 2020
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. // pushbutton pin
  2. const int buttonPin = 8;
  3. // servo pin
  4. const int servoPin = 3;
  5. Servo servo;
  6. //create a variable to store a counter and set it to 0
  7. int counter = 0;
  8. void setup()
  9. {
  10. servo.attach (servoPin);
  11.  
  12. // Set up the pushbutton pins to be an input:
  13. pinMode(buttonPin, INPUT);
  14. }
  15. void loop()
  16. {
  17. // local variable to hold the pushbutton states
  18. int buttonState;
  19. //read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
  20. buttonState = digitalRead(buttonPin);
  21. //if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
  22. if (buttonState == LOW) // light the LED
  23. {
  24. counter++;
  25. delay(150);
  26. }
  27. if(counter == 0)
  28. servo.write (20); // zero degrees
  29. else if(counter == 1)
  30. servo.write(90);
  31. else if(counter == 2)
  32. servo.write (150);
  33. else if(counter == 3)
  34. servo.write (180);
  35. //else reset the counter to 0 which resets thr servo to 0 degrees
  36. else
  37. counter = 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement