1

I am trying to get this pH project working and I've noticed that the serialEvent3 never sets the boolean value sensor_string_complete to true. I am not sure if it is actually capturing the values from the probe. What I want is for every loop through it should grab a pH value and that is not happening right now. Most times through the loop the serial3 port is not providing any values. Here is my code:

String sensorstring = "";
boolean sensor_string_complete = false;
float pH;

void setup() {
  Serial.begin(38400);
  Serial3.begin(38400); // pH stamp
}

void serialEvent3() {                                 //if the hardware       serial port_3 receives a char
  sensorstring = Serial3.readStringUntil(13);         //read the string until we see a <CR>
  sensor_string_complete = true;                      //set the flag used to tell if we have received a completed string from the PC
}

void loop() {
  getPh();
  if (sensor_string_complete == true) readph();
}

void getPh() {
  Serial.println("we are here");
  Serial3.print("r");
  Serial3.print(13);
}

void readph(){
  Serial.println("read ph here");
  if (isdigit(sensorstring[0])) {
    pH = sensorstring.toFloat();
    Serial.println(pH);
  }
  else{
    Serial.println(sensorstring);
  }

  sensorstring = ""; 
  sensor_string_complete = false;
}

This returns values such as we are here we are here we are here read ph here 7.0 we are here read ph here 7.0 we are here we are here we are here read ph here 7.0

I want this to return these values every loop. I expect to see: We are here read ph here 7.0 We are here read ph here 7.0

Any help on this would be great. Thanks so much!

3

On a typical Arduino, the delay between loop() ending and loop() running again is a few microseconds. Serial.println() in getPh() is interrupt-driven so that call too takes only a few microseconds. Writing two bytes with Serial3.print() at 38400 bps will take about half a millisecond. In short, there is plenty of time for loop() to run several times before serialEvent3() sets sensor_string_complete.

To call getPh() once and readph() once per loop(), instead of

if (sensor_string_complete == true) readph();

say

while (!sensor_string_complete) {}
readph();

This will stall the loop while the pH sensor takes and reports a reading.

Note, in getPh() instead of using two separate Serial3.print() statements, you can just say Serial3.print("r\r").

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.