Go Down

### Topic: How accurate are delays within loops (Read 1 time)previous topic - next topic

#### Ben1234

##### Sep 12, 2012, 04:49 pm
I am trying to store a value 40 to EEPROM times a second so the value will be stored every 25ms. If I put a 25ms delay in the loop then it delays 25ms, but takes time to run the rest of the loop so the time will actually be more than 25ms for the loop to run. Does anyone know how long that additional time will be? Is there a function that I can use to test the time? The number of samples a minute is flexible, but I need to know as accurately as possible how long that extra time will be. Thanks

#### Ben1234

#1
##### Sep 12, 2012, 04:50 pm
I am trying to store a value 40 to EEPROM times a second so the value will be stored every 25ms. If I put a 25ms delay in the loop then it delays 25ms, but takes time to run the rest of the loop so the time will actually be more than 25ms for the loop to run. Does anyone know how long that additional time will be? Is there a function that I can use to test the time? The number of samples a minute is flexible, but I need to know as accurately as possible how long that extra time will be. Thanks

#2
##### Sep 12, 2012, 04:54 pm
delay() is pretty accurate, but fairly useless for a variety of other reasons.  One of them of course being the problem you are running into.  It's rather useless when used in conjunction with the rest of the program to perform overall timing.

Take a look at the Blink without Delay sample.  That provides a good method for overall timing.

#### majenko

#3
##### Sep 12, 2012, 04:54 pm
It will be about as long as it takes for you to completely kill your EEPROM.

Storing a value to EEPROM that frequently is just plain stupid.  I'm sorry, but it is.

The EEPROM can only be written to a finite number of times.  Approximately 100,000 times.  At 40 times per second, that's 100,000/40 = 2500 seconds.  That's a little over 41 minutes.

Stop now, before you go any further, and re-think just what you really want to do.

As for calculating how long the loop will take?  Well, that depends on two factors:

1. The number of instructions (and hence the number of clock cycles) the loop compiles into.
2. The number of times the EEPROM has been written to - it slows down with use as it takes longer and longer to erase the cell before writing to it after a while.  The death of an EEPROM cell isn't actually the cell dying per se, but the fact that it takes longer than a threshold timeout to erase.

#### hugo007

#4
##### Sep 12, 2012, 04:55 pm
Quote
I am trying to store a value 40 to EEPROM times a second so the value will be stored every 25ms

Are you crazy dude?
You can't store a value on eeprom 40 times in one second.
Did you know the eeprom writes are limited!!!
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

#### Arrch

#5
##### Sep 12, 2012, 04:59 pm

Does anyone know how long that additional time will be?

That entirely depends on your code. If the rest of your loop is an assignment statement, a mere nano seconds. If the rest of your loop is a few hundred floating point calculations, then it might take a while.

Quote
Is there a function that I can use to test the time?

millis(). You're better off using that than delay() anyway.

#### Jack Christensen

#6
##### Sep 12, 2012, 05:02 pm
Use millis(). Save the value from millis() in a variable (use an unsigned long variable). Then in the main loop, constantly check millis() and compare it against the saved value. When it's 25 more than the saved value, then 25ms have elapsed, so do the next write. The timing should be reasonably accurate, within half a percent or so.

You are aware that according to the datasheet, EEPROM endurance is 100,000 write/erase cycles? At 40 writes per second, that works out to 41 minutes or so. Some folks have tested this and reported significantly higher endurance, but once the datasheet spec is exceeded, I wouldn't bank on it.

#### Jack Christensen

#7
##### Sep 12, 2012, 05:16 pm
http://arduino.cc/forum/index.php/topic,122540.msg921509.html#msg921509

#### AWOL

#8
##### Sep 12, 2012, 05:21 pmLast Edit: Sep 12, 2012, 05:45 pm by AWOL Reason: 1
Topics merged.

I'm really fed up having to tell people not to cross-post.

Did it not occur to you that other people would answer in one thread and not realise that someone else had answered it in the other?
Don't you realise this wastes their time?
How do you think it makes that person feel?

DO NOT CROSS-POST.

