// 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;
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led1:A
led1:C
sw1:1
sw1:2
sw1:3