Guest User

Untitled

a guest
Sep 27th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin = 12;     // the number of the pushbutton pin
  2. const int ledPin =  13;      // the number of the LED pin
  3.  
  4. // Variables will change:
  5. boolean ledState = LOW;         // the current state of the output pin
  6. int buttonState;             // the current reading from the input pin
  7. int lastButtonState = LOW;   // the previous reading from the input pin
  8.  
  9. // the following variables are long's because the time, measured in miliseconds,
  10. // will quickly become a bigger number than can be stored in an int.
  11. long lastDebounceTime = 0;  // the last time the output pin was toggled
  12. long debounceDelay = 50;    // the debounce time; increase if the output flickers
  13.  
  14. void setup() {
  15.   pinMode(buttonPin, INPUT);
  16.   pinMode(ledPin, OUTPUT);
  17. }
  18.  
  19. void loop() {
  20.   // read the state of the switch into a local variable:
  21.   int reading = digitalRead(buttonPin);
  22.  
  23.   // check to see if you just pressed the button
  24.   // (i.e. the input went from LOW to HIGH),  and you've waited
  25.   // long enough since the last press to ignore any noise:  
  26.  
  27.   // If the switch changed, due to noise or pressing:
  28.   if (reading != lastButtonState) {
  29.     // reset the debouncing timer
  30.     lastDebounceTime = millis();
  31.   }
  32.  
  33.   if ((millis() - lastDebounceTime) > debounceDelay) {
  34.     // whatever the reading is at, it's been there for longer
  35.     // than the debounce delay, so take it as the actual current state:
  36.     buttonState = reading;
  37.   }
  38.  if (buttonState =! lastButtonState) {
  39.  ledState = !ledState;
  40.  }
  41.   // set the LED using the state of the button:
  42.   digitalWrite(ledPin, ledState);
  43.  
  44.   // save the reading.  Next time through the loop,
  45.   // it'll be the lastButtonState:
  46.   lastButtonState = reading;
  47. }
Add Comment
Please, Sign In to add comment