1

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.

  • 1
    A simpler way would be to put the print inside setup which is only done once. – Nick Gammon Jul 6 '18 at 8:35
  • that doesn't work either – Roshan Jul 6 '18 at 8:40
  • My question here is not regarding how to print it once. I want to know why I am not receiving anything on the Uno when I only print it once. Even when I print in setup I do not receive the string on the uno. – Roshan Jul 6 '18 at 8:41
  • Have you used the while(true) loop, without exit(0)? This is not completely clear – chrisl Jul 6 '18 at 9:04
  • 1
    I can't reproduce that. With pin 18 (TX1) on my Mega connected to pin 10 (software serial receive), I get the expected message in the serial log if I reset the Mega (thus forcing it to send the message once). Note that you have pins 18 and 19 reversed in your question text. When I said "how have you wired this up?" I was hoping you would actually show what was connected to where (or a photo). If you have 19 on the Mega connected to 10 on the Uno, then it definitely won't work. – Nick Gammon Jul 7 '18 at 22:48
1

exit(0) is not the command to use, it is used to completely stop the program, although I'm not sure what happens exactly for an Arduino sketch.

To stop a loop (and continue, normally a break statement is used. This works inside a while or for loop and the sketch continues after the while or for loop where the break statement is executed.

Note, you also can start at the beginning of the while or for loop with the continue statement.

  • exit turns interrupts off and then goes into an infinite loop. Therefore any pending serial output is not done. – Nick Gammon Jul 7 '18 at 23:00
1

try putting a delay in the arduino mega code to make sure the uno is completely setup before it starts sending the data. I can see some delays in the uno code, so its possible that the arduino uno is not yet ready when the data is sent. Add a large delay, say 2000 to make sure uno is ready. Also there are neater ways to do this instead of just putting an infinite loop, unless the only goal of your program is just to send one string. You can use a flag that to disable sending.

bool isSent = true;

void loop()
{
  if(isSent)
   {
     isSent = false;    // set to false so we will never send again.
     Serial1.println("e2e4");   
   }
}
0

exit turns off interrupts which will stop the serial output. I suggest something simpler like while (true);

  • i've already mentioned trying that – Roshan Jul 6 '18 at 8:40
0

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?

Clearly there is a timing issue. When you open the serial monitor it resets the Arduino (in this case, I assume the Uno). So, the Uno resets, however the Mega doesn't reset because you didn't open the serial monitor there. Manually resetting the Mega forces it to send the data again (it would have sent it earlier when powered on).

In normal operation, of course you don't need to reset the Mega just to force a string "to be sent once". You have already proved that you can send a string many times, and sending a string once is clearly a subset of that situation. Sending n times is just (sending once) times n.

Your test setup, which requires powering both Arduinos at the same time, and then opening the serial monitor which resets one, but not the other, is what is confusing you. It is not that you can't send strings whenever you want, in normal operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.