so im using a float sensor to detect when the water level in a tank is full "FloatSwitch LOW" or empty "FloatSwitch HIGH". if the floatswitch state dont change within the 40 minutes i set as the delay then it will change the state of another variable and sound an alarm. the problem is i keep getting floatswitch high alarm as if "FloatSwitchMillis = millis();" is being reset every time the state of the switch changes. can someone look at this piece of code and tell me if you see anything wrong based on how im trying to use it?
if ((digitalRead(FloatSwitch) == HIGH) && TimersEnabled) { // waterlevel low
if (millis() - FloatSwitchMillis >= timerParams.FloatSwitchHIGHduration) { // 40 minute delay
if (AlarmEnabled) {
timerParams.alarmhighcounter++;
FloatSwitchAlarmStatus = true;
AlarmBuzzer = true;
FloatSwitchMillis = millis();
}
}
}
if ((digitalRead(FloatSwitch) == LOW) && TimersEnabled) { //water level high
if (millis() - FloatSwitchLowMillis >= timerParams.FloatSwitchLOWduration) {
if (AlarmEnabled) {
timerParams.alarmlowcounter++;
FloatSwitchAlarmStatus = true;
AlarmBuzzer = true;
FloatSwitchLowMillis = millis();
}
}
}
FloatSwitchState = digitalRead(FloatSwitch);
if (FloatSwitchState != lastFloatSwitchState) {
if (FloatSwitchState) {
timerParams.FloatSwitchCounter++;
FloatSwitchStateCounter++;
FloatSwitchLowMillis = millis(); //to reset float switch comp millis every time water level
FloatSwitchMillis = millis(); //to reset float switch comp millis every time water level changes
FloatSwitchResetMillis = millis();
//Serial.println("switch high counter");
//Serial.println(FloatSwitchStateCounter);
} else {
timerParams.FloatSwitchCounter++;
FloatSwitchLowMillis = millis(); //to reset float switch every time water level changes
FloatSwitchMillis = millis(); //to reset float switch every time water level changes
}
}
lastFloatSwitchState = FloatSwitchState;