Go Down

Topic: Code only works when serial monitor is open ! (Read 2381 times) previous topic - next topic

Whandall

#15
Aug 05, 2018, 07:23 pm Last Edit: Aug 05, 2018, 07:24 pm by Whandall
Hello psmelakt

"now the code works." : sure, one day or another you will have millis() >= 30000.
Crap. This is in setup, millis starts with zero and counts up, so "one day or another" happens after 30 seconds.

Have you read post #6 ?
which is
You are calling "while(!Serial);" before "Serial.begin()" - that may be an issue.
Why should anyone read that irrelevant superstitious post anyway?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

bidouilleelec

Hello Wandall

Quote
Posted by Whandall
 - Today at 05:23 pm

    Quote


Quote from: bidouilleelec on Today at 05:17 pm

    Hello psmelakt

    "now the code works." : sure, one day or another you will have millis() >= 30000.

Crap. This is in setup, millis starts with zero and counts up, so "one day or another" happens after 30 seconds.

Quote from: bidouilleelec on Today at 05:17 pm

    Have you read post #6 ?

which is
Quote from: Danois90 on Aug 03, 2018, 01:53 pm

    You are calling "while(!Serial);" before "Serial.begin()" - that may be an issue.

Why should anyone read that irrelevant superstitious post anyway?
Are you sure? Did you test it?
I did.

Regards,
bidouilleelec

sterretje

Hello Wandall

Are you sure? Did you test it?
I did.

Regards,
bidouilleelec
And what was the result?

I did test it numerous times in the past (and now again) and it does not make a difference as far as I can see.

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bidouilleelec

#18
Aug 06, 2018, 09:57 am Last Edit: Aug 06, 2018, 09:58 am by bidouilleelec
Hello Sterretje

Quote
And what was the result?
The result is :

Post #6 is right.

Post #9 is just adding a delay and testing !Serial is unusefull as Serial.begin is after the test. (sure one day comes after 30ms)

Regards,
bidouilleelec

Whandall

Post #9 is just adding a delay and testing !Serial is unusefull as Serial.begin is after the test. (sure one day comes after 30ms)
You realize that the sketch works if there is a serial connection?
And that devices without native USB return true always?

That's what while (!Serial); is waiting for.

Adding a timeout to the endless wait makes the sketch work even if there is no serial connection.

The connection test (operator bool) does not need a begin before it can be used.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

sterretje

The result is :

Post #6 is right.
Hmm, wonder wy my results are different (IDE 1.6.6, will try to test later with 1.8.5).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up