Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonPin = 2; // the pin that the pushbutton is attached to
- const int ledPin = 13; // the pin that the LED is attached to
- // Variables will change:
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- int ledState = false;
- void setup() {
- // initialize the button pin as a input:
- pinMode(buttonPin, INPUT_PULLUP);
- // initialize the LED as an output:
- pinMode(ledPin, OUTPUT);
- }
- void loop() {
- // read the pushbutton input pin:
- buttonState = digitalRead(buttonPin);
- // compare the buttonState to its previous state
- if (buttonState != lastButtonState) {
- // change detected, if button is down (inverted since pullup),
- // invert led state
- if (buttonState == LOW)
- ledState = !ledState;
- digitalWrite(ledPin, ledState);
- // Delay a little bit to avoid bouncing
- delay(50);
- }
- // save the current state as the last state,
- //for next time through the loop
- lastButtonState = buttonState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement