Data output of my EC probe circuit is a comma separated String as shown below:
5000, 32800, 32.7
This data is (up to 17) ASCII digits representing EC/TDS/Salinity and ending with a carriage return(ASCII 13).
I need to extract the first reading (5000) and convert it into float.
Please advise me on the above.
I also have pH probe circuit .
Output example is as shown below:
4.60
To transform String into float I am using following code ( it works, no problem):
if (sensorPH_stringcomplete){
//if a string from the Atlas Scientific product has been recived in its entierty
char buf[sensorstringPH.length()];
sensorstringPH.toCharArray(buf, sensorstringPH.length());
float pH_reading = atof(bufPH);
Serial.println(pH_reading);
//send that string to to the PC's serial monitor
}
OK, can convert into int.
For me is more important to know how to extract the first reading.
I am using following code:
/*
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 sensorstring = ""; //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_stringcomplete = false; //have we received all the data from the Atlas Scientific product
void setup(){ //set up the hardware
Serial.begin(38400); //set baud rate for the hardware serial port_0 to 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
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
}
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 serialEvent3(){ //if the hardware serial port_3 receives a char
char inchar = (char)Serial3.read(); //get the char we just received
sensorstring += inchar; //add it to the inputString
if(inchar == '\r') {sensor_stringcomplete = 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
Serial3.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete) //if a string from the Atlas Scientific product has been received in its entirety
{
Serial.println(sensorstring);
sensorstring = ""; //clear the string:
sensor_stringcomplete = false; } //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
}
First, might I recommend that you become familiar with the Tools menu? Specifically, the Auto Format item on that menu. Your randomly indented code leaves a lot to be desired.
Second, since you are using the String class, look at the indexOf() method. This will let you find the first comma.
Then, look at the substring() method. This allows you to extract just the portion of the string up to the comma to a new String.
Finally, look at the toInt() method (not documented) to convert the String to an int.
Thank you.
It works, but instead of 5000 it gives me 500. Why?
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
}
void loop() {
// indexOf() returns the position (i.e. index) of a particular character
String sensorstringEC = "5000,32800,32.7";
int firstComma = sensorstringEC.indexOf(',');
Serial.println("The index of ',' in the string " + sensorstringEC + " is " + firstComma);
String EC = sensorstringEC.substring(0,firstComma);
Serial.println("The first reading of " + sensorstringEC + " is " + EC);
char bufEC[EC.length()];
EC.toCharArray(bufEC, EC.length());
float EC_reading = atof(bufEC);
Serial.println(EC_reading);
// do nothing while true:
while(true);
}
The length of the String is NOT the value to use for the 2nd argument. The size of the array is. While it might seem that they are the same value (and currently are), this is where the problem arises. You have sized the array to hold 4 characters, including the terminating NULL. Since you have told the toCharArray() method that the array can hold 4 characters, only 3 are actually placed there, since the trailing NULL goes in the 4th position.
Your array needs to be one element larger, and you need to tell the toCharArray() method how big it is, not how long the String is. It already knows that.
So how to correct my code?
Please help.
Can it be done this way?
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
}
void loop() {
// indexOf() returns the position (i.e. index) of a particular character
String sensorstringEC = "5000,32800,32.7";
int firstComma = sensorstringEC.indexOf(',');
Serial.println("The index of ',' in the string " + sensorstringEC + " is " + firstComma);
String EC = sensorstringEC.substring(0,firstComma);
Serial.println("The first reading of " + sensorstringEC + " is " + EC);
char bufEC[EC.length()];
EC.toCharArray(bufEC, EC.length()+1);
float EC_reading = atof(bufEC);
Serial.println(EC_reading);
// do nothing while true:
while(true);
}