Hello, thanks in advance for any help you can provide.
I have 6 solenoid valves to control so I have set up a class to manage each valve, called “Valve”, with an object for each valve which designates the pins ( ie first.Valve(int open_pin, int close_pin)).
Then I have member functions to do stuff such as open the valve (ie first.open) or close the valve (first.close), etc.
I’m trying to get the valves to open sequentially for a specified time ie valve 1 for 1 min, valve 2 for 1 min, valve 3 for one min. I am familiar with the millis method for timing, but this turns into a lot of code if I have to set up an if statement for the timing of each of the 6 valves.
open valve 1
if time has elapsed
close valve 1
open valve 2
if time has elapsed
close valve 2
open valve 3
There are many different sequences that will be needed, so I’m looking for a better way.
I envisioned something like this:
Objects: Each represents a different physical valve
Then use a single “if millis” type statement and somehow use a variable to progress to the next valve (next member function) such as:
if (cycle_running = true)
if (run_once == false) // the first time cycle() runs, it will set the x variable and open the first valve
run_once = true; // Ensure first if statement isn’t run again
x.open(); //initially will open valve 1 (1.open, 2.open, etc)
if (x<= 6) // stop the cycle from running if all 6 valves have been run
if currentmillis >= timeperiod //check time
x.close(); // When time has elapsed, close the valve (1.close, 2.close, etc)
x++; // Advance x by one number
x.open(); // open the next valve
cycle_running = false; // Stops the loop function from calling cycle after the 6 valves have run
but it doesn’t seem that you can use a number for a member name (1.valve), or use a variable in a call to a function ie x.function.
I also thought about using a for loop with a variable for the specific member function.
for (x=1; x<=6; x++)
but again, this doesn’t work due to using a variable for a function call. And, the delay function halts the processor.
So, is there some trick to use a variable in a the call to a function, or another way to progress from one member function to another, or another established method for to achieve my goal?