Skip to content

Instantly share code, notes, and snippets.

@brysonian
Created January 25, 2019 03:34
Show Gist options
  • Select an option

  • Save brysonian/0fb787b6ae40738c42ac2bb90591ba0e to your computer and use it in GitHub Desktop.

Select an option

Save brysonian/0fb787b6ae40738c42ac2bb90591ba0e to your computer and use it in GitHub Desktop.
const int BUTTON_PIN = 9;
const int LED_PIN = 13;
int state = 0;
int oldVal = 0;
int val = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
updateStateFromInput();
// --------
if (state == 1) {
digitalWrite(LED_PIN, HIGH);
} else if (state == 2) {
renderBlinkState();
} else {
digitalWrite(LED_PIN, LOW);
}
}
void updateStateFromInput() {
val = digitalRead(BUTTON_PIN);
if ((val == LOW) && (oldVal == HIGH)) {
state = state + 1;
if (state > 2) {
state = 0;
}
}
oldVal = val;
}
void renderBlinkState() {
for (int i = 0; i < 10; i++) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
state = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment