Hi.
Hope someone can help me - very new to this.
I'm trying to get a simple debug message returned from an Arduino Leonardo. Sounds simple, I know.
Here's the sketch:
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
}
void loop() {
if (Serial.available() > 0) {
Serial.println(1);
delay(120);
Serial.println(2);
delay(120);
}
}
Now what happens is...
I upload it to the Arduino. It compiles correctly and uploads correctly. The LEDs blink as per normal and then everything stops. That's correct because from what I understand, the Arduino is waiting for the serial port to be opened. So, I open the serial port (at the same speed, 9600 baud). At which point, the board resets itself with the same sequence of flashing LEDs.
More often than not, the serial port then becomes unselectable within the IDE and I have to wait for ages before it clears itself.
As far as I know, I should be getting 1.....2.....1.....2....etc on the serial monitor.
Am I expecting the wrong thing or am I doing something wrong?
If anyone could help, that'd be great. Would hate to give up at this early stage!
That is a default setting on the circuit board. It allows the serial port to start the bootloader when uploading code, but is a bit of a nuisance other times. You can disable that feature by cutting the "reset en" trace between the two pads on your circuit board. After cutting the trace, you must press the reset button when the IDE starts uploading the code, or the bootloader will not start, and the upload will fail.
edit: I notice the Leonardo does not have the "reset en" pads. What happened there?
The board should reset when you open the serial port, and of course when you press the reset button. The board doesn't actually wait for the serial connection to come up (as far as I know - I'm aware that the Leonardo is slightly different in the way the serial port is initialised but the other boards certainly don't) but would reset when the serial port is opened so it would look similar if the serial port is all you see.
To make sense of what is going on, I suggest you make a couple of changes to your sketch:
Put a Serial.println("Hello, world"); or similar in setup() so that you get some straight forward output at startup time that does not depend on you typing anything into the serial monitor.
Put some code into loop() to toggle pin13 at regular intervals so that you can see the onboard LED flash. I'd suggest once per second, or something like that. Note that the value you pass to delay() is in milliseconds so a delay of 120 is quite short - you want delay(500) or something like that.
The code you currently have in loop() currently waits until it receives input on the serial port before it does anything. You don't mentioned trying to send it any keyboard, and you don't read from the serial port either, so I'm not sure what the point of that is. If there's no point, then take the serial port stuff out of loop() completely.
The board shouldn't stop working when you open the serial monitor, and the serial port shouldn't disappear or stop working on the PC side, and if it does this suggests that the Arduino (or its USB chip) is doing something wrong. I don't see anything in your sketch to cause anything like that, but it's still worth making sure your sketch is squeaky clean before you move on. Do you have any hardware connected to the Arduino, other than the USB cable? What operating system is the PC, and which version of the Arduino IDE are you using?
@PeterH: On the other boards, the usb reset starts the bootloader. How does the Leonardo start the bootloader? Must you press the reset button? BTW, opening the serial monitor on Linux resets my Mega.
recantha:
As far as I know, I should be getting 1.....2.....1.....2....etc on the serial monitor.
Am I expecting the wrong thing or am I doing something wrong?
If anyone could help, that'd be great. Would hate to give up at this early stage!
I suspect your problem is that you misunderstand what the test
if (Serial.available() > 0)
actually means. It returns true when there are some characters waiting to be read from the serial input. If you aren't sending any input characters from your serial monitor, it will never be true, and so you will never see any "1 2 1 2" output.
Try your sketch leaving the test out, and you will see what you expect.
I'm thinking you might be thinking it tests if the serial port is "available" for sending characters. If so, you need to get into the mindset of reading carefully the reference documentation for each function before you use it. It's easy to guess wrong what a function does by only reading example code.
On the subject of auto reset for an Leonardo board, looking at a schematic for the Leonardo one can see there is no hardware generated reset pulse to the reset pin, so any auto-reset function it uses does is just with some combination of the PC's USB driver and the firmware (bootloader?) installed on the Leonardo. This is a big enough difference that any forum past knowledge of the standard arduino auto-reset feature and it's possible problems may or may not apply to the Leonardo board.
Leonardo is a different animal and I for one won't be giving out advice or guesses on what may be wrong when uploading fails or what (and how) happens when one opens or closes the serial monitor.
retrolefty:
Leonardo is a different animal and I for one won't be giving out advice or guesses on what may be wrong when uploading fails or what (and how) happens when one opens or closes the serial monitor.
I was aware that there were some differences, but I had no idea that they were so fundamental. I will follow your example from now on.
SurferTim: @retrolefty: My bad. I missed the "Leonardo" part of the first post.
Well I was not trying to criticize or chastise anyone, it was really just to explain my personal ignorance of the practical differences between the Leonardo design and past arduino 328p based boards. And now with the release of the Arduino Due board we all need to make sure we know what board a poster is asking about before we dash out standard answers to common questions. I note that they gave the Due board a separate forum section in the Products area, I wonder if the Leonardo is enough of a different animal to deserve to also have it's own section?
The arduino platform is starting to spread and diverge in several different directions and it will be a challenge to keep up I guess.
Hi again all.
Thanks for the responses so far. I'm happy to have general Arduino advice, even if not specific to Leonardo (although that obviously would be preferable). I'll try anything at this stage!
I'm running Windows 7, 64-bit. IDE is version 1.0.3
So, here's my sketch now:
int led =0;
// the setup routine runs once when you press reset:
void setup() {
led = 13;
pinMode(led, OUTPUT);
dah();
dah();
dah();
dah();
dah();
dah();
Serial.begin(9600);
Serial.println("Hello, world");
}
void loop() {
Serial.println('flash');
dah();
Serial.println('finish');
delay(500);
}
void dah() {
/* turn on LED for half second, then turn off */
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
}
What someone said about the script waiting for input makes sense - that'll teach me for trying to run before I can walk.
Okay. Now it doesn't reset when I open the serial monitor. This is expected behaviour for the Leonardo, so no worries there.
However, although the LED now flashes (lovely), I don't get any output from the serial monitor.
Serial.println('flash');
flash is a string not a character. Characters go in single quotes, strings go in double quotes. Serial.println('finish');
Same thing here.
Nope. That hasn't helped.
I've tried println, print and write...
The TX light isn't flashing, although the RX light does if I just type something in and hit send.
recantha:
I don't get any output from the serial monitor.
I think this code fragment is needed after the Serial.begin(), to make your sketch wait until the serial hardware has finished initialising:
while (!Serial)
{
// do nothing
}
If my understanding is correct, and if you add that code between the Serial.begin() and the Serial.println(), and then connect the Arduino to the PC via USB with the serial monitor open in the Arduino IDE, and then reset the Arduino, you should see "Hello, world" in the serial monitor.
Are you absolutely sure that the sketch has actually uploaded? Can you upload 'blink' and see the expected behaviour? I'm reluctant to blame the hardware, but I'm running out of alternative explanations.