Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. int ledState = 0; // the current state of the output pin
  2. int buttonState = 0; // the current reading from the input pin
  3. int lastButtonState = 0; // the previous reading from the input pin
  4. int lastDebounceTime = 0; // the last time the output pin was toggled
  5. int debounceDelay = 50; // the debounce time; increase if the output flickers
  6.  
  7. void setup()
  8. {
  9. pinMode(8, INPUT);
  10. pinMode(2, OUTPUT);
  11. }
  12.  
  13. void loop()
  14. {
  15. int reading = digitalRead(8);
  16. if (reading != lastButtonState)
  17. lastDebounceTime = millis() % 100000000;
  18. if ((millis() - lastDebounceTime) > debounceDelay)
  19. {
  20. if (reading != buttonState)
  21. {
  22. buttonState = reading;
  23. if (buttonState == 1)
  24. ledState = !ledState;
  25. }
  26. }
  27. digitalWrite(2, ledState);
  28. lastButtonState = reading;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement