For a project I need to emulate a USB keyboard and so I'm using an Arduino Leonardo.
For different reasons, I need to use the serial port through the USB port at the same time.
I've read a lot of posts on the internet that encourage me to be wary of this type of configuration: they're right!
Here are two sources that should do more or less the same thing:
the first waits 10 seconds, continues, ... and everything works!
the second waits for the port to open, ... and loops ad infinitum!
I must have missed something!
No, it loops until the serial connection gets available (as you also write in the next post).
As you didn't write yet, what intention you had to use these two types of initialization we don't know what hints to give to you.
If you need the serial connection in your sketch to work for it's functionality, the second option should be used. If you just want to avoid printing anything to the serial interface if nobody is listening you can use a
When I use an Arduino (or other board) in a project, I try as much as possible to implement this behavior:
the normal start, as planned in the project specifications
start in development mode, so anyone can continue/troubleshoot/improve the project
For this project, normal mode just needs to send characters emulating a standard USB keyboard.
In development mode, I plug in the card which I may not know what program it has, and in the serial console, I show a magic number and some other version or other information. These informations are stored for example in EEPROM.
Do you thinks it's acceptable to wait a few seconds on every startup just for the case you might want to be in debug mode? If not, you should add a pin that puts the board into debug mode if connected to ground at startup. If you don't see this connection at startup, immediately start over and don't print anything to serial.