Advertisement
rosea4

Code for debounce button

Mar 17th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. const int LED=9 ;
  2. const int BUTTON=2 ;
  3. boolean lastButton = LOW;
  4. boolean currentButton = LOW;
  5. boolean ledOn = false;
  6. void setup ()
  7. {
  8. pinMode (LED, OUTPUT) ;
  9. pinMode (BUTTON, INPUT) ;
  10. }
  11. boolean debounce (boolean last)
  12. {
  13. boolean current = digitalRead (BUTTON) ;
  14. if (last != current)
  15. {
  16. delay(5) ;
  17. current = digitalRead (BUTTON) ;
  18. }
  19. return current ;
  20. }
  21. void loop( )
  22. {
  23. currentButton = debounce(lastButton) ;
  24. if (lastButton == LOW && currentButton == HIGH)
  25. {
  26. ledOn = !ledOn ;
  27. }
  28. lastButton = currentButton;
  29. digitalWrite (LED, ledOn) ;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement