// https://wokwi.com/projects/410458155479810049
#define pin_LED 18
#define pin_BTN_red 25
#define pin_BTN_yellow 26
#define pin_BTN_green 27
long timer = 0;
const long period_sleep = 10000;
const long period_warmup = 4000;
const long period_measure = 7000;
String mode = "uninitialized";
String mode_new = "cyclic";
String state = "uninitialized";
int btn_red, btn_yellow, btn_green;
void setup() {
Serial.begin(115200);
// config GPIO21 as input pin and enable the internal pull-up resistor
pinMode(pin_BTN_green, INPUT_PULLUP);
pinMode(pin_BTN_yellow, INPUT_PULLUP);
pinMode(pin_BTN_red, INPUT_PULLUP);
pinMode(pin_LED, OUTPUT);
}
void loop() {
if (digitalRead(pin_BTN_green) == LOW) { mode_new = "on"; };
if (digitalRead(pin_BTN_yellow) == LOW) { mode_new = "cyclic"; };
if (digitalRead(pin_BTN_red) == LOW) { mode_new = "off"; };
if (mode_new != mode) {
mode = mode_new;
if ((mode == "on") or (mode == "cyclic")) {
if ((state != "measure") and (state != "go.warmup") and (state != "warmup")) {
state = "go.warmup";
}
}
if (mode == "off") {
if ((state != "sleep") and (state != "go.sleep")) { state = "go.sleep"; }
}
Serial.print("--> CHANGE MODE to mode="); Serial.print(mode); Serial.print(" | state="); Serial.println(state);
}
if (state == "go.warmup") {
state = "warmup";
timer = millis() + period_warmup;
digitalWrite(pin_LED, HIGH); // start device
}
if ((state == "warmup") and (millis() > timer)) {
state = "measure";
timer = millis() + period_measure;
}
if ((state == "measure") and (mode == "cyclic") and (millis() > timer)) {
state = "go.sleep";
}
if (state == "go.sleep") {
state = "sleep";
timer = millis() + period_sleep;
digitalWrite(pin_LED, LOW); // stop device
}
if ((state == "sleep") and (mode == "cyclic") and (millis() > timer)) {
state = "go.warmup";
}
if (state == "warmup") {
Serial.println("... emptying the stream");
//delay(1000);
}
if (state == "measure") {
Serial.println("... do measurement");
//delay(1000);
}
Serial.print("mode="); Serial.print(mode); Serial.print(" state="); Serial.print(state); Serial.print(" | ");
Serial.print(timer); Serial.print(" "); Serial.print(millis()); Serial.println("");
delay(1000);
}