// https://forum.arduino.cc/t/solved-blinking-indicators/1327297
// https://wokwi.com/projects/416000716783497217
const int indicator = 11;
const int myBuuton = 4;
//...
enum {PHASE0 = 0, PHASE1, PHASE2};
char *tags[] = {"phase zero", "phase one", "phase two", };
int thePhase = PHASE0;
bool butStateCur = true;
bool butStatePre;
bool ledOn;
unsigned long millisPre = 0;
unsigned long millisCur;
unsigned long phaseStart;
const int phaseDelay = 3000; // 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;
thePhase = PHASE1;
}
break;
case PHASE1 :
if (switchChange) {
ledOn = false;
thePhase = PHASE2;
phaseStart = millisCur;
}
break;
case PHASE2 :
if (millisCur - phaseStart > phaseDelay) {
thePhase = PHASE0;
}
break;
}
digitalWrite(indicator, ledOn ? HIGH : LOW); // C/C++ ternary operator
static int printedPhase = -1; // no it does not!
if (printedPhase != thePhase) {
Serial.print("entered ");
Serial.println(tags[thePhase]);
printedPhase = thePhase;
}
}