In my setup I have connected two arduino's Serially. An Arduino Mega is the on that is sending the string and a Arduino Uno is the one receiving it.
- Arduino Mega : RX1(19),TX1(18) - Serial1
- Arduino Uno : RX(10),TX(11) - using SoftwareSerial
- Gnd is connecetd on both boards
When I send a single string from the mega to Uno multiple times(In void loop) I receive the string sent infinite number of times on the Uno which is to be expected:
Arduino Mega - Single string sent multiple no. of times
void setup()
{
Serial1.begin(9600);
}
void loop()
{
Serial1.println("e2e4");
delay(100);
}
Now I want to only send the string a single time, so I inserted an exit(0) or an infinite loop after the delay that is always true.
Arduino Mega - Single string sent only one time
void setup()
{
Serial1.begin(9600);
}
void loop()
{
Serial1.println("e2e4");
delay(100);
while(true);
}
Arduino UNO - code is same for both cases
#include <SoftwareSerial.h>
SoftwareSerial s(10, 11);
void setup()
{
delay(200);
Serial.begin(9600);
s.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
}
void loop()
{
if(s.available()>0)
{
Serial.print("\nrec\n");
delay(100);
String b = s.readStringUntil('\n');
delay(100);
Serial.print(b);
delay(40);
}
}
Note : Arduino uno is receiving the string using : readStringUntil('\n')
But when I send the string only once I receive nothing on the Arduino Uno, can anyone help me figure out why this is happening
What I want to know is why the uno receives the string only when it's sent an infinite number of times and not when it is sent 1 time.
EDIT (July 8)
Still haven't got a solution that I'm looking for, if anyone needs any extra information to help me out please mention it to me.
EDIT (July 8-9)
Sorry it's my fault for writing Tx1 as 19 and Rx1 as 18 although I did not make the same mistake in the code. When I pressed the reset button on the mega the string was received on the uno side.How did this happen? Until now, after I uploaded the code in both the arduino's I opened the serial monitor of the port connected to Uno and I did not receive the string.
Why do I need to reset the mega in order to send the string , shouldn't it work when the serial monitor is opened?
Also I need the mega to send the string automatically without any forced/physical reset is that possible?
I'm asking this because sending a string is part of a larger code where values get updated as time goes by,and a reset will reset all the values to their original state which I don't want happening.
setup
which is only done once.while(true)
loop, withoutexit(0)
? This is not completely clear