Help with code for pH probe

Hi everyone,

New to the forum and figured its about time i seek help regarding the below code.

So i have working on a project. Basically we are receiving data from 3 sensors including ph, conductivity and temp.

I want to write and "if" code where by, if the pH value is with +-1 of its value for 5 minutes, then its stable and another function happens. Not sure how to write this. In other words the pH value is considered stable when it remains with 1ph value its value for a period of 5 min. E.g. if pH was 7 and didn't stray outside of 6 and 8.

the same will apply to temp and conductivity. so all three value would need to be stable at the same time for the code to carry on

how would i write this?

any suggestions would be greatly appreciated :)

cheers, matt

Hi, Welcome to the forum.

Have you written any code to make sure you can receive data from the sensors?

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

What model Arduino do you have? Can you post links to your sensor specs/data please.?

Thanks.. Tom.. :)

Thanks mate.

Details are:
Arduino Uno R3
Sensors include the Atlas Scientific Conductivity, pH, temp and flow. Only concerned with the first three for this problem. Code for pH circuit can be found here https://www.atlas-scientific.com/_files/code/Arduino-Uno-pH-sample-code.pdf

Code is as follows. Only include the section of concern (too big otherwise).

This code is basically a compilation of there codes on forums and its not finished. Feel free to comment/edit as required. Open to any suggestions…

let me know what else i can provide

if pinMode(0,HIGH);   //if auto mode is selcted, will activate auto sampling and beep warning when ready
{
lcd.setCursor(0,4);   //set position on lcd for pH
   lcd.print("Auto_Mode");

conserial.print("R\r");                     //send it the command to take a single reading.
   if(con_string_received==1){              //did we get data back from the Con Circuit?
     con=atof(con_data);                    //convert orp_data string to Con float
     if(con>800){Serial.println("high\r");} //This is the proof that it has been converted into a string.
     if(con<800){Serial.println("low\r");}  //This is the proof that it has been converted into a string.
     con_string_received=0;}                //reset the string received flag.
    
phserial.print("R\r");                      //send it the command to take a single reading.
   if(ph_string_received==1){               //did we get data back from the ph Circuit?
     ph=atof(ph_data);                      //convert ph_data string to ph float
     if(ph>=7.5){Serial.println("high\r");} //This is the proof that it has been converted into a string.
     if(ph<7.5){Serial.println("low\r");}   //This is the proof that it has been converted into a string.
     ph_string_received=0;}                 //reset the string received flag.   

temserial.print("R\r");                      //send it the command to take a single reading.
   if(tem_string_received==1){               //did we get data back from the tem Circuit?
     tem=atof(tem_data);                      //convert tem_data string to ph float
     if(tem>=14){Serial.println("high\r");} //This is the proof that it has been converted into a string.
     if(tem<14){Serial.println("low\r");}   //This is the proof that it has been converted into a string.
     tem_string_received=0;}                 //reset the string received flag. 
     
floserial.print("R\r");                      //send it the command to take a single reading.
   if(flo_string_received==1){               //did we get data back from the flow Circuit?
     flo=atof(flo_data);                      //convert tem_data string to flow float
     if(flo>=1){Serial.println("high\r");} //This is the proof that it has been converted into a string.
     if(flo<){Serial.println("low\r");}   //This is the proof that it has been converted into a string.
     flo_string_received=0;}                 //reset the string received flag. 

long currentTime = RTC.now();                                // Get the current time (currnet time and date)
File dataFile = SD.open("datalog.txt", FILE_WRITE);         //open the file
  if (dataFile) {                                            // if the file is available, write to it:
      dataFile.println(currentTime);                         // logs the time in milliseconds since the program started
      dataFile.print(",");                                   //inserts a comma
      dataFile.println(ph);                                  //logs the pH
      dataFile.print(",");                                   //inserts a comma
      dataFile.println(con);                                 //logs the con
      dataFile.print(",");                                   //inserts a comma
      dataFile.println(flo);                                 //logs the flow
      dataFile.print(",");                                   //inserts a comma
      dataFile.println(tem);                                 //logs the temperature in degrees C
 //     dataFile.print("\r");                                  //inserts a return character
 //    dataFile.close();                                       // commented out and included at the end to try and encapsulate final reading and comment sample has been taken
      delay(30,000);                                          //log parameters every 30 sec when in auto mode

 {
 //this section looks at the incoming data from the pH, Con and Temp and checks it until it is stabilised. Sample need to be stabilised before carrying on.

if (ph==<(ph+1)and >(ph-1))for millis(300000) // ???? need help here
  and 
  if (con
  and
  if (tem

 //if the three sensors are within the parameters as above then LCD will print 'ready to sample' and beep 3 times.

 {
  lcd.setCursor(0,4);                //set position on lcd 
   lcd.print("Ready_to_Sample");     //message to notify sample is stabalised and ready 
  pinMode(9, HIGH);                  // beep speaker 3 times
  delay(3000);
  pinMode(9, LOW);
  delay(1000);
  pinMode(9, HIGH);
  delay(3000);
  pinMode(9, LOW);
  delay(1000);
  pinMode(9, HIGH);
  delay(3000);
  pinMode(9, LOW);
}
      dataFile.println(Sample_Taken);                                 //logs the temperature in degrees C
      dataFile.print("\r");                                           //inserts a return character
      dataFile.close();                                               //closes files
    }
  }
}
if pinMode(0,HIGH);

If you are trying to read the state of a pin then pinMode() is not the way to do it. digitalRead(), or maybe analogRead() is what you want.

However, if you are using pin 0 then it is likely to interact with the Serial interface and/or uploading programs.

Oh yeah. All fixed. :confused: