Last active
March 7, 2023 18:22
-
-
Save olivierspoor/b653fe5b207df7c7e859d9a1b377ee7b to your computer and use it in GitHub Desktop.
Arduino: Toggle a LED with a single button On/Off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int buttonPin = 2; | |
const int ledPin = 11; | |
int currState = HIGH; | |
int prevState = HIGH; | |
int lightState = LOW; | |
void setup() { | |
pinMode(buttonPin, INPUT_PULLUP); | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() { | |
currState = digitalRead(buttonPin); | |
if(currState != prevState) { | |
if(currState == LOW and lightState == LOW) { | |
lightState = HIGH; | |
digitalWrite(ledPin, lightState); | |
} | |
else if (currState == LOW and lightState == HIGH) { | |
lightState = LOW; | |
digitalWrite(ledPin, lightState); | |
} | |
} | |
prevState = currState; | |
delay(100); | |
} |
Author
olivierspoor
commented
Dec 13, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment