LED blink real time

Hello,

Does anybody know the difference (if there is any) between the theoretical blink time that I program and the real blink time? I am talking about milliseconds and even microseconds. I am pretty sure that the hardware can't do exactly what the code says.

Thanks.

Yes it does EXACTLY what you tell it. However, note that all instructions take time to exicuite, not just the delays.

I can't tell you exactly what the accuracy is, but it can be very accurate if you use the millis() function, rather than delay().

The problem with delay() is not delay() itself, but that the other functions & instructions in your porgram are adding to the total time.

The counter used by millis() keeps getting updated while your program is doing other things. There are still other instructions executing that take-up time, but the delays don't accumulate. So let's say your LED is switching-on for 50 milliseconds, then off for 50 milliseconds. The on & off times might not be exactly 50mS depending on how your code is written... it might be on for 49 mS and off for 51mS, but the loop-time will be 100mS. And if you run it for 100 seconds, you will get exactly 1000 flashes. With the Arduino running off a crystal, the long-term accuracy should be about the same as a digital watch.

DVDdoug: The problem with delay() is not delay() itself, but that the other functions & instructions in your porgram are adding to the total time.

I think the point you are trying to make is that generally people do not include the time it takes to execute the instructions which leads up to or after a delay(). However, in millis() you are only counting a difference in time so these instructions become less important.

One potential way to trip up this generalization is if the check for millis() isn't done often enough.

Thank you all for the replies.

To simplify the discussion I am referring to a code similar to blink (as in short and simple) that turns on the LED when I press a button. The time from the moment I pres the button to the moment the LED lights up is not important. All that matters is the length of time the LED is on. I am pretty sure LEDs have a rise and fall time just like light bulbs.

I was wondering that if I set the LED to turn on for 100 microseconds would the LED be on for 100 microseconds or for 98? Or 104? Nothing is spot on. Not even atomic clocks. That's why I am asking.

I might be wrong but I think that simple things like the resistivity of the solder joints for example can affect the time at short intervals like this one.

Coding 100 microseconds and getting 100 microseconds would be perfect! But unfortunately nothing is perfect in this world. I am curious about the % error this experiment has.

Thanks.

What is it you are trying get at?

I might be wrong but I think that simple things like the resistivity of the solder joints for example can affect the time at short intervals like this one.

You are wrong. Resistance does not affect the speed of of electricity.

I was wondering that if I set the LED to turn on for 100 microseconds would the LED be on for 100 microseconds or for 98? Or 104? Nothing is spot on.

100 micro seconds is in electronic terms a long time. LEDs are used to modulate information in fiber optic systems in the GHz range. The actual time it takes an LED to turn on depends on what LED you are using. A good data sheet should tell you this. The speed of turning an LED on and off limits the modulation rate:- http://www.jensign.com/opto/ledmodulator/index.html It is a poor LED that can't respond at least to 0.1mS. However the delay in turning on is matched, by and large, by the delay in turning off so the on time will be much more accurate than 0.1mS.

Why does this matter to you?

Thank you for the replies. I was talking to some friends about Arduino and Atmega projects and our conversation lead to doing various tests as accurately as possible to different projects we might do (like ballistic chronographs). I was wondering if the hardware configuration is influencing the end result in a bad way.

Thank you again.