How tell Arduino to wait for Serial to be available before continuing
Use the "Serial" and "yield" function together or in combination with a timer or a macro.
There are three ways to get Arduino to wait for Serial, all of which make use of the Serial and yield functions.
A | Standalone
The simplest way to tell Arduino to wait until Serial is available is to use the following line of code.
This can be inconvenient as if Serial is not found (i.e. the board is not plugged into a computer via a USB) the code will not continue.
B | With a Macro
Another way to achieve this is to define a macro that dictates if WAIT_FOR_SERIAL or DEBUG_ENABLED is enabled.
This can be inconvenient as you need to manually set the Macro to be true if you want the board to wait for Serial.
C | With a Timer
A third way to achieve this to create logic that waits for the Serial to be available for a maximum period of time before proceeding.
This is the recommended approach as it will work regardless of whether Serial can be found or not.
Looking to learn more about things you can do with Arduino ?
Search our blog to find educational content on Arduino.