Here is the final code being used:
int ATOsolenoid = 3; //This is the output pin on the Arduino we are using
int FlushSolenoid = 4; //This is the output pin on the Arduino we are using
int lowEye = 7;
int highEye = 8;
void setup() {
Serial.begin(9600);
pinMode(ATOsolenoid, OUTPUT); //Sets the pin as an output
pinMode(FlushSolenoid, OUTPUT); //Sets the pin as an output
pinMode(lowEye, INPUT);
pinMode(highEye, INPUT);
}
unsigned long now;
enum notAnonymous { Idle,
Flush,
Waiting };
int state = Idle;
const unsigned int flushForTime = 20000; // flush for 20 seconds
const unsigned int pauseForTime = 3600000; // wait 1 hr 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;
// adjust water level with ATOsolenoid
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, or reset the flushing machine
if (now - flushTimer >= flushForTime) {
flushTimer = now;
state = Waiting;
digitalWrite(FlushSolenoid, LOW); //Switch Solenoid OFF
} else if (digitalRead(ATOsolenoid) == LOW) {
digitalWrite(FlushSolenoid, LOW); //Switch Solenoid OFF
state = Idle;
}
break;
case Waiting: // wait an hour, or reset the flushing machine
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;
}
}