Is there a way to use serial input to change the arguments (might not be the right word) in a class?
I may be using the wrong terminology here, so please correct me if I’ve gotten anything wrong.
(As a side note, does anyone know of a of good tutorial that explains all of the terminology in a class?)
I have a program to control a series of 6 solenoid valves, opening and closing the valves at specified times. Since the basic code is the same for each valve, I have it set up as a class in a library. Each valve has its own instance with 5 variables for each instance: The valves number in the series, two pin numbers (the valves have a + open wire and a + close wire with a common ground), a variable for the time the valve should open, and a variable for the duration it should remain open.
In my main sketch I call (is that the right word?) 6 instances of the class (one for each valve) which sets the values of the valve number, open pin, close pin, open time, and open duration. The times are set as variables so I can quickly alter the code to change the times in hours, and it converts this to millis.
// Just here to show how the instance is set up
Valve 1(Valve number, Open pin, Close pin, Ground pin, Open time, Run time)
// Calling? the actual instances
Valve 1(1, 4, 5, Valve_1_openTime, valve_1_run_time)
Valve 2(2, 6, 7, valve_2_openTime, valve_2_run_time)
It all works great, but if I want to change the times, I have to alter the actual code and re-upload it. I’d like to be able to use the serial monitor to type in the times from my computer when the arduino first powers up, but it think the class is already initialized by the time the serial port is up so it’s too late to change the variables.
I found an old post about objects being destructed and re-constructed but that it shouldn’t actually be done in practice as it leads to memory leaks, and I couldn’t understand the scrap of code given as the final answer in the post. It’s an old post so I can’t just respond to it.
I have tried to play with something like a class.begin() command, thinking I could use this to initialize the class after the serial input was complete, but it seems like this can’t actually be used to change the arguments in an instance.
Since the times are different for each valve, I can’t just use an extern to send the variable to all of the instances.
The only workaround I can think of is to set the times as external variables in the main sketch and then use a series of if() statements within the class to assign the variable to the correct instance such as:
// In main sketch I could set open times
valve_1_openTime = x
valve_2_openTime = y
// Then in the class:
if (valve_number == 1)
{ open_time = valve_1_openTime; }
if (valve_number == 2)
{ open_time = valve_2_openTime;}
but this doesn’t seem very elegant. There must be a better way.
So now I’m stuck. Is there a way to change the arguments or to re-initialize the class? Or is there another method that is used to send such variables into a class that I should look into? Maybe another method of sending a variable to a class that could be unique to each instance of the class?
Usually I can find solutions by digging through the forums, but in this case, I’m probably not even searching for the right thing since I’m not sure what I’m looking for so if anyone could point me in the right direction it would be greatly appreciated.
Valve.cpp (8 KB)
Valve-class-serial2.ino (9.18 KB)
Valve.h (1.2 KB)