Hello sorry I am new here, I hope I am in the right place. I'm sure this is a programming issue.
What I have is a pH dosing system. I am using an Arduino Uno with an Atlantic Scientific pH probe. I am driving 2 12v peristaltic pumps with relays.
If the pH is too low or high and it turns on one of the pumps, the readings on my pH probe end up being the delay that I have set for my pump to remain off. For instance I want to add solution for 500ms and delay 120 seconds to keep getting readings.
If i take the pumps out of the mix I can set the delay for readings on the pH probe to whatever I want, but then if a pump gets triggered it overwrites it's digitalwrite LOW delay value into the analog readings delays.
I really hope that make sense, It seems like I need to isolate the pump code from the analog code somehow, and I am not sure to do that. Thank you in advance for any advice, and I hope I have posted this in the proper format.
/* Once uploaded, open the serial monitor, set the baud rate to 9600 and append "Carriage return"
The code allows the user to observe real time pH readings as well as calibrate the sensor.
One, two or three-point calibration can be done.
Calibration commands:
low-point: "cal,4"
mid-point: "cal,7"
high-point: "cal,10"
clear calibration: "cal,clear" */
#include "ph_grav.h" //header file for Atlas Scientific gravity pH sensor
#include "LiquidCrystal.h" //header file for liquid crystal display (lcd)
#include <LiquidCrystal_I2C.h>
#define pH_up 2
#define interval 1000
#define pH_down 4
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
// Data Variables
String inputstring = ""; //a string to hold incoming data from the PC
boolean input_string_complete = false; //a flag to indicate have we received all the data from the PC
char inputstring_array[10]; //a char array needed for string parsing
Gravity_pH pH = A0; //assign analog pin A0 of Arduino to class Gravity_pH. connect output of pH sensor to pin A0
LiquidCrystal_I2C pH_lcd(0x27, 20, 4); //make a variable pH_lcd and assign arduino digital pins to lcd pins (2 -> RS, 3 -> E, 4 to 7 -> D4 to D7)
void setup() {
//Initialize LCD
pH_lcd.init(); // initialize the lcd
pH_lcd.backlight();
Serial.begin(9600); //enable serial port
while (!Serial) ;
Serial.println("start");
pH_lcd.begin(20, 4); //start lcd interface and define lcd size (20 columns and 4 rows)
pinMode(pH_up, OUTPUT);
pinMode(pH_down, OUTPUT);
// First Row LCD
pH_lcd.setCursor(7, 0); //place cursor on screen at column 1, row 1
pH_lcd.print("#^(^#)"); //display characters
//Last Row LCD
pH_lcd.setCursor(0, 3); //place cursor on screen at column 1, row 4
pH_lcd.print("--------------------"); //display characters
//Row 2 LCD
pH_lcd.setCursor(5, 1); //place cursor on screen at column 6, row 2
pH_lcd.print("pH Reading"); //display "pH Reading"
if (pH.begin()) {
Serial.println("Loaded EEPROM");
}
Serial.println(F("Use commands \"LOW\", \"MID\", and \"HI\" to calibrate the circuit to those respective values"));
Serial.println(F("Use command \"CAL,CLEAR\" to clear the calibration"));
}
void serialEvent() { //if the hardware serial port_0 receives a char
inputstring = Serial.readStringUntil(13); //read the string until we see a <CR>
input_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}
void loop() {
int sensorValue = analogRead(A0);
// Serial.println(sensorValue);
delay(1000);
if (input_string_complete == true) { //check if data received
inputstring.toCharArray(inputstring_array, 30); //convert the string to a char array
parse_cmd(inputstring_array); //send data to pars_cmd function
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
inputstring = ""; //clear the string
}
Serial.println(pH.read_ph()); //output pH reading to serial monitor
pH_lcd.setCursor(8, 2); //place cursor on screen at column 9, row 3
pH_lcd.print(pH.read_ph()); //output pH to lcd
delay(1000);
if (pH.read_ph() < 5.50) {
digitalWrite(pH_up, HIGH);
delay(250);
digitalWrite(pH_up, LOW);
delay(1000);
}
if (pH.read_ph() > 6.50){
digitalWrite(pH_down, HIGH);
delay(250);
digitalWrite(pH_down, LOW);
delay(1000);
}
}
void parse_cmd(char* string) { //For calling calibration functions
strupr(string); //convert input string to uppercase
if (strcmp(string, "L") == 0) { //compare user input string with CAL,4 and if they match, proceed
pH.cal_low(); //call function for low point calibration
Serial.println("LOW CALIBRATED");
}
else if (strcmp(string, "M") == 0) { //compare user input string with CAL,7 and if they match, proceed
pH.cal_mid(); //call function for midpoint calibration
Serial.println("MID CALIBRATED");
}
else if (strcmp(string, "H") == 0) { //compare user input string with CAL,10 and if they match, proceed
pH.cal_high(); //call function for highpoint calibration
Serial.println("HIGH CALIBRATED");
}
else if (strcmp(string, "C") == 0) { //compare user input string with CAL,CLEAR and if they match, proceed
pH.cal_clear(); //call function for clearing calibration
Serial.println("CALIBRATION CLEARED");
}
}