#define LED 3
#define BTN 2
#define BLINK_DLAY 1000
enum possibleStates {on, blink, off};
possibleStates state = on;
unsigned long lastPressed = 0;
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BTN), btnPressed, FALLING);
}
void loop() {
static unsigned long lastBlink = millis();
// put your main code here, to run repeatedly:
switch (state) {
case on:
digitalWrite(LED, HIGH);
break;
case blink:
if (lastBlink + BLINK_DLAY < millis()) {
digitalWrite(LED, !digitalRead(LED));
lastBlink = millis();
}
break;
case off:
digitalWrite(LED, LOW);
break;
}
}
void btnPressed() {
int now = millis();
if (now < lastPressed + 200) {return;}
state = (state + 1) % 3;
lastPressed = now;
}