upload fails … sometimes

Hi there!

After numerous succesful uploads my Arduino Mega board decided to throw an error during upload:

avrdude: stk500v2_ReceiveMessage(): timeout

It looks like the Arduino does not like the sketch since the blink sketch and lots of other example sketches upload without a problem.
Yet it’s hard to find out where the problem is: the exact sketch that was working before does not upload anymore, and even some Arduino-supplied examples fail at uploading.
Most of these sketches do upload fine after a few tries, but the sketch I’m working on is more stubborn. To make it a bit more confusing: sometimes this problematic sketch uploads without a problem.

I tried finding the source of the problem by commenting out sections of the code, but every time I found a possible suspect, a small test-sketch prove me wrong by uploading just fine.
It was a tabbed file. I merged the tabs into one file and it uploaded successfully, but when I worked on it a bit further, the problem came back.
It seems that the sketch-size is not involved: I made a test-sketch and maxed it out with lots of strings but that just uploaded without a problem.

Can someone clarify this situation a bit?

Other details:
The problematic code works fine on the Arduino Diecimila.
I’ve read about the Arduino not liking the following form:

if(a = 0) {}

but I checked that all my comparisons always use ‘==‘.
There is no !!! in the sketch and a serial loopback test evaluated successfully.
I'm working on a Macintosh with OS 10.9.1 and the Arduino I want to use is a Mega 2560.

Kind regards,
Elias

Attached is the code (without the libraries attached)

lidar_03_menu.ino (8.22 KB)

1 Like

The Mega board had issues in the past.
As far as I know the bootloader was updated for the Mega about 1 year ago or half a year ago.
Do you have a programmer ? To rewrite the bootloader.

Do you use the newest Arduion software, version 1.0.5 ?
You could try the new Beta version.

You can try with a better and shorter USB cable, and use it on a desktop computer, not a laptop.

Sometimes the sketch has a problem when the size is going over 32k or 64k. You can try moving text to flash.

Hi Caltoa,

I bought the Mega in may 2013.
I don't have a programmer yet. But found a few pretty cheap ones. Do you think it's safe to burn a new bootloader? I'm pretty much a total noob in this area...

  • Yes, I use Arduino 1.0.5.
    I also tried the nightly build (if that is what you mean with beta) but uploading results in the same weird problem.
  • I can try using a better / shorter USB cable, but that wouldn't explain why some patches upload without any problem.
  • I'm working on a desktop computer.
  • I'm confused since the problem mostly occurs with the patch I'm working on, yet I can't find a bad piece of code to reproduce the error.
  • What do you mean with 'moving text to flash'? The sketches I'm having trouble with are around 11,000 bytes (but will grow over time). But since I had no trouble uploading a sketch of +- 250,000 bytes I don't suspect that size is a problem there.

May 2013... When I read the release notes, http://arduino.cc/en/Main/ReleaseNotes
I read at version ARDUINO 1.0.4 - 2013.03.11, this : "Fixed a bunch of bugs on Mega2560's bootloader".
You might have the older version or newer version.

With the BETA version, I ment the Arduino 1.5.5 BETA

Burning a bootloader is easy, once you have done it. Perhaps something to keep in mind for later.

Did you know an Arduno Mega clone is about 16 dollars inclusive shipping ?

Some destroy an Arduino board with by touching it with a 12V wire, so it can be good to have a spare.

The upload problem that existed was with certain code and with certain size. Some changes in memory could make it work.
It is even possible to create a memory filler that could make it work.

prog_uchar memoryFiller[] PROGMEM  = {"This is a very long line of text, make it very long to fill flash memory"};

Start here: http://arduino.cc/en/Tutorial/Memory
And click on the PROGMEM link.

There is also the 'F()' macro, that places a string in Flash, just like PROGMEM.

// This "Hello" is in code, and will be copied to RAM during startup.
Serial.println("Hello");

