Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Arduino Toggle Button Code */
- bool ledState = LOW; // store state of LED.
- int currentButtonState = LOW; // track when button has changed states
- int previousButtonState = LOW;
- void setup() {
- pinMode(13, OUTPUT); // built-in LED
- pinMode(12, INPUT); // button
- digitalWrite(12, HIGH); // turn on internal pull-up
- }
- void loop() {
- digitalWrite(13, ledState); // keep LED on pin 13 in sync
- currentButtonState = digitalRead(12); // get current state of button
- if ((previousButtonState==HIGH) && (currentButtonState == LOW)) { // has the button changed?
- delay(100); // let the button de-bounce
- ledState = !ledState; // changes value of ledState from HIGH to LOW or LOW to HIGH.
- }
- if ((previousButtonState==LOW) && (currentButtonState == HIGH))
- delay(100); // de-bounce again
- previousButtonState = currentButtonState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement