Skip to content

Instantly share code, notes, and snippets.

@inactivist
Created July 29, 2018 15:44
Show Gist options
  • Save inactivist/8c63184e82eed98367dc94c96c9e9012 to your computer and use it in GitHub Desktop.
Save inactivist/8c63184e82eed98367dc94c96c9e9012 to your computer and use it in GitHub Desktop.
Arduino wait for Serial ready with timeout
/**
* Wait for serial connection, with timeout
* @param timeout_milis The maximum time to wait for serial port, in milliseconds
*
* usage:
*
* void setup() {
* Serial.begin(9600);
* // Wait up to 3 seconds for serial connection
* waitForSerial(3000);
* // Continue with sketch setup
* }
*/
void waitForSerial(unsigned long timeout_millis) {
unsigned long start = millis();
while (!Serial) {
if (millis() - start > timeout_millis)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment