Skip to content

Instantly share code, notes, and snippets.

@olivierspoor
Last active March 7, 2023 18:22
Show Gist options
  • Save olivierspoor/b653fe5b207df7c7e859d9a1b377ee7b to your computer and use it in GitHub Desktop.
Save olivierspoor/b653fe5b207df7c7e859d9a1b377ee7b to your computer and use it in GitHub Desktop.
Arduino: Toggle a LED with a single button On/Off
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);
}
@olivierspoor
Copy link
Author

olivierspoor commented Dec 13, 2021

Drukknop_InputPullup_LED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment