Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ledState = 0; // the current state of the output pin
- int buttonState = 0; // the current reading from the input pin
- int lastButtonState = 0; // the previous reading from the input pin
- int lastDebounceTime = 0; // the last time the output pin was toggled
- int debounceDelay = 50; // the debounce time; increase if the output flickers
- void setup()
- {
- pinMode(8, INPUT);
- pinMode(2, OUTPUT);
- }
- void loop()
- {
- int reading = digitalRead(8);
- if (reading != lastButtonState)
- lastDebounceTime = millis() % 100000000;
- if ((millis() - lastDebounceTime) > debounceDelay)
- {
- if (reading != buttonState)
- {
- buttonState = reading;
- if (buttonState == 1)
- ledState = !ledState;
- }
- }
- digitalWrite(2, ledState);
- lastButtonState = reading;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement