Collect 5 seconds of data from sensor with a gap of 1/60 seconds between each reading

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:

unsigned long runTime = 5000;

void loop() {

  startMillis = millis(); 

  while (millis() - startMillis < runTime)
  {
    char data[9];
    digitalWrite(adxl355._ss, LOW);
    adxl355.vspi->transfer(0x11);         
    for (int i = 0; i < 9; i++) {
      data[i] = adxl355.vspi->transfer(0x00);
    }
    digitalWrite(adxl355._ss, HIGH);//Disable SPI
    tempx = (data[0]) << 12 | (data[1]) << 4 | (data[2]) >> 4;
    tempy = (data[3]) << 12 | (data[4]) << 4 | (data[5]) >> 4;
    tempz = (data[6]) << 12 | (data[7]) << 4 | (data[8]) >> 4;

    x = adxl355.twosCompliment(tempx[i]) * 0.0000039; //For +-2g: 0.0000039, For +-8g: 0.0000156
    y = adxl355.twosCompliment(tempy[i]) * 0.0000039;
    z = adxl355.twosCompliment(tempz[i]) * 0.0000039;

  }


}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Do you have a question ?

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
image

➜ 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)

ok doing

done

I have done

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)

i am using esp32 wrover

do you mean that data acquisition is different and 60 seconds data is different?

How can I make that there should be 1/60 secs gap between each reading?

check if the last measurement was 60 ms ago (millis() - previousMeasurement > 60
if true --> measure and store the actual millis() in previousMeasurement

➜ that's done here

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

hence the proposed code I shared previously ➜

the code structure is there, just need to be tested and the measurement integrated. ➜ for OP to accomplish

(edit: I corrected the wrong < into >=)

1 Like

Since I was addressing the post by noiasca, I guess I don't see your point. All yours, I guess.

My point was just that we agreed :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.