the thing about the blink without delay example, the sampling frequency is not constant. It makes it blink after it has passed 1 sec and not exactly one second. does that make sense?
This is the code they have in that example:
if (currentMillis - previousMillis >= interval)
as you can see, they have the >= to indicate that they are just seeing if it has passed the one second mark and not exactly one second.
What I am trying to achieve is to collect data exactly 1000 times in one second. If I did the same thing I did with this example, I would not be able to achieve it because the additional milliseconds will eventually add up.
Hope this makes sense. I feel like I did a bad job explaining what was going through my head
If a consistent sample frequency is very important in your application (and there are many where it is), then it is acceptable to do your A/D sampling in a timer ISR. Possibly the best way to understand that is to check out how millis() works. In my v1.6.6 installation of Arduino it is in this file -> C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring.c
It's easy to see in this file how the number of ms is counted in a timer ISR. If you just do your A/D sampling there, you'll have as close to 1kHz sampling as possible. You should start to be careful, however. If you spend 200us in this ISR and it runs every 1ms, then you just used up 20% of your processor. Also, any other ISRs in your system may be delayed by up to 200us now if your timer ISR happens to fire immediately before. Likewise, any other interrupts that fire first may still affect your sample frequency. You may want to minimize the number of other ISRs in your system.
One way to make your ISR more efficient is just start the A/D sample there but read it in a different interrupt when the conversion is complete. I'd need to google how to do that on the Arduino, but I can see from Table 12-1 of the ATmega328P datasheet that ISR #22 = "ADC Conversion Complete" so the capability is there.
It's true what was said about the Arduino's timer itself not being very accurate. If your application can't tolerate the actual sample frequency being +/-5% (I'm estimating, I haven't looked at the datasheet) then you need some sort of an external reference to adjust your timer. One common method is to use the 50Hz/60Hz utility line. In most places you can count on that to be a steady frequency, so you can use it measure the error in your sample frequency and adjust the timer accordingly.