Hello. I am new to Arduino programming and I am having troubles with a project I am doing. The project is a pH controller for hydroponics, and the idea that I have is when the difference between the pH that I want and the pH that I have is above a certain number a pump activates (with a relay) for a certain period if time but I can't use a delay. The part of the code is the following:
I am not quite sure if it is good to go, because I don't really understand if Millis() starts to measure from the point I call it or since the beginning, or if I can have different Millis(). Thanks in advance for your help.
The value of millis() increments from the second the program beings to run and is whatever value it is at the time the program looks for its value thru the call.
Looking at your code I don't really understand how you are going to differentiate CurrentTime to millis()
hopefully someone else can teach us both about that.
I found the first part of the loop on the internet (how to read signals from the pH sensor). And with the state-machine, I also knew I could use it but I don't really know how to specify the values (for example, the states). Thank you very much for your help.
The variable state Pump only needs to hold a very small value (basically 0 (off) or 1 (on).) This could be done with a a single bit but a byte is handy.
The other values -- those that are used in millis() timing need to be 32-bits wide because the values returned by millis() are 32-bit unsigned values.
In this case, the pump can turn on and at some point before 5 seconds dpH goes out of range, the pump does not turn off.
It is important that time checks are made in millis code.
To that effect, a pH test might set variables to trigger a function outside of the test code itself.
Time conditions are only one kind of event. Pin states and values in variables are other trigger events for conditional tasks in main loop code. Engineers have been doing it since the 70's that I know of.
if ( pumpTimer == 0 ) { // to pH test only when the pump is not running
// you may or may not want that. You must decide, take your time.
....
if (dpH <= 3 && dpH > 2)
{
pumpTimer = 5000; // an int, does double-duty
digitalWrite(rele, 1);
timerStart = millis(); // causes the one pump function to run in turn
}
else if (......
void runPumpOnTimer() // this function Must run every time that loop() runs.
{
if ( pumpTimer > 0 ) // you turn it on by telling it how long to run
{
if ( millis() - timerStart >= pumpTimer ) {
digitalWrite(rele, 0);
pumpTimer = 0; // this makes the pump run a 1-shot event
}
}
}