Hello, I'am really new on Arduino and try making a pond controller with a atlas scientific ph shield and a multicarier board of this company. For the serial connections I use altsoftserial. I see al lot of possibilities to read the serial port and don't which is the best.
Using a while loop checking with a if, reading the available bytes. I have now the following code which works okay. But sometimes it stops reading. I think sometimes the serial port get some garbage on no CR how can I make my code so I can prevent this hanging and take a new reading.
int Pin_A = 7; //Arduino pin 7 to control pin A
int Pin_B = 6; //Arduino pin 6 to control pin B
int PH = 1;
int ORP = 2;
int EC = 3;
int DO = 4;
float ph_reading=0;
char sensordata[20]; //A 30 byte character array to hold incoming data from the sensors
char *channel; //Char pointer used in string parsing
char charBuf[20];
String sensorstring = "";
boolean sensor_stringcomplete = false;
void getProbe(int probe) {
int i;
lcd.clear();
lcd.home();
lcd.print("* Take Reading * ");
delay(500);
Serial.println("Take Reading");
openProbeChannel(probe); //Open Probe channel
altSerial.print("E\r"); //Stops all readings. Enter standby/quiescent mode
altSerial.print("r\r"); // 'Takes one pH reading
delay(500); //Sends that value to the ph chip ROFL
while (altSerial.available()>0)
{
char inchar = (char)altSerial.read();
sensorstring += inchar;
Serial.print("inchar =: ");
Serial.println(inchar);
Serial.print("Sensorstring =: ");
Serial.println(sensorstring);
if(inchar == '\r') {
sensor_stringcomplete = true;
Serial.println("Sensorstring Complete");
}
}
if(sensor_stringcomplete==1){
sensorstring.toCharArray(charBuf, 20);
if (atoi(charBuf) != 0) {
ph_reading = atof(charBuf);
Serial.println("atoi ok");
//print to LCD
lcd.clear();
lcd.home();
lcd.print("Nieuwe Meting ");
lcd.setCursor(0,1);
if (probe ==1){ //check if probe is PH(1)
Serial.print("Ph =: ");
lcd.print("Ph =: ");
//ph_reading=atof(sensordata);
Serial.println(ph_reading);
lcd.print(ph_reading); //print sensordata to lcd
}
if (sensor_stringcomplete == true)
{
altSerial.flush();
Serial.println("Buffer 1 has been cleared");
Serial.println("-------------------------");
}
sensor_stringcomplete=false;
sensorstring = "";
float ph_reading=0;
}
else {
Serial.println("atoi false");
sensor_stringcomplete == false;
}
}
}
Thanx a lot I learned already so much from the forum.