CLEAR ENOUGH?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### PeterH

#9
##### Sep 12, 2012, 08:41 pm

I am trying to store a value 40 to EEPROM times a second so the value will be stored every 25ms. If I put a 25ms delay in the loop then it delays 25ms, but takes time to run the rest of the loop so the time will actually be more than 25ms for the loop to run. Does anyone know how long that additional time will be? Is there a function that I can use to test the time? The number of samples a minute is flexible, but I need to know as accurately as possible how long that extra time will be. Thanks

Setting aside the issue of killing the EEPROM, you can avoid the processing delay issue you mention if you code the time comparison correctly:

Code: [Select]
`unsigned long lastTime;const unsigned long interval = 25; // ms...if(millis() - lastTime > interval){    // ... do stuff    lastTime += interval;}`

The crucial part here is that you update lastTime to record when the event was due, not when your sketch handled it. This means the timing doesn't slip by however long it took your sketch to notice that the interval had elapsed.

#### AWOL

#10
##### Sep 12, 2012, 08:42 pm
Code: [Select]
`if(millis() - lastTime >= interval)`
The really crucial part is to get the right comparison.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### udoklein

#11
##### Sep 12, 2012, 11:27 pm
Here is one of the articles on EEPROM endurance: http://blog.blinkenlight.net/experiments/counting-resets/wear-leveling/. Burning out the EEPROM is not a all or nothing issue though. It's more an issue of aging

#### Ben1234

#12
##### Sep 13, 2012, 02:05 am
I will not cross post in the future.

What exactly does the EEPROM "die" after 100,000 writes? I am only trying to store about 100 points which would theoretically be 2.5 seconds at 40 values per second. Afterwards I would upload them to the computer and wipe the old values. Any suggestions of a better way to do this? It was mentioned to me to instead use an SD card. If that is a good idea do you know of any tutorials or threads that you could point me in the direction of. Thanks.

#### majenko

#13
##### Sep 13, 2012, 02:18 amLast Edit: Sep 13, 2012, 02:21 am by majenko Reason: 1

I will not cross post in the future.

What exactly does the EEPROM "die" after 100,000 writes? I am only trying to store about 100 points which would theoretically be 2.5 seconds at 40 values per second. Afterwards I would upload them to the computer and wipe the old values. Any suggestions of a better way to do this? It was mentioned to me to instead use an SD card. If that is a good idea do you know of any tutorials or threads that you could point me in the direction of. Thanks.

A great site that tells you all about it:

The important bit from it:

Quote

...cycling a NAND flash cell entails extremely high electrical fields that can exceed 10 million volts per cm, which is needed to force the electrons to pass through the oxide layer in either direction. Over time, this causes degradation of the oxide layer by causing atomic bonds to break within the oxide layer proper and also at its interfaces with the floating gate and the substrate. The intermediate result is trapping of electrons at the broken bond sites with the oxide layer acquiring negative electrical charges. As a consequence, the erase process becomes slower whereas the programming becomes faster.

Wear levelling is most commonly used to get around the problem - the more cells there are for you to write to, the more you can write before the whole thing becomes unusable.

The great thing with an SD card over the internal EEPROM is that a) the SD card is replaceable so can be swapped when it begins to die, and b) is considerably bigger than the internal EEPROM, so has many millions more cells for you to play with, so you have many millions times more lifetime from it.  Most SD cards include their own wear levelling systems, so you can just forget about it and it does it for you.

There are plenty of tutorials and examples around for using an SD card with the Arduino.  Check the Examples within the IDE for a start.

Oh, and one tip - don't write your data points direct to EEPROM/SD/Whatever.  Collate them in RAM first, and then write them after the sampling has been done together in one batch.

#### Oracle

#14
##### Sep 13, 2012, 02:27 am

I am only trying to store about 100 points which would theoretically be 2.5 seconds at 40 values per second. Afterwards I would upload them to the computer and wipe the old values.

Why can't you store the 100 bytes in SRAM then until you upload it to the computer?  The only reason to use EEPROM is if you want the values to persist through the Arduino being reset.

Go Up