Hi Guys,
I am pretty new to coding and am trying to do a project for school.
I am trying to create a water quality monitoring system. I am using the Atlas Scientific pH,ORP & Temperature Probes.I am interfacing with a Mega. I need to display all the probe readings simultaneously.Everything works great alone and the pH & ORP work brilliant together.These are serially connected to the board. The problem I have is when I add the Temperature Probe. It gives me back readings as do all the probes but it starts by giving me 4 or 5 temp readings then a pH the 2 temp then an ORP then 3 more temps and so on like this in random sequences.
I would like to get a reading like pH 1st followed by ORP then followed by the Temp and continuous in that order. I have tested the probes on my UNO and both pH & ORP worked alone with the Temp Probe no problem.
I thought the hardest part would be to get the 2 serial devices working together,obviously not.The code is posted below and attached is a screen grab of the readouts I am getting.
/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino MEGA 2560 board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
**Type in a command in the serial monitor and the Atlas Scientific product will respond.**
**The data from the Atlas Scientific product will come out on the serial monitor.**
Code efficacy was NOT considered, this is a demo only.
The TX3 line goes to the RX pin of your product.
The RX3 line goes to the TX pin of your product.
Make sure you also connect to power and GND pins to power and a common ground.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
*/
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring2 = ""; //a string to hold the data from the Atlas Scientific product
String sensorstring3 = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete2 = false; //have we received all the data from the Atlas Scientific product
boolean sensor_stringcomplete3 = false; //have we received all the data from the Atlas Scientific product
float temp; //where the final temperature data is stored
void setup(){ //set up the hardware
Serial.begin(38400); //set baud rate for the hardware serial port_0 to 38400
Serial2.begin(38400);
Serial3.begin(38400); //set baud rate for software serial port_3 to 38400
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring2.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
sensorstring3.reserve(30);
pinMode(2, OUTPUT); //set pin 2 as an output
}
void serialEvent() { //if the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
if(inchar == '\r') {
input_stringcomplete = true;
} //if the incoming character is a <CR>, set the flag
}
void serialEvent2(){ //if the hardware serial port_3 receives a char
char inchar = (char)Serial2.read(); //get the char we just received
sensorstring2 += inchar; //add it to the inputString
if(inchar == '\r') {
sensor_stringcomplete2 = true;
} //if the incoming character is a <CR>, set the flag
}
void serialEvent3(){ //if the hardware serial port_3 receives a char
char inchar = (char)Serial3.read(); //get the char we just received
sensorstring3 += inchar; //add it to the inputString
if(inchar == '\r') {
sensor_stringcomplete3 = true;
} //if the incoming character is a <CR>, set the flag
}
void loop(){ //here we go....
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial2.print(inputstring);
Serial3.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flag used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete2){ //if a string from the Atlas Scientific product has been recived in its entierty
Serial.print("pH = ");
Serial.println(sensorstring2); //send that string to to the PC's serial monitor
sensorstring2 = ""; //clear the string:
sensor_stringcomplete2 = false; //reset the flag used to tell if we have recived a completed string from the Atlas Scientific product
}
if (sensor_stringcomplete3){
Serial.print("ORP = ");
Serial.print(sensorstring3); //send that string to to the PC's serial monitor
Serial.println(" mV");
sensorstring3 = ""; //clear the string:
sensor_stringcomplete3 = false; //reset the flag used to tell if we have recived a completed string from the Atlas Scientific product
}
temp = read_temp(); //call the function “read_temp” and return the temperature in C°
if (temp){
Serial.print("Temp = ");
Serial.print(temp); //print the temperature data
Serial.println(" *C");
delay(1000); //wait 1000ms before we do it again
}
}
float read_temp(void){ //the read temperature function
float v_out; //voltage output from temp sensor
float temp; //the final temperature is stored here
digitalWrite(A0, LOW); //set pull-up on analog pin
digitalWrite(2, HIGH); //set pin 2 high, this will turn on temp sensor
delay(2); //wait 2 ms for temp to stabilize
v_out = analogRead(0); //read the input pin
digitalWrite(2, LOW); //set pin 2 low, this will turn off temp sensor
v_out*=.0048; //convert ADC points to volts (we are using .0048 because this device is running at 5 volts)
v_out*=1000; //convert volts to millivolts
temp= 0.0512 * v_out -20.5128; //the equation from millivolts to temperature
return temp; //send back the temp
}
Any help would be much appreciated.
Kind Regards