strange behavior...

I’m looking for some help on this program… I was just blinking a LED, and instead of using the Delay() function, I decided to make a nested cycle to simulate that delay, but the next code doesn’t work… it (MyDelay() function ) doesn’t delay anything… what’s wrong with it?

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


//If clock is 16Mhz… then, this nested cycle of 1610001000 = 16 Million, at least should produce a delay of 1 second…
//I’m guessing that a cycle of the clock is used to perform a single addition (K = K +2 ), if not more cycles are needed…
void MyDelay()
unsigned long K =0;
for(unsigned int X=0; X < 16; X++) {
for(unsigned int Y=0; Y < 1000; Y++){
for(unsigned int Z=0; Z < 1000; Z++){
K = K +2;

void loop() {
digitalWrite(13, HIGH); // set the LED on
MyDelay(); //This is suppose to delay, but
digitalWrite(13, LOW); // set the LED off


I'm not sure, for some reason arduino doesn't like "unsigned int" as a counter variable. Try just int.

The compiler has decided that MyDelay doesn't do anything useful and optimized the whole thing away. Hence, no delay. In other words, you are adding to K, which you never use anywhere.

Change it to:

 volatile unsigned long K =0;

Then the compiler no longer makes that assumption.

BTW, when I tested, the delay was more like 27 seconds. Those instructions take a couple of clock cycles each in some cases, plus you have the adding of loop counters, testing, and so on.

I presume you have a reason for not just using the inbuilt delay() function which lets you delay with reasonable accuracy to within a millisecond or so?

Thank you so much. It resolved my problem perfectly. The main reason I decided to test that kind of code, was because I tried to make the same additions 100,000 times in a Netduino, that has a clock of 48Mhz, and a RAM available of 60kB. I bought one and made a test on it, just to find out that a cycle of 100,000 additions (K = K +2) took to the Netduino, 5 seconds!!!..
Later on, I found out that the platform . NET microframework installed on the firmware of the Netduino, is the responsible for such latency. To make programs on C#, is to make unmanage code, an every instruction is interpreted for the .NET microframework and translated into a native code in real time before is in fact executed.

int K = 0;
for (int C = 0; C < 100*1000; C++)
K = K + 2;
So simple eh?.. With a clock speed of 48mhz, 100 thousand additions should be performed in a blast. But in real life it took 5 seconds!!!
5 seconds for many applications is not relevant. But for a lot of them, where the toggle of I/O pins or fast processing of data is a must, this platform is useless. Imagine an application where you have to display every 0.1 seconds an image of 100,000 pixels on a matrix of 100x1000 Leds.
All that latency is due the .NET Micro Framework and C# combination. In order to take all the advantage of that fast micro controller, you have to erase .NET Framework from the Netduino and write your programs in native language (for the ARM7 microchips), which complicates the main purpose of buying something better and easier to work with. I’m a hobbyist, and a hobby doesn’t have to be that complicated, because when a hobby becomes a nightmare to program, the pleasure is gone. And there has to be pleasure in a hobby.
Arduino in the other hand, is easier to program, harder to debug, and the Achille’s Heel is… its extremely low space of RAM (2kb for board “UNO”).

I haven’t used my netduino much, but I think you may be making a bit of a leap from ‘my program was slow’ to ‘netduino is unusable’. Were you running in debug mode or release? Did you have the debugger running on your PC? Does it have USB 2.0 or higher? Finally, did you pose the question on their forums? Of course the framework imposes overhead, but are you sure what you see is all attributable to the framework?


Did you try this code in the Netduino? Just 100,000 additions. Just that. [u]No debugger[/u], just blink a LED after that cycle is complete, and let us know your conclusions.

Over 200,000 reads from memory, 200,000 additions, 200,000 writes to memory, 100,000 comparisons, and 100,000 jumps, since you happen to be incrementing X, Y, and Z as well... I believe (although am not sure) that each read and addition will take 4 cycles, since there's 4 bytes and it's an 8 bit processor.