Arduino R4 Minima doesn't reset after opening serial monitor

Hello,

I've just switched from an Uno R3 to a R4 Minima.

So far, so good.

There is just one problem. I've been using an external terminal emulator (Putty or Kitty) for logging and saving the sensor readings I've been doing including a timestamp.

With the R3 the arduino did reset itself after starting the terminal and so the program started always new. The R4 doens't do this. After starting the Arduino and then opening the terminal quite a few seconds have passed (~20 sec). With that, I have to edit to logfile at the end of the measurement, so it starts at 0 sec.

Is there a way to get the same behaviour with the R4 as I had with the R3?

Thanks in advance!

Hi @prankenandi.

I don't know of a way to accomplish that. However, there is an alternative solution, which is to configure the program so that it will wait for the serial port to be opened. You can accomplish that by adding the following line:

while(!Serial) {}  // wait for serial port to connect.

This is standard practice in the Arduino world (it is needed for any board with native USB capability; not UNO R4 Minima exclusively).

The relevant documentation in the Arduino Language Reference:

https://docs.arduino.cc/language-reference/en/functions/communication/Serial/ifSerial/


You should note that, in cases where you want to run your program when the board is not connected to Serial Monitor, you must remove that while loop to allow the rest of the sketch to run.

Ok. Thanks for your reply. I couldn't try it today, but I hope it will help.

If or while doesn't make much difference, probably?

If there is no Serial, do nothing or While there is no Serial, do nothing.

However, thanks for your help.

It does. You must use while for this application because you want the program to remain waiting in that loop until Serial evaluates as true when the serial port is opened by PuTTY/etc.

There are some use cases where it would make sense to do something like if(Serial), but not this particular one.

Of course you could do this if you were really set on using if:

while(true) {
  if(Serial) {
    break;
  }
}

But that is just accomplishing the same thing with a lot more code so there isn't any point.