Advertisement
2607

button4

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