Advertisement
coltonspastebin

Spinning fan with servo motor

Jan 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const int buttonpin = 12;
  2. const int motorpin = 9;
  3. boolean lastbutton = false;
  4. boolean currentbutton = false;
  5. boolean motorOn = false;
  6.  
  7.  
  8.  
  9. void setup()
  10. {
  11. pinMode (lastbutton, INPUT);
  12. pinMode (motorpin, OUTPUT);
  13. Serial.begin (9600);
  14. }
  15. boolean debounce (boolean lastbutton)
  16. {
  17. boolean currentbutton = digitalRead (buttonpin);
  18. if (lastbutton != currentbutton)//if lastbutton is different than currentbutton then delay for 5 milliseconds and read the currentbutton
  19. {
  20. delay (5);
  21. currentbutton = digitalRead (buttonpin);
  22. }
  23. return currentbutton;
  24. }
  25.  
  26. void loop()
  27. {
  28. currentbutton = debounce (lastbutton);
  29. if (lastbutton == LOW && currentbutton == HIGH)
  30. {
  31. motorOn =! motorOn;
  32. }
  33. digitalWrite (motorpin, motorOn);
  34. lastbutton = currentbutton;
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement