July 25, 2024, 01:23:07 PM

while(!Serial); doesn't work

Started by danielrleinad, June 18, 2014, 07:26:00 PM

Previous topic - Next topic


Hey everyone,

I want to send text to the serial monitor, but while(!Serial); doesn't work in the setup method. I tried it with while(Serial.available() == 0); and it works (after my input of course).

This is the complete code:
int num = 0;

void setup() {
  while(!Serial); //wait for serial port to connect - needed for Leonardo only

void loop() {
  num += 1;
  if (num >= 23){

void stop() {

  while(1 == 1);

void wait_for_serial(void)

  while(Serial.available() == 0);

Is there a workaround for the Olimexino 32-U4 without sending input?



I'm assuming your reason for not wanting to send input is that you want it to be automated. If that's not the case, you can ignore this post.

As much as I'd hate to do this to you, it would technically work.. So I'm only suggesting it as an absolute worst case scenario. You could start sending data from your board via Serial and waiting for a response from something like a Java program. Once you get a response, your serial port is ready.

Here's a tutorial on Java/arduino serial communication: http://playground.arduino.cc/Interfacing/Java. You can copy & paste this code, just change the serial event to do this instead of printing the input it gets from the olimex board:


now from your olimex board, you can use Serial.available() like so:

while(Serial.available() == 0)




Note that the output from the devices is completely ignored; it doesn't matter what you send to either side.

This is definitely way overkill, but if you never find anything better or just need a solution ASAP, then this should work for you. Hope it helps.

EDIT: This would likely be much simpler to achieve in python, but the concept is the same.