// https://wokwi.com/projects/390745796014539777
// https://forum.arduino.cc/t/help-with-until-loop/1228025
const int ATOsolenoid = 3;
const int FlushSolenoid = 4;
const int lowEye = 7;
const int highEye = 8;
const int lowWater = A2;
const int highWater = A1;
#define EXE_INTERVAL_1 5000
#define EXE_INTERVAL_2 10000
void setup() {
Serial.begin(9600);
pinMode(ATOsolenoid, OUTPUT);
pinMode(FlushSolenoid, OUTPUT);
pinMode(lowEye, INPUT);
pinMode(highEye, INPUT);
pinMode(lowWater, OUTPUT);
pinMode(highWater, OUTPUT);}
unsigned long now;
enum notAnonymous {Idle, Flush, Waiting};
int state = Idle;
const unsigned int flushForTime = 20000; // flush on duration in milliseconds
const unsigned int pauseForTime = 120000; // wait between flushes
void loop() {
now = millis(); // read the time for use in this loop iteration
// bool waterIsLow = digitalRead(lowEye) == LOW;
// bool waterIsHigh = digitalRead(highEye) == HIGH;
// proxy water level and sensors instead
bool waterIsLow = analogRead(A0) < 128;
bool waterIsHigh = analogRead(A0) > 896;
digitalWrite(lowWater, waterIsLow ? HIGH : LOW);
digitalWrite(highWater, waterIsHigh ? HIGH : LOW);
// adjust valve or pump or whatever it is
if (waterIsLow) {
digitalWrite(ATOsolenoid, HIGH);
}
if (waterIsHigh) {
digitalWrite(ATOsolenoid, LOW);
}
// start or continue hourly 20 second flushing
static unsigned long flushTimer;
switch (state) {
case Idle : // start flushing
if (digitalRead(ATOsolenoid) == HIGH) {
state = Flush;
flushTimer = now;
digitalWrite(FlushSolenoid, HIGH); // Switch Solenoid ON
}
break;
case Flush : // flush for 20 seconds
if (now - flushTimer >= flushForTime) {
flushTimer = now;
state = Waiting;
digitalWrite(FlushSolenoid, LOW); // Switch Solenoid OFF
}
else if (digitalRead(ATOsolenoid) == LOW) {
state = Idle;
digitalWrite(FlushSolenoid, LOW); // Switch Solenoid OFF
}
break;
case Waiting : // wait an hour, or reset the flushing machine if ATOsolenoid says
if (now - flushTimer >= pauseForTime) {
flushTimer = now;
state = Flush;
digitalWrite(FlushSolenoid, HIGH); // Switch Solenoid ON (time to)
}
else if (digitalRead(ATOsolenoid) == LOW) {
state = Idle;
digitalWrite(FlushSolenoid, LOW); // Switch Solenoid OFF (ATOsolenoid dropped)
}
break;
}
}
/*
// never mind
static int pState = -1;
if (pState != state) {
Serial.print("state is "); Serial.println(state);
pState = state;
}
*/
ATO
Flush
LOW
HIGH