// This "World" is in code, and stays in code. No RAM is used.
Serial.println(F("World));

You might have the older version or newer version

The suspence is killing me :slight_smile:

I tried the 1.5.5 BETA, but I get the same upload-error.

I ordered a small programmer (https://www.sparkfun.com/products/9825). When it arrives I'll try some bootloader burning!

Those Arduino clones are surprisingly cheap! But I've read on this forum that it's hard to debug because you're never sure the device is made to Arduino-specification.

I'm very new to memory management (I don't yet understand your examples), but I'll follow your advice to start learning at the Memory tutorials. Thanks a lot!

I use the cheap USBasp programmers from Ebay with a extra 10-pin to 6-pin adapter.
They do sometimes have timing issues. One USBasp works only on one of my computers, and another USBasp programmer works on my other computer.

The clones use very cheap components, I have about 5 clones and had to repair 2 of them.

The memory usage is worth looking into.
When the Arduino starts, the RAM is empty, the sketch is only in the code (flash) part. During startup the variables in RAM are filled with their initial value which is copied from flash, so they cost both RAM and flash memory.

If you can't fix the problem, you can upload your sketch. Perhaps with something like the memoryFiller variable in my previous post it is possible to enlarge the sketch with dummy data from 11k to more than 16k or 32k or 64k. Enlarging the size beyond such a boundery can sometimes make it work.
Old Mega boards had a problem with three !!! characters in the binary code (the resulting binary code, it can be in your code without knowing it).
It seems to be fixed in the new bootloader since version 1.0.4, that is the same version that I wrote about before.
http://code.google.com/p/arduino/issues/detail?id=459

I tried to burn the bootloader with the AVR Pocket Programmer from sparkfun, didn't work. I tried it with an arduino Uno as ISP, didn't work (I guess), and lastly I burned the default bootloader for the Arduino Mega from the Arduino IDE successfully with the AVRISP mkII from Atmel.
Yet the problem didn't go away.

I was thinking that the errors could be related to the MenuBackend 1.4 ( MenuBackend, new menu managment library - Development - Arduino Forum ) but meanwhile, a sketch without that library also failed to upload.

I'll try looking into the Memory issue later, but how can that be relevant if the patches uploaded fine a while ago?

Hard to wrap my head around this one!

You have tried a lot by now. This is indeed a hard one.

That behaviour was typically for the old bootloader.
With the new bootloader the sketch size and the binary data should no longer influence any upload fail.

A sketch that fails to upload, sometimes does upload ? Then it might be a hardware problem : noise, voltages, timing, and so on problems.

Do you have an official Arduino Mega 2560 ? Or a clone from Ebay ?
Can you try a different computer with a different usb cable and a different Arduino Mega board ?
Or at least measure the 5V of the Arduino board during uploading.
Can you inspect the Arduino board with a magnifier, to see if the soldering is done well.

Perhaps you can also try to remove all the Arduino and all the Java from your computer, but keep your project files and user libraries. Re-install Arduino 1.0.5.
Arduino comes with a subset of Java included, so if there is some Java on your computer, it might conflict with the Java of the Arduino IDE.

If another USB port on your computer with another USB cable does not solve it, then a new Arduino Mega board is the best option in my opinion.

Thanks Caltoa,

sorry, it's been a while now.

I do have a official Arduino mega 2560.

I tested the same board on another mac (10.8 Mountain Lion): a file that gave problems when uploading on my computer uploaded just fine here. I concluded that the problem would be in my computer. I tried few things:

  • booting windows XP. That didn't work. Maybe because I had not the right drivers or so.
  • I found out I had two versions of Java (1.6 and 1.7). I deinstalled 7 and installed a clean 1.6 version. Didn't solve the problem though
  • I made a new user and tried to upload there, but encountered the same problem.

The strangest thing is that I can get rid of the upload problem (timeout() ) by writing some unimportant code: Sometimes I write a few Serial.prints, sometimes only a lot of them make the upload work, but I have had a moment where changing a zero to a one (as argument to a simple function) made the difference. If a sketch uploads, it will upload the next time. So the process is not random. This is the way I'm coding now: for every upload I'm writing a few minutes nonsense-code to try to make the upload work.

Maybe downgrading to Mountain Lion could help, but it seems to be a hassle since I would break compatibility with some apps I have. And I'm almost done with coding so I think I leave this issue where it's at now...

Thanks for thinking with me!

Elias

It sounds to me like your computer is not resetting the Mega and starting the bootloader reliably. The other computer didn't have any problems uploading to it?

If it is not random, get the Mega into the situation that the upload would fail, except this time when the code is compiling, press the reset button on the Mega, and when the IDE shows "uploading", release the reset button. Does that work?

Just a thought...

Hi SurferTim!

Indeed, the other computer didn't seem to have any problems uploading to the Mega.
I tried what you suggested: bring the code in the state that it didn't wanted to upload and pressed the button when I hit upload (compiling) and released the button when the IDE showed 'uploading'. I tried it a few times but unfortunately the sketch didn't want to upload.

I don,t have a Mac anymore but my Dell XP failed to upload over and over again, until I checked the sync. I found that the board had changed the port requirement to com6. I changed the port name to com6, Told my WP to sync everything, and have not experienced the problem again. Best to You and your situation, Guydsk9@gmail.com.

Reading the posts, I suddenly remember something. I had a situation that I could upload once, but not a second time. It had to do with low voltage on the usb-to-serial adapter.

So the problem is software timing or hardware timing or hardware voltages. And perhaps it has to do with a weak reset.

I suggest not to try to find the problem. Use another computer if possible. This is a very rare situation.