Advertisement
Guest User

Button with Motor

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. const int motor = 9;
  2. const int button = 10;
  3. int ReadValue;
  4. int count = 0;
  5. boolean currentButton;
  6. boolean lastButton;
  7. boolean flagOn;
  8.  
  9. void setup()
  10. {
  11. pinMode (motor, OUTPUT);
  12. Serial.begin (9600);
  13. }
  14. boolean debounce (boolean last)
  15. {
  16. boolean current = digitalRead (button);
  17. if (last != current)
  18. {
  19. delay (5);
  20. current = digitalRead (button);
  21. }
  22. return current;
  23. }
  24.  
  25. void loop()
  26. {
  27. currentButton = debounce (lastButton);
  28. if (lastButton == LOW && currentButton == HIGH)
  29. {
  30. count ++;
  31. Serial.println (count);
  32. }
  33. if (count == 0)
  34. {
  35. analogWrite (motor, 0);
  36. delay (50);
  37. }
  38. if (count == 1)
  39. {
  40. analogWrite (motor, 100);
  41. delay (50);
  42. }
  43. if (count == 2)
  44. {
  45. analogWrite (motor, 150);
  46. delay (50);
  47. }
  48. if (count == 3)
  49. {
  50. analogWrite (motor, 200);
  51. delay (50);
  52. }
  53. if (count == 4)
  54. {
  55. analogWrite (motor, 250);
  56. delay (50);
  57. }
  58.  
  59. if (count == 5)
  60. {
  61. count =0;
  62. }
  63. lastButton = currentButton;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement