Go Down

Topic: [RESOLVED] cannot set pin 13 mode when using while (!SerialUSB) in setup (Read 740 times) previous topic - next topic

exedor

Wondering if someone can help me understand what is going on with the code below??

Code: [Select]

void setup()
{
 // This seems to ignore the pinMode statement but commenting out the while statement below works fine.
 while (!SerialUSB);
 pinMode(13, OUTPUT);
}

void loop()
{
 digitalWrite(13, LOW);
}


When changing the line above to "while (!Serial);" it works just as if it was commented out entirely.  I read somewhere that you should put while (!Serial); in your setup as a first step to ensure the serial port hardware is initialized before doing anything else with it.  Wouldn't this also be the case for SerialUSB when using the native USB port??

If the while statement on SerialUSB is used at all the digitalWrite statement has no effect and LED always stays on.

Markus_L811


Wondering if someone can help me understand what is going on with the code below??

Code: [Select]

void setup()
{
 // This seems to ignore the pinMode statement but commenting out the while statement below works fine.
 while (!SerialUSB);
 pinMode(13, OUTPUT);
}

void loop()
{
 digitalWrite(13, LOW);
}


When changing the line above to "while (!Serial);" it works just as if it was commented out entirely.  I read somewhere that you should put while (!Serial); in your setup as a first step to ensure the serial port hardware is initialized before doing anything else with it.  Wouldn't this also be the case for SerialUSB when using the native USB port??

If the while statement on SerialUSB is used at all the digitalWrite statement has no effect and LED always stays on.


Yes thats right.

The code lines
Code: [Select]
while (!Serial);
and
Code: [Select]
while (!SerialUSB);
let the Due what until there is an USB connection between Due and PC. If you use the programming port and set this line
Code: [Select]
while (!Serial);
The Due will walk over this line while the USB is connected but if you connect after the programming the due with the nativ USB-Port nothing will happend the Due waits for an plugin in the programming port.

Vice versa for both ports.

exedor

Ahh, duh.  That makes sense.  I am using native USB in production and programming in development.  I was under the impression it was waiting for initialization of the internal controller and not an actual connection to an end-point/host.  Makes perfect sense.  Thank you very much!

Go Up