Using time related code in Arduino

Hi,I’m using Arduino Nano 33 IoT and I’m working on a project where I use IMU in the arduino to get the angle of the Arduino. And I’m trying to ring the motor if the ardino is held to a certain angle range for a certain amount of time. Here is the code so far:

#include <Arduino_LSM6DS3.h>

double X, Y, Z;
float gx, gy, gz;
float ax, ay, az;
double Fx, Fy, Fz;
double t, d;
int motorPin = 6;

void setup() {
  pinMode(motorPin, OUTPUT);
  while (!Serial);
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);

void loop() {
  unsigned long StartTime = millis();

  if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
    IMU.readAcceleration(gx, gy, gz);
    IMU.readGyroscope(ax, ay, az);
    t = pow(IMU.gyroscopeSampleRate(), -1);

  Z = atan(gz / sqrt(pow(gy, 2) + pow(gx, 2))) * 57.2957795130823;

  if (millis() - d >= 2) {
    Fz = 0.98 * (Fz + t * ay) + 0.02 * Z;
    d = millis();

  if (Fz > 20 && Fz < 90) {
    //digitalWrite(motorPin, HIGH);   // turn the motor on (HIGH is the voltage level)
    digitalWrite(LED_BUILTIN, HIGH);
    //digitalWrite(motorPin, LOW);   // turn the motor off
    digitalWrite(LED_BUILTIN, LOW);

Right now, this code rings the motor as soon as the angle reaches between 20 degree and 90 degree. Let’s say I want to ring the motor if the arduino remains in that angle range for 10 minutes. How should I code this?

Thanks in advance!

Every time through loop(), any time the angle range is exceeded, record a time stamp (the value of millis() ). Separately, but also in loop(), any time millis() - timeStamp > 10 minutes, "ring the motor" (and reset the time stamp).

You can evaluate the ticks -> millis()
You store this time regularly in a variable, unless the skew is too large.

If the skew is not above the limit, the variable will be little different from the current time because it will be updated each time it is tested. If the skew is above the limit, the difference between the stored time and the real time will become larger and larger.

Then you check regularly if the difference between the stored time and the current one reaches a threshold.

It then triggers your action.

I’m trying to figure out how to store the value of millis() function. Whenever I try to store the initial millis() value(the time the IMU starts to be in the set range), the stored value also increases as time goes on.

angleMillis = currentMillis

Is there a way to store it without changing as time goes on?

Is there a way to store it without changing as time goes on?

Just don't change it when you don't want it to change.

boolean WasInRange = false;

void loop()
  boolean isInRange = some boolean expression
  if (isInRange != WasInRange)
    // State has just changed
    WasInRange = isInRange;
    if (isInRange)
      // Just entered the desired range
      angleMillis = currentMillis;

  // If we are in the desired range and have been there long enough...
  if (isInRange && currentMillis - angleMillis > DesiredTimeInRange)
    // Do the thing