Go Down

Topic: Arduino Mega 1280 delay() turbocharged (Read 18548 times) previous topic - next topic



I'm using an arduino mega 1280 on 64 bit linux, using arduino 0021 software release. I can compile and upload the "blink" sketch without problems, but once uploaded on the board, the delay(1000) function, which is supposed to add a one second delay, is running much quicker.

Namely, delay(1000) results in a delay of perhaps 100 miliseconds (the LED just keeps blinking really quickly). Changing the function call to, for instance, delay(5000) results in the LED blinking a little slower, but still well above once per second.

Bottom line is, the delay() function is working MUCH faster than it should. The sketch "blink without delay" which uses the milis() function seems to work fine. Also, compiling and downloading the "blink" sketch on windows works fine.

Does anyone have a clue what might be the issue here? Maybe something with the Linux version of the arduino libraries?

Thanks in advance,


I guess you have checked the board setting in the tools menu?


Are you sure your uploading of the blink sketch was successful? I think the bootloader blinks pin 13 led
at a fast rate if there is no valid sketch in the flash memory.



Thanks for the suggestions, however still no go:

dafid: The board is configured correctly as Mega (1280) under the tools menu

retrolefty: The arduino software confirms that upload is done. Also, if i change the delay() argument to a higher number, eg: delay(7000), I can tell the difference in the blink rate. Same happens if I output the signal to another pin (I tried with pin 12).

I'm still thinking this might be a compiler/library problem on 64bit Linux, any takers?



I'm still thinking this might be a compiler/library problem on 64bit Linux, any takers?

Must be something like that.

You would think we would have a good well debugged blink sketch by now.  :D



Haha true that!

I'll try in 32 bit linux version as soon as I fix my other laptop.



Jan 27, 2011, 07:36 pm Last Edit: Jan 27, 2011, 07:38 pm by dafid Reason: 1
I guess the simplest question is always 'what does your code look like?'

One possibility is that 01000 is an octal ( base 8 ) constant in C++
ie 1x8x8x8 = 512(decimal) not 1000 = 1x10x10x10.

So leading '0' zeros on numbers make them smaller...



Code: [Select]
Hi dafid,

Thanks for the tip, but that is not the problem. The code is just the standard blink sketch, i.e:
 Turns on an LED on for one second, then off for one second, repeatedly.

 This example code is in the public domain.

