// https://forum.arduino.cc/t/solved-blinking-indicators/1327297
// https://wokwi.com/projects/415999787798480897
const int indicator = 11;
const int myBuuton = 4;
//...
enum {PHASE0, PHASE1, PHASE2};
int thePhase = PHASE0;
bool butStateCur = true;
bool butStatePre;
bool ledOn;
unsigned long millisPre = 0;
unsigned long millisCur;
unsigned long phaseStart;
const int phaseDelay = 2000; // delay for phase to timeout
void setup() {
Serial.begin(115200);
Serial.println("\nJello Whirled!\n");
pinMode(indicator, OUTPUT);
pinMode(myBuuton, INPUT_PULLUP);
butStatePre = digitalRead(myBuuton) == LOW;
}
void loop() {
millisCur = millis();
//... let us do our own switch handling
bool switchChange = false; // until proven otherwise
butStateCur = digitalRead(myBuuton) == LOW;
if (butStatePre != butStateCur) {
butStatePre = butStateCur;
switchChange = true;
delay(20); // debounce
}
//...
switch (thePhase) {
case PHASE0 :
ledOn = (millis() / 500) % 2;
if (switchChange) {
ledOn = true;
Serial.println("Phase = 1");
thePhase = PHASE1;
}
break;
case PHASE1 :
if (switchChange) {
ledOn = false;
Serial.println("Phase = 2");
thePhase = PHASE2;
phaseStart = millisCur;
}
break;
case PHASE2 :
if (millisCur - phaseStart > phaseDelay) {
thePhase = PHASE0;
Serial.println("Timeout, Phase = 0");
}
break;
}
digitalWrite(indicator, ledOn ? HIGH : LOW); // C/C++ ternary operator
}