Skip to content

Instantly share code, notes, and snippets.

@brysonian
Created January 24, 2020 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brysonian/0c8a03443c7fb96190f384781030d519 to your computer and use it in GitHub Desktop.
Save brysonian/0c8a03443c7fb96190f384781030d519 to your computer and use it in GitHub Desktop.
const int BUTTON_PIN = 9;
const int LED_PIN = 13;
int state = 0;
int lastVal = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int val = digitalRead(BUTTON_PIN);
// transition if necessary based on input
if (val == LOW && lastVal == HIGH) {
if (state == 0) {
state = 1;
} else if (state == 1) {
state = 2;
} else {
state = 0;
}
delay(10);
}
lastVal = val;
// respond to the state
if (state == 1) {
digitalWrite(LED_PIN, HIGH);
} else if (state == 2) {
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
delay(50);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment