Run a while loop for 5 seconds

Hello, I want to make a while loop run for 5 second and after that time is cosumend to exit the loop, but I want to do this each cicle time. This is, I wan to obtain measurements from an accelerometer for 5 seconds, then I will select the max value and store it on an array. Then some computation will be than and after the computation I want to read another 5 second.

I'm trying to doing with millis() and TimerOne library but I can't get it.

Thanks.

Well I'd like to help you out. What do you think you should do next so we/I can help you?

What does that attempt look like? It can't be invisible.

Have you tried using compound conditions to control the while loop. Change any of those conditions and your loop will end.

Can't help without the code, but with it the fix should be quick.
Please post the code using a code block </>

Hi!

Try this:

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

sensors_event_t event;

#define MAX_SAMPLES 50 // The maximum amount of sumples.
#define SAMPLE_TIME 5  // Time to get samples in s.

unsigned long timeMillis = 0;
bool end = false;

int sort_asc(const void *cmp1, const void *cmp2)
{
  // Need to cast the void * to int *
  int a = *((int *)cmp1);
  int b = *((int *)cmp2);
  // The comparison
  if (a == b)
  {
    return 0;
  }
  return (a < b) ? -1 : 1;
}

void setup()
{
  Serial.begin(115200);

  while (!mpu.begin())
  {
    Serial.println("MPU6050 not connected!");
    delay(1000);
  }
  Serial.println("MPU6050 ready!");
}

void loop()
{
  float accX[MAX_SAMPLES];
  float accY[MAX_SAMPLES];
  float accZ[MAX_SAMPLES];
  int samples = 0;
  timeMillis = millis();

  while (((millis() - timeMillis) < (SAMPLE_TIME * 1000UL)) && (samples < MAX_SAMPLES))
  {
    mpu.getAccelerometerSensor()->getEvent(&event);
    accX[samples] = event.acceleration.x;
    accY[samples] = event.acceleration.y;
    accZ[samples] = event.acceleration.z;
    samples++;
    delay(50);
  }

  qsort(accX, MAX_SAMPLES, 4, sort_asc); // qsort(array, array size, size of each array element > float = 4 bytes)
  qsort(accY, MAX_SAMPLES, 4, sort_asc);
  qsort(accZ, MAX_SAMPLES, 4, sort_asc);

  Serial.print("\nX = [");
  for (int i = 0; i < samples; i++)
  {
    Serial.print(accX[i]);
    if(i < samples - 1)
    {
      Serial.print(",");
    }
  }
  Serial.println("]");

  Serial.print("\nY = [");
  for (int i = 0; i < samples; i++)
  {
    Serial.print(accY[i]);

    if(i < samples - 1)
    {
      Serial.print(",");
    }
  }
  Serial.println("]");

  Serial.print("\nZ = [");
  for (int i = 0; i < samples; i++)
  {
    Serial.print(accZ[i]);

    if(i < samples - 1)
    {
      Serial.print(",");
    }
  }
  Serial.println("]");

  Serial.print("\nMax. X: ");
  Serial.print(accX[samples - 1]);
  Serial.print(", Max. Y: ");
  Serial.print(accY[samples - 1]);
  Serial.print(", Max. Z: ");
  Serial.print(accZ[samples - 1]);
  Serial.print(", samples: ");
  Serial.println(samples);
}

In my test I got this output:

X = [-3.14,-3.16,-3.20,-3.20,-3.22,-3.22,-3.23,-3.23,-3.24,-3.24,-3.24,-3.24,-3.24,-3.24,-3.25,-3.26,-3.26,-3.27,-3.27,-3.28,-3.29,-3.29,-3.29,-3.29,-3.30,-3.30,-3.30,-3.31,-3.31,-3.32,-3.32,-3.33,-3.33,-3.33,-3.33,-3.34,-3.35,-3.35,-3.36,-3.36,-3.36,-3.38,-3.38,-3.39,-3.39,-3.40,-3.41,-3.42,-3.43,-3.44]

Y = [1.33,1.35,1.36,1.36,1.36,1.36,1.37,1.38,1.38,1.38,1.38,1.39,1.39,1.39,1.39,1.39,1.39,1.39,1.39,1.39,1.40,1.40,1.40,1.40,1.40,1.40,1.40,1.41,1.41,1.41,1.41,1.41,1.42,1.42,1.42,1.43,1.43,1.43,1.43,1.43,1.43,1.44,1.44,1.45,1.45,1.45,1.45,1.45,1.47,1.48]

Z = [-9.05,-9.06,-9.09,-9.10,-9.11,-9.11,-9.12,-9.12,-9.13,-9.13,-9.13,-9.13,-9.14,-9.14,-9.15,-9.15,-9.15,-9.15,-9.15,-9.16,-9.16,-9.16,-9.17,-9.17,-9.17,-9.17,-9.17,-9.17,-9.17,-9.17,-9.18,-9.18,-9.18,-9.18,-9.18,-9.18,-9.19,-9.19,-9.19,-9.19,-9.19,-9.20,-9.21,-9.21,-9.21,-9.23,-9.24,-9.24,-9.24,-9.25]

Max. X: -3.44, Max. Y: 1.48, Max. Z: -9.25, samples: 50

I got 50 samples in less than 5 s so to get samples during 5 s you have to increase the value of MAX_SAMPLES. The amount will depend of the maximum free memory available to waste with this function.

Best regards.