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