makes sense in the video? Isn't Serial always different from 0, because it's a static object that has a non-zero address? even if Serial.begin(9600) fails?
Jan Cumps Apparently, a reference to Serial in an if or while argument context evaluates to true if the specified serial port is available else there is a permanent/temporary error. There must be something hidden in the compilation/interpreter that treats Serial special. An if( Serial.isConnected() ) would be better. Secrets are bad engineering practices but what can you do? I cannot find any documentation of the static Serial object other than what's in the arduino.cc API. Have you?
To guard against a single spurious failure instance, I use this in setup() instead of the if-statement for Serial applications:
int count_down = 3; while( ! Serial ) { // Sleep 1 second if not yet ready. delay( 1000 ); // Tried the maximum allowed times already? if( count_down-- < 1 ) { // Tried 3 seconds to get the serial connection ready - giving up. // Timeout error processing goes here } }
Top Comments
-
Jan Cumps
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
-
Former Member
in reply to Jan Cumps
-
Cancel
-
Vote Up
+1
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
-
Jan Cumps
in reply to Former Member
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
Jan Cumps
in reply to Former Member
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children