Hi
I have an Arduino Mega and his job is to read sensors and show the values to LCD's.
2 Ultrasonic sensors to read the water level in two tanks and also 2 Flow sensors that can
see the water flow that fills each tank. All this is working nice and now I need to send the
data to a dashboard at IO Adafruit. But this is not why I post this message for now.
My Arduino Mega is sending the values to the serial in a string of data with comma separated value.
,value,value,value,value,
Example:
,41,12,85,24,
It means
41 cm for the level of the tank_1
12 liters per minute filling the tank_1
85 cm for the level of the tank_2
24 liters per minute filling the tank_2
I take the TX pin of the Mega and connected it to the RX of my arduino 8266
and it's ok.
Parsing
I have a parsing code that works on another project. I was using it to receive values and print
it on a wireless LCD and it work nice. (same comma separate value)
Except that this code is made to deal with a receiver so it begin by if receive.available()
In my new project the data is coming from serial so it won't work
I check on the net and read instructions and examples with serial.available
and String and array and almost got it but it's not working.
Instead of showing everything I tried, this is the code that work very nice
with my other project and the receiver.
String text = " ";//String to hold the text
void loop(){
if (receive.available()) //check when received data available
{
char buf[64];
receive.read(&buf, sizeof(buf));
text = (char*)buf;
Serial.println(text);
}
if (text.startsWith(",")) {
int sepLocation[10];
int index = text.indexOf(",");
int i = 0;
String extractedValues[9];
while (index >= 0) {
sepLocation[i] = index;
i++ ;
index = text.indexOf(",", index + 1);
}
for (int o = 0; o < 9; o++) {
extractedValues[o] = text.substring((sepLocation[o] + 1), sepLocation[(o + 1)]);
}
lcd.setCursor(0,0);
lcd.print (extractedValues[0] + "cm");
lcd.setCursor(9,0);
lcd.print (extractedValues[1] + "l/m");
lcd.setCursor(1,0);
lcd.print (extractedValues[2] + "cm");
lcd.setCursor(1,9);
lcd.print (extractedValues[3] + "l/m");
}
}
This code is nice and was parsing very well the data I received on a RF receiver to print the values
to an LCD. Each value from extractedValues[0], extractedValues[1], etc..
Question
How can I modify this code to take the same kind of comma separated value but from my serial pin. ?