Hi everybody,
I'm new to this forum, i'm recently building up an energy monitor, which uses the following code to sampling the analog inputs and get timestamped. I would like to get 1000 samples per second in order to restore the AC wave. the code is running fine, however, for every 6 samples it take, it delays for 5ms to get another 6, i dont know why, could anyone help me?
im attaching my code down here.
code.txt (5.22 KB)
data.txt (1.32 KB)
what about all that writing to storage? When you call flush() I assume that blocks until the buffer is empty...
how fast do you expect to write?
You have in your code:
Line 7:
#define LOG_INTERVAL 5 // mills between entries !!
Line 79:
delay(LOG_INTERVAL);
It should explain the 5 ms delay, isn't it ?
And BTW, a Sequential Analog reading with registers will be much faster.
ard_newbie:
You have in your code:
Line 7:
#define LOG_INTERVAL 5 // mills between entries !!
Line 79:
delay(LOG_INTERVAL);
It should explain the 5 ms delay, isn't it ?
And BTW, a Sequential Analog reading with registers will be much faster.
hi, i was trying to change the delay to 5ms to see if the outcome still have the same problems. But yes, for whatever value i used for the LOG_INTERVAL, it still shows a 5ms delay every 6 takes of values.
for example, if im using 1ms interval, it takes the first group of data at 89ms since started, then 90ms, 91ms, 92ms,93ms,94ms after that, it just jumped to 99ms for the 7th data.
BulldogLowell:
what about all that writing to storage? When you call flush() I assume that blocks until the buffer is empty...
how fast do you expect to write?
hi thank you for the reply. i was trying to fully use the buffer to make the reading process faster, so i used 10 seconds per flush. Since i saw that do not flush too often, so i chose 10 seconds interval. Is this gonna be a problem?
spycatcher2k:
Why urgent? why post code incorrectly?
hi, the code is correct and can perfectly running on the arduino.
but there is only one problem, the output data got 5ms delay for every 6 takes of data, it doesn't matter with the value used for delay(LOG_INTERVAL) function. It happened all the time.
spycatcher2k:
No one said otherwise, it is just posted incorrectly - read the stickies on how to use the forum!
oh, im sorry, im new to the forum, im gonna check the rules rightnow
I suspect your Serial baud rate is too low, your multiple Serial.print and logfile.println to be responsible for most overhead. Obviously, analog readings can be much faster than writing these readings on an SD card.
In fact, another way to proceed might be to:
- Read N loops of n analog values to fill a large buffer.
- Store this buffer on an SD card.
- Serial.print these values on serial monitor ONLY if you need to.
There would be no overhead for the N*n readings.
But anyway, to debug your actual sketch, I would try stepwise:
1/ Change Serial.begin(9600) by Serial.begin(250000) and modify baud rate accordingly on your Serial monitor
2/If it's not sufficient then comment out all the Serial.print() in your loop()
3/If it's not sufficient, write at the beginning of your loop():
printf("\n Time in ms = %d\n", millis());
Comment out all logfile.println, then see how long it takes to proceed 1,2,3,….,N loops of n analog readings.
Writings on an SD card is rather slow and maybe not deterministic.