Wow, you wrote that and replied very quick. Thank You very much, I really appreciate your help.
I didn't think I'd get to it for another day or two, but had to try it. You are a genius with this stuff.
It worked as you wrote it.
However I have one or two problems, when I add it to what I have already, the delays I have for printing to the LCD are buggering the whole thing up.
Visually the delays give me what I am looking for, it shows PH - EC and Temperature, it flicks between EC and Temperature showing EC for 8 seconds and Temperature for 4 but it affects the timing of the pump coming on and off. I understand this is because of the delay being so long (12 seconds).
Now I could take out the delay and temperature reading, and it would show PH and EC values and be constantly polling them but I'm sure that would not be the right way of doing it.
Secondly I am adding a second pump to pump.
One is for pumping PH DOWN if greater than PH7 the other for pumping PH UP if less than PH5.
I didn't expect to just be able to copy and paste over for a second pump, I just tried it anyway, not many errors showed when compiling this, just around the switch, state expected unqualified-id before switch.
I've tried to code some of it myself for both issues, but not getting remotely close, I won't get to it again until probably Sunday. But have you any advice on what to look at next.
///////////////PH Probe - EC Probe - LCD - PH Pump////////////////////
//////////////EC Probe///////////////////
#include "DFRobot_EC.h"
#include <EEPROM.h>
#define EC_PIN A1
float voltage,ecValue,temperature = 25;
DFRobot_EC ec;
//////////////LCD//////////////////////////////////////////
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//////////////L298n Dual H Bridge Driver///////////////////
/////////////Pump Up//////////////
int ena = A7;
int in1 = A6;
int in2 = A5;
/////////////Pump Down//////////////(Not Added Yet)
int enb = 8;
int in3 = 9;
int in4 = 10;
//////////////PH Probe - Calibrated as per instructions with offset value/////////////////////
float calibration = 22.99; //change this value to calibrate
const int analogInPin = A0;
int sensorValue = 0;
unsigned long int avgValue;
float b;
int buf[10], temp;
/////////////Timing - PH Pump - Pause for PH check after dispense/////////////////////////////
enum {IDLE, RUN, PAUSED};
unsigned char disState = IDLE;
unsigned long theTimer;
unsigned long now; // time for everybody all the time
const unsigned long runPeriod = 20000; // run for 20 seconds
const unsigned long pausePeriod = 15000; // rest for 15 seconds
/////////////Set Up/////////////////
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("PH Calibrating");
lcd.setCursor(0, 1);
lcd.print("EC Calibrating");
delay(5000);
lcd.clear();
////////////////////Pump Up//////////////////
pinMode(A7, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
////////////////////Pump Down////////////////
pinMode(8, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
////////////////////////PH Probe Sketch that is avaialbe online///////////////////////////
}
void loop() {
now = millis();
for (int i = 0; i < 10; i++) {
buf[i] = analogRead(analogInPin);
delay(30);
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (buf[i] > buf[j]) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++)
avgValue += buf[i];
float pHVol = (float)avgValue * 5.0 / 1024 / 6;
float phValue = -5.70 * pHVol + calibration;
////////////////////////EC Probe Sketch that is avaialbe online/////////////////////////
{
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U) //time interval: 1s
{
timepoint = millis();
voltage = analogRead(EC_PIN)/1024.0*5000; // read the voltage
//temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
ecValue = ec.readEC(voltage,temperature); // convert voltage to EC with temperature compensation
Serial.print("temperature:");
Serial.print(temperature,1);
Serial.print("^C EC:");
Serial.print(ecValue,2);
Serial.println("ms/cm");
}
ec.calibration(voltage,temperature); // calibration process by Serail CMD
}
////////////////////////Serial Print and LCD//////////////
Serial.print("PH = ");
Serial.println(phValue);
delay(4000);
lcd.begin(16, 2);
lcd.print("PH = ");
lcd.print(phValue);
lcd.setCursor(0, 1);
lcd.print("EC = ");
lcd.print(ecValue);
lcd.print("ms/cm");
delay(8000);
lcd.setCursor(0, 1);
lcd.print("Temperature=");
lcd.print(temperature,1);
/////////////////////////Turning on and Off pumps if PH greater than PH7.//////////////////////////////
// IDLE - the dispenser is not running
// RUN - the dispenser is on a timed run
// PAUSED - the dispenser is enjoying a time-out
switch (disState) {
case IDLE : // not running. should it?
if (phValue > 7) {
digitalWrite(A7, HIGH); // If pH is greater than 7, A7 goes high, turns on LED
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
theTimer = now;
disState = RUN; // now wait out the run period
}
else {
digitalWrite (A7, LOW); // If pH is less than or equal to 7, A7 goes low, turns off LED
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
break;
case RUN : // running. for long enough?
if (now - theTimer >= runPeriod) {
digitalWrite (A7, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
theTimer = now;
disState = PAUSED; // now wait out the pause period
}
break;
case PAUSED : // paused. for long enough?
if (now - theTimer >= pausePeriod)
disState = IDLE; // back to pH control
break;
}
}
/////////////////////////Turning on and Off pumps if PH less than PH6.//////////////////////////////
switch (disState) {
case IDLE : // not running. should it?
if (phValue > 6) {
digitalWrite(8, HIGH); // If pH is greater than , 8 goes high, turns on LED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
theTimer = now;
disState = RUN; // now wait out the run period
}
else {
digitalWrite(8, LOW); // If pH is less than or equal to 8, 8 goes low, turns off LED
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
break;
case RUN : // running. for long enough?
if (now - theTimer >= runPeriod) {
digitalWrite(8, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
theTimer = now;
disState = PAUSED; // now wait out the pause period
}
break;
case PAUSED : // paused. for long enough?
if (now - theTimer >= pausePeriod)
disState = IDLE; // back to pH control
break;
}
}```