Hello im trying to switch 3 relay based on BH1750 light sensor input attached is an example of the flowchart , i managed to code the first part , but the problem I'm facing is i want to check the 3 relays status each 1 hour , and act based on their status the following hour , i can see in the serial monitor that the code is actually switching from 8-9 am to 9-10 am , but the case state doesnt change : this is if wifi is connected and time :
while (WiFi.status() == WL_CONNECTED) { // Check for WiFi connection
time(&now);
localtime_r(&now, &timeinfo);
printLocalTime();
Serial.println("sns hons");
float lux = lightMeter.readLightLevel();
lcd.setCursor(0,2);
lcd.print("Light: ");
lcd.print(lux, 0);
delay(500);
lightMeter.begin();
// Check the time and run the appropriate function
if (timeinfo.tm_hour >= 9 && timeinfo.tm_hour < 17 && timeinfo.tm_min >= 0 && timeinfo.tm_min <= 52) {
Serial.println("8-9");
loop_wifi_connected(); // loop_wifi_connected i have the code for first hour ,
else if (timeinfo.tm_hour >= 16 && timeinfo.tm_min >=52 && timeinfo.tm_hour < 24) {
Serial.println("13-14");
loop_wifi_connected1(); // in this i have code for seond hour
}
this is the first hour code works perfectly :
State state = STATE1;
void loop_wifi_connected() {
bool isFinished = false;
float lux = lightMeter.readLightLevel();
lightMeter.begin();
// Check the state of the state machine
switch (state) {
case STATE1:
time_t now;
struct tm timeinfo;
char buffer[32];
time(&now);
localtime_r(&now, &timeinfo);
if (digitalRead(relay1Pin) == LOW){
state = STATE0_1;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == LOW){
state = STATE2;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == LOW){
state = STATE3;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == HIGH){
state = STATE5SEC3;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE0_1:
delay (2000);
// If the lux value is less than the threshold, turn on the relay and move to the next state
if (lux < LUX_THRESHOLD ) {
digitalWrite(relay1Pin, HIGH); // turn on relay1
Serial.println("r1 on");
state = STATE1_1;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE1_1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
state = STATE2;
waitMillis = 0;
lastMillis = millis();}
// If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 0;
lastMillis = 0;
}
break;
case STATE2:
delay (2000);
// If the lux value is less than the threshold, turn on the relay and move to the next state
if (lux < LUX_THRESHOLD) {
digitalWrite(relay2Pin, HIGH); // turn on relay2
Serial.println("r2 on");
state = STATE5SEC1;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE5SEC1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
state = STATE3;
waitMillis = 0;
lastMillis = millis();
}
// If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay2Pin, LOW); // turn off relay2
state = STATE5SEC2;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC2:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD) {
state = STATE5SEC2_1;
waitMillis = 2000;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC2_1:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 5000;
lastMillis = 0;
isFinished = true;
}
break;
case STATE3:
delay (2000);
if (lux < LUX_THRESHOLD) {
digitalWrite(relay3Pin, HIGH); // turn on relay3
Serial.println("r3 on");
state = STATE5SEC3;
waitMillis = 5000;
lastMillis = millis();
}
case STATE5SEC3:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
Serial.println("wait light ");
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay3Pin, LOW); // turn off relay3
Serial.println("r3 off");
state = STATE5SEC3_1;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC3_1:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
Serial.println("wait light ");
waitMillis = 0;
lastMillis = millis();
}
else if (lux > LUX_THRESHOLD) {
Serial.println("r2 off ");
digitalWrite(relay2Pin, LOW);
state = STATE5SEC3_2;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC3_2:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
state = STATE5SEC2_1;
waitMillis = 5000;
lastMillis = 0;
}
}}
but then when the second hour starts and take off from wherever the state in hour 1 code was , and does not reset :
void loop_wifi_connected2() {
bool isFinished = false;
float lux = lightMeter.readLightLevel();
State state = STATE1;
lightMeter.begin();
// Check the state of the state machine
switch (state) {
case STATE1:
time_t now;
struct tm timeinfo;
char buffer[32];
time(&now);
localtime_r(&now, &timeinfo);
if (digitalRead(relay1Pin) == LOW){
state = STATE0_1;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == LOW){
state = STATE2;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == LOW){
state = STATE3;
waitMillis = 2000;
lastMillis = millis();
}
else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == HIGH){
state = STATE5SEC3;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE0_1:
delay (2000);
// If the lux value is less than the threshold, turn on the relay and move to the next state
if (lux < LUX_THRESHOLD ) {
digitalWrite(relay1Pin, HIGH); // turn on relay1
Serial.println("r1 on");
state = STATE1_1;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE1_1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
state = STATE2;
waitMillis = 0;
lastMillis = millis();}
// If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 0;
lastMillis = 0;
}
break;
case STATE2:
delay (2000);
// If the lux value is less than the threshold, turn on the relay and move to the next state
if (lux < LUX_THRESHOLD) {
digitalWrite(relay2Pin, HIGH); // turn on relay2
Serial.println("r2 on");
state = STATE5SEC1;
waitMillis = 2000;
lastMillis = millis();
}
break;
case STATE5SEC1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
state = STATE3;
waitMillis = 0;
lastMillis = millis();
}
// If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay2Pin, LOW); // turn off relay2
state = STATE5SEC2;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC2:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD) {
state = STATE5SEC2_1;
waitMillis = 2000;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC2_1:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay1Pin, LOW); // turn off relay1
waitMillis = 5000;
lastMillis = 0;
isFinished = true;
}
break;
case STATE3:
delay (2000);
if (lux < LUX_THRESHOLD) {
digitalWrite(relay3Pin, HIGH); // turn on relay3
Serial.println("r3 on");
state = STATE5SEC3;
waitMillis = 5000;
lastMillis = millis();
}
case STATE5SEC3:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
Serial.println("wait light ");
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
digitalWrite(relay3Pin, LOW); // turn off relay3
Serial.println("r3 off");
state = STATE5SEC3_1;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC3_1:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
Serial.println("wait light ");
waitMillis = 0;
lastMillis = millis();
}
else if (lux > LUX_THRESHOLD) {
Serial.println("r2 off ");
digitalWrite(relay2Pin, LOW);
state = STATE5SEC3_2;
waitMillis = 5000;
lastMillis = 0;
}
break;
case STATE5SEC3_2:
delay (2000);
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
waitMillis = 0;
lastMillis = millis();
}
else if (lux >= LUX_THRESHOLD) {
state = STATE5SEC2_1;
waitMillis = 5000;
lastMillis = 0;
}
}}