void setup() {                
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(13, OUTPUT);    

void loop() {
 digitalWrite(13, HIGH);   // set the LED on
 delay(1000);              // wait for a second
 digitalWrite(13, LOW);    // set the LED off
 delay(1000);              // wait for a second

and it runs just as expected on the board when it is compiled and uploaded through windows. It is only "turbocharged" when compiled and uploaded through Linux. Also milis() function seems to work fine. I dug up the implementation of delay() and it uses delayMicroseconds() so I'll do some testing on delayMicroseconds() to see if thats the problem or if its some kind of compilation issue.


I just received an Arduino UNO!

The first thing I tried is uploading the blink sketch! Changed the board type to UNO in the IDE and uploaded the sketch.

Behold! The LED is always ON, no flickering like on Arduino Mega 1280, and changing the delay() argument to, for instance, delay(5000), which would result in a slower blinking LED on the Mega, appears to do nothing. The LED is constantly on. Uploading the example "Blink without delay" works fine on both Uno and Mega boards.

This suggests that there is something seriously wrong with the Linux 64 bit libraries since the call to the same function (delay(1000)) results in different behaviours on different boards. I fear that many other functions may give unexpected results and I can't trust these libraries... so if anyone has any more clues about these issues I would love to hear some suggestions before installing a copy of window$...

Also, niadh in the topic "Arduino not responding correctly" is also having strange issues on 64 bit linux - maybe this is related?

Is there a place where I can file a bug with the devs about this issue? Open source hardware should have a good IDE implementation for open source software, no?

Thanks in advance,


Jan 28, 2011, 07:10 pm Last Edit: Jan 28, 2011, 07:18 pm by dafid Reason: 1
Igor, You are right - there is nothing to go wrong in your code :).
And since it works from windows and not from linux - it suggests the either the libararies are the problem, or the compiler is.

It would be good to see the avrdump of the file that is downloaded in each case.

As the WINAVR distribution said (dont recall exactly where, will try to track it down) the libraries are identical for all distributions (32, 64, x86, ...) as they vary with the target that is compiled for not the platform that does the compiling...

But since this a case where the behaviour is broken .. it could be the libraies are different - but not deliberately.

An asside - to your last message - I saw that delay() (in V21 and V22 from what I understand) which is in wiring.c, appears to be
Code: [Select]

void delay(unsigned long ms)
uint16_t start = (uint16_t)micros();

while (ms > 0) {
if (((uint16_t)micros() - start) >= 1000) {
start += 1000;

(Since I am looking at source that I did not use to build the libraries, I am not 100% confident of what I am reading.)

So it does not use delayMicroseconds() but rather loops until it has seen that many lots of 1000 micros() pass by..

And micros is updated by the timer0 interrupt ... and setup() has conditional code to set up timer-0 control registers.

It is all a bit hard in the abstract - as i have 32bit ubuntu and 64 bit windows... and not enough time to reverse this.


I'm running 64 bit linux and Arduino 0021 with both a Mega and a Uno and have had no issues at all with delay(), it is running at the correct speed here. I use the Mega for CNC so I think I would have noticed by now if there was a problem with delay!

I'd suggest backing up your sketches, then completely remove your Arduino directories then download again and reinstall. It sounds like an installation problem.

Which distribution are you using, and what is the output of running avr-gcc -v
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html


Thanks for the help guys!

dafid: In fact you're right. I looked at wiring.c a couple of days ago and for some reason I remembered it using delayMicroseconds(), but I just checked again and the implementation of delay() is identical in my wiring.c as what you posted above. It does indeed use micros() - my mistake!

stimmer: I'm running 64 bit Arch Linux and Arduino 0021. The IDE installation on Arch is a bit of a mess since it bundles its own patched avrdude and librxtx with it instead of using the system wide ones. Also, the installation requires removing 64 bit gcc and gcc-libs and installing 64 and 32 bit compatible versions of these packages (gcc-multilib), which is pretty hackish IMHO. In addition, the package is not available from official "repository" but it is user contributed, so it is very likely to be an installation problem. I have removed everything and reinstalled again but the results are the same.

compiler and other program versions are:
Code: [Select]

??[/usr/share/arduino/hardware/arduino/cores/arduino]$ avr-gcc -v
Using built-in specs.
Target: avr
Configured with: ../configure --disable-libssp --disable-nls --enable-languages=c,c++ --infodir=/usr/share/info --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --prefix=/usr --target=avr --with-gnu-as --with-gnu-ld --with-as=/usr/bin/avr-as --with-ld=/usr/bin/avr-ld
Thread model: single
gcc version 4.5.2 (GCC)

Code: [Select]

??[/usr/share/arduino/hardware/arduino]$ avrdude -v

avrdude: Version 5.10, compiled on Nov 29 2010 at 11:24:36

Can you please let me know your avr-gcc compiler version?



I am on Ubuntu 10.10, avr-gcc 4.3.5
Code: [Select]
$ avr-gcc -v
Using built-in specs.
Target: avr
Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/info --mandir=/usr/share/man --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-checking --disable-libssp --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=avr
Thread model: single
gcc version 4.3.5 (GCC)

I don't believe it would be caused by changing avrdude or librxtx. I wonder if it is a problem with GCC 4.5.2?
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html


Thanks for that.
I'll try to downgrade my avr-gcc but its not a trivial matter on Arch linux (rolling release model).


Hello all,

I would like to report that I have the same problem using the delay() function on arch 32 bit and avr-gcc 4.5.2

I have the problem on 3 different boards, the arduino nano, arduino duelmilanove 168 and the 328.

On the 168 the LED blinks very fast and on the 328 and the nano the led is just on all the time.

I've duplicated the problem on three different computers running arch. On one of them I downgraded the avr-gcc to 4.3.5 and the problem was still there.

The delay() works ok when I compile my code and upload it using Ubuntu 10.04 and avr-gcc 4.3.5.


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131