Created
July 29, 2018 15:44
-
-
Save inactivist/8c63184e82eed98367dc94c96c9e9012 to your computer and use it in GitHub Desktop.
Arduino wait for Serial ready with timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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