Serial Communication Question

Hey guys,
I am using two Arduino Unos to communicate over serial communication, which i have working. Whenever i send my data from Arduino 1 (sender) to Arduino 2 (receiver), Arduino 2 will read it and print it to serial monitor correctly, however once it goes into its next loop, its prints out the data as 0.00, 0.00, etc, all with a 1 second delay. My question is how do i get Arduino 2 to stop reading when no data is sent, and not print 0.00s!

Arduino 1 Code

float numb1 = 0; 
float numb2 = 0;
unsigned long currenttime = 0;
void setup()
{
  // initialize serial:
  Serial.begin(9600);

}

void loop()
{
  
 if (currenttime <= millis())
 {
  //Data 1
    numb1 = analogRead(A0);
    numb2 = analogRead(A0);
  // example data 286480000,31.44,-61.89,418.93,29.3,35
    Serial.print(numb1);
    Serial.print(",");
    Serial.print(numb2);
    Serial.print(",");
    Serial.print("-61.9");
    Serial.print(",");
    Serial.print("418.93");
    Serial.print(",");
    Serial.print("29.3");
    Serial.print(",");
    Serial.print("35");
    Serial.println();
 
   currenttime = millis() + 10000; 
 }
 }

Arduino 2 (receiver)

float Time, voltage, current, amphour, temperature, something;
// Numbers from the data strings
unsigned long currenttime = 0;


void setup()
{
  // initialize serial:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop()
{

  while (Serial.available() == 0 ) // while serial data available do

  {
  }
  // Data 1

  Serial.println("Data 1"); // Print Data 1
  Time = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(Time); // Print number

  voltage = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(voltage); // Print number

  current = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(current); // Print number

  amphour = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(amphour); // Print number

  temperature = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(temperature); // Print number

  something = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(something); // Print number
  Serial.println("Done");
  Serial.println();

  if (Time > 511 )
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

}

Here is the data printed on Arduino 1 and Arduino 2 serial monitor respectively.
1.
872.00,872.00,-61.9,418.93,29.3,35
872.00,872.00,-61.9,418.93,29.3,35
873.00,872.00,-61.9,418.93,29.3,35
872.00,872.00,-61.9,418.93,29.3,35
873.00,872.00,-61.9,418.93,29.3,35 (this is correct)

Data 1
872.00
873.00
-61.90
418.93
29.30
35.00
Done

Data 1
0.00
0.00
0.00
0.00
0.00
0.00 ( incorrect)
Done

Data 1
871.00
872.00
-61.90
418.93
29.30
35.00
Done

and then this printing repeats.

I have been messing around with Arduino 2's while loop with no luck, and looked through lots of arduino posts. Any ideas, hope i have been clear.
Thanks

Think about what it's doing:

  while (Serial.available() == 0 ) // while serial data available do

  {
>> hang out here while there is no data
  }
  // Data 1
>> than as soon as just 1 byte of data is available do all this stuff:

  Serial.println("Data 1"); // Print Data 1
  Time = Serial.parseFloat(); // Turn string into float, stop at ',' delimited
  Serial.println(Time); // Print number
:
:

I think you should change things up to not do anything until Serial.available() is >=35 or so or how ever many characters you are sending, or just buffer the data received until you see a carriage return or line feed or something indicating the end of a string, and then do the parsing and printing.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. I suggest you use the 3rd example on both of your Arduinos.

...R

I suspect the 'println()' at the end of your sent data is the problem. You receive all the digits and when you go back to the top of loop() there is one character left in the input buffer. Since Serial.available() != 0 the 'while' loop doesn't wait and you start trying to parse numbers. Each parse times out and returns 0.0.

At the end of the parsing, put in a loop to read characters until no more are available. THEN you can go back to the top of loop() and wait for a character.

At the end of the parsing, put in a loop to read characters until no more are available. THEN you can go back to the top of loop() and wait for a character.

Or stop sending useless characters.

Thanks for all the replys!
Robert, i have done "Serial.available() is >=35" and it is working, great idea. I can even reduce the number 35 in the future. I really have to go through my code line by line and think about it! Thanks

Robin2 i have read that tutorial before alot of times, it is great. My only issue was in my planned scenario, i will not be able to implement < or > characters into the sent data. I have an earlier code with them in it and it worked fine, so your tutorial did help me develop my understanding, thanks.

John and Paul i will stop sending useless characters!

EDANN:
Robin2 i have read that tutorial before alot of times, it is great. My only issue was in my planned scenario, i will not be able to implement < or > characters into the sent data. I have an earlier code with them in it and it worked fine, so your tutorial did help me develop my understanding, thanks.

It just depends on what you want / need to send. You can use '!' and '?' or whatever for the start and end markers. Or you can e.g. use 0x02 (STX) and 0x03 (ETX) as start and end marker; see e.g. http://www.asciitable.com/. You can't test the latter with serial monitor though.

EDANN:
i will not be able to implement < or > characters into the sent data.

I had the impression that you are writing the programs for both the sending and the receiving Arduino and if that is true I can't see why you cannot implement start- and end-markers. Of course they don't have to be the < and > characters.

...R

Yes guys i am aware both your inputs, anyways it works thanks for your time!
-Dan

Still, there is something to say!

1. @CrossRoads has solved the problem by getting all the characters of Aduino 1 (the sender) into the serial buffer first, and then carrying out the parsing. This technique has removed everything from the serial buffer; the control goes back for the 2nd pass, and everything works fine.

2. The problem can also be solved (tested) without waiting for all the characters to arrive from Arduino 1 (the sender), but by calling the parse function: float somet = Serial.parseFloat(); for another time. This will parse out the last message that has come from Arduino 1 as a new line (the Serial.println() which @johnwasser has meticulously noted).

3. Observation:
The Arduino 2 (the receiver) starts messing up among the received 6 messages when the Arduino 1,
for some reasons, inserts delays in between successive transmissions of the messages. What is the solution? Software handshaking?