I have a perfectly valid sketch.. that bricks Leonardo.

Hi. I am writing a sketch that will control my dishwasher - I planned to replace its broken IC with Leonardo.

My current code is here at GitHub: https://github.com/Bougakov/ArduinoDishwasher/blob/29e14ce5671f620607bf08732502221d9551aeb1/Dishwasher.ino

I've uploaded the latest version to Freaduino Leonardo and Windows immediately showed a pop-up "USB device not recognized". Hopefully, I have Freaduino UNO that I've used as programmer to restore bootloader - it un-bricked the Leonardo.

I can upload any example sketch, such as "Blink" to Leonardo, it works. But as soon as I upload my code, it gets bricked.

Same code uploaded to UNO works kinky. It either hangs in the middle of the line 96, or, if I comment it and all other lines out, freezes around line 121. But at least UNO doesn't get bricked.

"Upload using programmer" doesn't work either - this approach also bricks Leonardo.

What can I do with this?

Get it to work properly on the Uno first?

Is it running out of memory or overwriting memory locations?

...R

Not at all. Just a bunch of variable declarations, simple buzzer function, led blinking and temp measurements.

It uses about 1/3 of memory. Grab my code from https://raw.github.com/Bougakov/ArduinoDishwasher/29e14ce5671f620607bf08732502221d9551aeb1/Dishwasher.ino and compile it yourself, if you wish.

What version of the IDE do you have?

You use the String class in two functions. The String class always causes grief because it can't work effectively with a small amount of memory. A Leonardo has 2.5kB static ram which is only marginally more than in a UNO.
You should change all uses of the String class to null-terminated C strings.

Pete

[quote author=Nick Gammon link=topic=199794.msg1473405#msg1473405 date=1384816800]
What version of the IDE do you have?[/quote]

As a newbie, I was under impression that "if compiler says my code is OK, it will run". Probably, the use of String was a bad idea, and it was what caused my problem. Good that I have 2nd Arduino to unbrick the other one. If I had only single board, it would be a poor situation.

It's not at all easy to brick your Arduino. The Leonardo in particular can be idiosyncratic to upload new sketches to, but you rarely are in the position where you have lost if forever.

if compiler says my code is OK, it will run

If the compiler says it is OK, then its syntax is acceptable. But that's like saying:

King Arthur and his knights visited the Pentagon yesterday and ate three mile long hamburger buns on top of the asparagus.

It might make sense syntax-wise, but operationally, it doesn't.

if compiler says my code is OK, it will run

No, all that means is that the syntax is correct, that has nothing to do with the program running as you expect, or indeed running at all.


Rob

Are you sure it's actually "bricked" ? On a leonardo it would be easy to write a sketch that causes the board to stop responding to USB traffic, but doing an actual hard reset via the RESET button should still get you to the bootloader...

SanjaB:
I've uploaded the latest version to Freaduino Leonardo and Windows immediately showed a pop-up "USB device not recognized".

Your code does something that disables the USB interface? That's not hard to understand, the chip is designed to be able to do that.

The code "PRR1=255;" would do it, and it's perfectly legal!

westfw:
Are you sure it's actually "bricked" ? On a leonardo it would be easy to write a sketch that causes the board to stop responding to USB traffic, but doing an actual hard reset via the RESET button should still get you to the bootloader...

If the program disables USB then pressing RESET would just run that program again from the beginning. It's not a good 'feature' of the Leonardo, but it's not difficult to understand how it could be 'bricked' by bad software.

Yes, but if you hold reset down, and release it at the right moment, the bad sketch doesn't get a chance to run. However I agree the Leonardo can be annoying to program.

For example, if you take Blink.pde, and disable USB in setup:

void setup() {
  PRR1 |= 1<<PRUSB;  //disable USB

Then the board is "bricked" in the sense that no host-side actions will cause another sketch to upload successfully. USB has to be working in order to recognize the traffic that causes an auto-reset.

But hitting the reset button manually will still give you several seconds to upload a sketch that doesn't disable USB, and that should restore the leonardo.

You seem not to be writing your code correctly for this board. I do not see the

   Serial.begin(what ever baud rate);
   // while the serial stream is not open, do nothing:
   while (!Serial) ;

At the very beginning of setup.
Without this you will have trouble like you are finding.

When in doubt read the instructions

Grumpy_Mike:
When in doubt read the instructions

Heresy!

Grumpy_Mike:
When in doubt read the instructions

This is WRONG ...

It should be "When all else fails read the instructions"

...R

Robin2:

Grumpy_Mike:
When in doubt read the instructions

This is WRONG ...

It should be "When all else fails read the instructions"

Ah you sound like the younger generation where reading the instructions is considered as some form of defeat. My kids tell me off about reading instructions.

I have found it best to first of all not read the instructions, play about with it a bit and then read the manual. That way you have a grasp of what is important and what is not and often pick up useful tips and short cuts that you need as opposed to most short cuts which are in just because the programmer thought they would be fun to implement.

FAILED AGAIN :frowning:

It was meant to be funny .....

...R

Robin2:
FAILED AGAIN :frowning:

It was meant to be funny .....

...R

No I was laughing, maybe I should use more :slight_smile: to let you know. :slight_smile:

Grumpy_Mike:
It should be "When all else fails read the instructions"

By which time you can't remember where you put them. :slight_smile: