I am using and ESP32 and this is my timer cycle loop code. i tried to comment in the variables how it works the best i can. this code is part of a much much larger program. will someone please check my logic. i commented the order and logic of things. im trying to track a bug i cant identify. does anyone see anything that might not be doing what i think.
I have been working at this code for 2 days now and im stumped. please have a look thankyou.
if (_EEPROM.timerMode == 3) {
if (millis() - startupdelayMillis >= timerMode3StartupDelay) {
if (_EEPROM.enabled) {
Serial.println("tmode=3");
if (secondstime - _EEPROM.timerMode3LastCycletime > _EEPROM.frequency && timerMode3State == 0) {
if (fullFloatSensorStatus) { //if is FULL continue
flowMilliLitres = 0; //reset MilliLitres
totalMilliLitres = 0; //reset totalMilliLitres
handleTimers(1, 1, 0, 1); //turn on relay 3 "dosePump on"
timerStates[2] = 1; //timer 3 state 1
_EEPROM.timerRuntimes[2]++; // run counter
timerMode3State = 1; // go to next state
_EEPROM.timerMode3LastCycletime = secondstime; // timerMode3LastCycletime rtc.epoch Seconds
_writeEEPROM(); //set timerMode3LastCycletime new value to eeprom memory
}
}
if (timerMode3State == 1) {
if (millis() - previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / _EEPROM.calibrationFactor;
previousMillis = millis();
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
}
if (totalMilliLitres >= _EEPROM.timerMode3MaxML && doseUsed) {
handleTimers(1, 1, 1, 0); //turn off dose pump turn on mixing pump
timerStates[2] = 0; //timer 3 state 0 for off
timerStates[3] = 1; //timer 4 state 1 "mixing pump is on"
_EEPROM.timerRuntimes[3]++;
doseUsed = false; // new dose not used yet
Mix = true; // enable mix state true
prevMixmillis = millis(); // set mix prevmillis
}
if (Mix && millis() - prevMixmillis >= 20000) { //wait for mix to be done enable feed pump
readyToFeed = true; //enable feed pump power by setting readytofeed true
prevMixmillis = millis();
}
//if tank not empty and has dosed enough ml and is ready to feed, then power on feed pump
if (!emptyFloatSensorStatus && totalMilliLitres >= _EEPROM.timerMode3MaxML && readyToFeed) {
totalMilliLitres = 0; // reset to 0 for next dose cycle
doseUsed = true; // prev dose has been used so doseUsed true
readyToFeed = false; // not ready to feed again yet
handleTimers(1, 0, 1, 1);//turn off mixing pump and turn on feed pump
timerMode3State = 2; // next cycle state enable feed pump power off countdown
timerStates[1] = 1; // feed pump power state is true
timerStates[3] = 0; //mixing pump state if off
_EEPROM.timerRuntimes[1]++; // feed pump counter
}
}
if (timerMode3State == 2) {
if (emptyFloatSensorStatus) { // wait until tank empty
handleTimers(1, 1, 1, 1); // turn off all relays/pumps
timerStates[1] = 0; // feed pump state is off
_EEPROM.timerMode3LastCycletime = secondstime;// update eeprom variable for beginning of new cycle
timerMode3State = 0; // set timerMode3State to 0 to restart at beginning.
}
}
}
}
}
i update secondstime with eopch seconds from rtc time chip.
in timerMode3State 0 after timeout has expired turn on dose pump if tank is full. then timer3ModeState 1 will wait until flow meter counts set milliliters. after mL is dosed then we start mixing pump for 20 seconds, then we start feed pump and repeat.
I understand there is some guess work for the reader without the complete code. "assuming" the timer/time variables that you cant see are correct, how about the flow of the state change. does it do what i described or is something out of place?
Recently during testing, sometimes the mixpump don't run. so im thinking maybe one of the states is out of order but it looks right to me. maybe someone else will notice a problem i have not recognized yet.
When i powercycled the mcu it worked fine. but it seemed like after the 72000 second"_EEPROM.frequency" duration expired the mix pump didn't run before starting the feed pump.