Help with "until" loop

int ATOsolenoid   = 3;
int FlushSolenoid = 4;
int lowEye        = 7;
int highEye       = 8;

enum { Idle, Flush };
int  state = Idle;

#if 0
const unsigned long MsecFlush  = 20 * 1000L;
const unsigned long MsecIdle   = (3600 * 1000L) - MsecFlush;
#else
const unsigned long MsecFlush  = 2 * 1000L;
const unsigned long MsecIdle   = (8 * 1000L) - MsecFlush;
#endif
unsigned long msec0;

// -----------------------------------------------------------------------------
void loop()
{
    unsigned long msec = millis ();

    // flush hourly
    switch (state) {
    case Idle:
        if (msec - msec0 >= MsecIdle)    {
            msec0 = msec;
            state = Flush;
            digitalWrite(FlushSolenoid, HIGH);  //Switch Solenoid ON
        }
        break;
    case Flush:
        if (msec - msec0 >= MsecFlush )  {
            msec0 = msec;
            state = Idle;
            digitalWrite(FlushSolenoid, LOW);   //Switch Solenoid OFF
        }
        break;
    }

    // -------------------------------------
    // maintain water level
    bool waterIsLow  = digitalRead(lowEye)  == LOW;
    bool waterIsHigh = digitalRead(highEye) == HIGH;

    if (waterIsLow)
        digitalWrite(ATOsolenoid, HIGH);    // open

    if (waterIsHigh)
        digitalWrite(ATOsolenoid, LOW);     // close
}

// -----------------------------------------------------------------------------
void setup() {
    // put your setup code here, to run once:
    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);
}
1 Like