Opening up separate water valves at separate times

For a “smart home project”, I’m making an automatic watering system that will water different zones at different times. I’m opening up water solenoid valves for different zones and there will be a capacitive moisture sensor that is assigned to each solenoid valve. I am trying to program something that will read the moisture and open up separate solenoid valves at different times depending on if a zone needs more water.

I heard about “finite state machines” and I tried programming a class on how to do this. However, the solenoid valve is not closing and I’m not sure what I’m doing wrong. Any help would be nice and I would be very grateful.

const uint32_t WATERING_TIME = 2000;  // Water zone for 10 minutes

class TimedValve
{
public:
    TimedValve(uint8_t pin) : valvePin(pin), state(OFF) {}
    void turnOn() {
        if (state == ON) return;  // do nothing if already ON
        digitalWrite(valvePin, HIGH);
        state = ON;
        timeStarted = millis();
    }
    void update() {
        if (state == ON && millis() - timeStarted >= WATERING_TIME) {
            digitalWrite(valvePin, LOW);
            state = OFF;
        }
    }
private:
    uint8_t valvePin;
    enum { OFF, ON } state;
    uint32_t timeStarted;
};

const int solenoidPin1 = 1;
const int solenoidPin2 = 2;

const int moisturePin1 = A0;
const int moisturePin2 = A3;


void setup(){
    Serial.begin(9600);
    pinMode(solenoidPin1, OUTPUT);
    pinMode(solenoidPin2, OUTPUT);

}

void loop()
{
    static TimedValve valve1(solenoidPin1), valve2(solenoidPin2);
    // Turn the valves ON if needed.
    if (analogRead(moisturePin1) <= 450)
        valve1.turnOn();
    if (analogRead(moisturePin2) <= 450)
        valve2.turnOn();

    // Let them turn OFF if it's time.
    valve1.update();
    valve2.update();
}

D1 is the TX pin, suggest you use a different pin.

Try to move this in front of setup() TimedValve valve1(solenoidPin1), valve2(solenoidPin2); .

LarryD: D1 is the TX pin, suggest you use a different pin.

Try to move this in front of setup() TimedValve valve1(solenoidPin1), valve2(solenoidPin2); .

Well... that did it. Boy do I feel stupid. Thanks for the help. Now if you will excuse me, I'll be slamming my head on the wall.