Serial.print skipped in void setup()

We keep or move all the cool LeoStick demos, fun stuff, example code and sketches here...
Locked
shackers
Posts: 4
Joined: Fri Jul 13, 2012 8:26 am

Serial.print skipped in void setup()

Post by shackers » Fri Jul 13, 2012 10:03 am

the following code executes correctly when compiled/loaded on a Uno but skips the "Hello World" output on a leostick

Anyone know why? The monitor baud rate has no effect on the result

void setup()
{
Serial.begin(57600); // set up Serial library at 57600 bps
Serial.println("Hello world!"); // prints hello with ending line break
}

void loop()
{
Serial.println("Goodbye world!");
delay(1000);
}

shackers
Posts: 4
Joined: Fri Jul 13, 2012 8:26 am

Re: Serial.print skipped in void setup()

Post by shackers » Sun Jul 15, 2012 12:50 am

Try running this on a Leostick. the variable 'i' IS updated during setup, but the "Hello World" serial print ISN'T executed - or at least, it doesn't appear in the serial monitor window..

But works fine on a Uno! Am I missing something here?

int i=0;
void setup()
{
Serial.begin(57600);
i = 1;
Serial.println("Hello world!");
}

void loop()
{
Serial.print(i);
Serial.println(" Goodbye world!");
delay(1000);
}

User avatar
tronixstuff
Freetronics Staff
Freetronics Staff
Posts: 341
Joined: Tue Nov 01, 2011 10:03 am
Location: Melbourne, Australia

Re: Serial.print skipped in void setup()

Post by tronixstuff » Sun Jul 15, 2012 6:16 am

The Arduino Leonardo, LeoStick and others that use the ATmega32U4 have to be told to wait for the serial port to be available before writing to it. So you need to add the following command in void.setup after Serial.begin(xxxx) and before any Serial.write functions...
while (!Serial) { ; }
which causes the sketch to wait until the serial port is available.
[Currently] to make this work you need to select the board as Arduino Leonardo and not Leostick.
Last edited by tronixstuff on Sun Jul 15, 2012 7:36 am, edited 1 time in total.
Reason: Duplicate - see http://forum.freetronics.com/viewtopic.php?f=27&t=757

Locked

Who is online

Users browsing this forum: No registered users and 1 guest