 # Help with Millis() in control

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:

``````  dpH = phValue - pHQ;
if (dPH > 0) {
if (timer == false){
currentTime = millis();
timer = true;
}
pump();
}
}

void pump()
{
if (dpH <= 3 && dpH > 2) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 5000) {
digitalWrite(rele, 0);
timer = false;
}
}
else if (dpH <= 2 && dpH > 1) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 3000) {
digitalWrite(rele, 0);
timer = false;
}
}
else if (dpH <= 1 && dpH > 0) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 2000) {
digitalWrite(rele, 0);
timer = false;
}
}
else {
digitalWrite(rele, 0);
timer = false;
}
}
``````

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.

Code fragment looks ok, but better to post the whole thing.

The currentTime thing is ok because it's only set when not already timing.

Could you consider controlling the pump with, say, a state-machine:

``````void pumpControl( void )
{
static uint8_t
statePump = ST_OFF;
static uint32_t
tStart,
tRunTime;
uint32_t
tNow = millis();

switch( state )
{
case    ST_OFF:
dpH = phValue - pHQ;
if( dpH <= 3 )
{
digitalWrite( rele, HIGH );
tStart = tNow;
if( dpH <= 1 )
tRunTime = 2000ul;
else if( dpH > 1 && dpH <= 2 )
tRunTime = 3000ul;
else
tRunTime = 5000ul;

state = ST_RUN;

}//if
break;

case    ST_ON:
if( (tNow - tStart) >= tRunTime )
{
digitalWrite( rele, LOW );
state = ST_OFF;
}//if

break;

}//switch

}//pumpControl
``````

I'm sorry, I'm posting the whole code now.

``````#include "DFRobot_PH.h"
#include <EEPROM.h>

#define PH_PIN A1
const int relay = 2;
float voltage, phValue= 20;
int pHQ = 7;
int dpH = 0;
unsigned long currentTime = 0;
boolean timer = false;
DFRobot_PH ph;

void setup()
{
Serial.begin(115200);
ph.begin();
pinMode(rele, OUTPUT);
initialTime= millis();
}

void loop()
{
static unsigned long timepoint = millis();
if (millis() - timepoint > 1000U) {
timepoint = millis();
voltage = analogRead(PH_PIN) / 1024.0 * 5000;
phValue = -2.1429 * ph.readPH(voltage, temperature) + 15.71;
Serial.print("pH:");
Serial.println(phValue, 2);
}
dpH = phValue - pHQ;
if (dPH > 0) {
if (timer == false){
currentTime = millis();
timer = true;
}
pump();
}
}

void pump()
{
if (dpH <= 3 && dpH > 2) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 5000) {
digitalWrite(rele, 0);
timer = false;
}
}
else if (dpH <= 2 && dpH > 1) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 3000) {
digitalWrite(rele, 0);
timer = false;
}
}
else if (dpH <= 1 && dpH > 0) {
digitalWrite(rele, 1);
if (millis()-currentTime >= 2000) {
digitalWrite(rele, 0);
timer = false;
}
}
else {
digitalWrite(rele, 0);
timer = false;
}
}
``````

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.