DigitalWrite TRUE for a spesific amount of seconds.

Hi!
I have looked at a lot of posts for controlling of the function millis(), but I need it to reset somehow. My program is about achieving a constant PH value, by controlling two pumps (one for acid and one for base). For instance, if the PH value goes above 7,10, then deliver a digital signal to turn on pump for acid. If it goes below 6,90, then deliver a digital signal to turn on pump for base. I only want the digital signal to be on for 3 seconds, and then wait a little while for the solution to stabilize and check the PH value.

My program so far:

const int acid = 6;
const int base = 8;
const int ledPin = 13;
int val = 0;
const double analogPin = 0;
unsigned long startMillis = 0;
const long interval = 3000;

void setup() {
Serial.begin(9600);
pinMode(acid, OUTPUT);
digitalWrite(acid, HIGH);

pinMode(base, OUTPUT);
digitalWrite(base, HIGH);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
delay(1000);
}

void loop() {
unsigned long currentMillis = millis();
val = analogRead(analogPin);
Serial.println(val);

if(val < 550) {
if(currentMillis - startMillis >= interval){
digitalWrite(acid, LOW);
startMillis = currentMillis;}
digitalWrite(acid, HIGH);
} else {
digitalWrite(acid, LOW);
}

if (val > 450){
if(currentMillis = startMillis > interval){
digitalWrite(base, LOW);
startMillis = currentMillis;}
digitalWrite(base, HIGH);
} else digitalWrite(base, LOW);
delay(500);
}

Constant_PH.ino (951 Bytes)

your problem is here:

"if(currentMillis - startMillis >= interval)" and "if(currentMillis = startMillis > interval)"

for you to use millis(), in both cases it should be "if(millis() - startMillis > interval)"

and then instead of "startMillis = currentMillis;" it should be "startMillis = millis();"

hope that helps

sherzaad:
your problem is here:

"if(currentMillis - startMillis >= interval)" and "if(currentMillis = startMillis > interval)"

for you to use millis(), in both cases it should be "if(millis() - startMillis > interval)"

and then instead of "startMillis = currentMillis;" it should be "startMillis = millis();"

hope that helps

It should be

if(millis() - startMillis >= interval)

Hi!
Thanks for your replies!
The pumps still don't run for 3 seconds after the digitalWrites has been true. My program now:

const int acid = 6;
const int base = 8;
const int ledPin = 13;
int val = 0;
const double analogPin = 0;
unsigned long startMillis = 0;
const long interval = 3000;

void setup() {
Serial.begin(9600);
pinMode(acid, OUTPUT);
digitalWrite(acid, HIGH);

pinMode(base, OUTPUT);
digitalWrite(base, HIGH);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
delay(1000);
}

void loop() {
val = analogRead(analogPin);
Serial.println(val);

if(val < 550) {
if(millis() - startMillis >= interval){
digitalWrite(acid, LOW);
startMillis = millis();}
digitalWrite(acid, HIGH);
} else {
digitalWrite(acid, LOW);
}

if (val > 450){
if(millis() - startMillis >= interval){
digitalWrite(base, LOW);
startMillis = millis();}
digitalWrite(base, HIGH);
} else digitalWrite(base, LOW);
delay(500);
}

const double analogPin = 0; Your analogue pin is floating(point)?

Please remember to use code tags when posting code.

You may wish to reconsider your use of a single value of startMillis.

Hi again!

Please don't worry about the data type for the analogue pin at the moment. Right now I am measuring voltage in terms of bits. I haven't connected the PH meter yet. I am only controlling the voltage with a potmeter to see the behaviour of the pumps.

I only need to be able to control the time of how long the pumps should be switch on when their digital pin is LOW. Only pasting the void loop of my code:

void loop() {
val = analogRead(analogPin); //read analog input 0
Serial.println(val); // print value of analog input 0

if(val > 550) {
digitalWrite(acid, LOW); // start pump
millis();
if(millis() - startMillis >= interval){
digitalWrite(acid, HIGH); //stop pump when interval has passed
startMillis = millis();}
} else {
digitalWrite(acid, HIGH);
}

if (val < 450){
millis();
digitalWrite(base, LOW); //start pump
if(millis() - startMillis >= interval){
digitalWrite(base, HIGH); //stop pump when interval has passed
startMillis = millis();}
} else digitalWrite(base, HIGH);
delay(500);
}

Sorry! It is working now! The code I posted on the last comment is working.

Thanks for the help!