I wanted to ask that how can i get 5 seconds of data from sensor and there should be delay of 1/60 secs between each reading. I have posted the code below:
yes, I wanted to ask that how can i get 5 seconds of data from sensor and there should be delay of 1/60 secs between each reading. I have posted the code above. Can you pls help out? Thanks
can you please fix your first post ? then we can probably help...
click the edit button and add the code tags
➜ please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)
you need two time based loop, you to drive the acquisition, and one to drive the sampling rate.
The first one could be replaced by a target number of sample to acquire
something like this
const unsigned long runTime = 5000;
const unsigned long acquisitionPeriod = 16667; // in µs
const size_t maxIndex = 300;
int tempx[maxIndex];
unsigned long startMillis, lastAcquisitionTime;
size_t currentIndex = 0;
void setup() {
startMillis = millis();
}
void loop() {
if (millis() - startMillis < runTime) { // we are still in the collection phase (could be a test on currentIndex versus maxIndex
if (micros() - lastAcquisitionTime >= acquisitionPeriod) {
// time to acquire
if (currentIndex < maxIndex) {
tempx[currentIndex] = 1; // make the reading
currentIndex++;
}
lastAcquisitionTime = micros();
}
} else {
// acquisition is now complete, you have currentIndex samples in the array
// handle the data
// get ready for a new acquisition
startMillis = millis();
lastAcquisitionTime = micros() - acquisitionPeriod;
currentIndex = 0;
}
}
of course this will not keep up with the sampling rate you need if the adxl355 reading is too long compared to the 1/60th of a second. You need also a good arduino with enough SRAM for your arrays (3 arrays with 300 samples using 2 bytes is 1800 bytes. You have only 2048 on a UNO and need some SRAM for the rest)
check if the last measurement was 60 ms ago (millis() - previousMeasurement > 60
if true --> measure and store the actual millis() in previousMeasurement
Actually, 16.667 ms will provide a 60 Hz sample rate, if his code and the processor are up to it.
But to get this just right, he'll probably need to use micros(), and non-blocking code. I think an interrupt would be the wrong way to go, should the discussion go there, because the calls to the sensor will take too long.
C