I'm confused as to how to accurately measure the time since loop() started, since millis() measures the total time since the code is being ran. In my case, these are not the same and never will be. I have a switch attached to A0 and ground, and I want the millis() function to initialize to 0 once the switch has been activated. The code I currently have is:
void loop() {
float x, y, z, xa, ya, za;
if (digitalRead(switchPin) == LOW) {
if (IMU.gyroscopeAvailable() && IMU.accelerationAvailable()) {
IMU.readGyroscope(x, y, z);
IMU.readAcceleration(xa, ya, za);
//print these values with other stuff
time1 = millis();
Serial.println(time1);
}
}
}
This code still just returns the normal value of millis(), not the amount of time since the switch has been on. Any help is appreciated!
Save millis() as the start time when the switch becomes closed, not when it is closed
Then, at any time later, if you subtract the start time from the current value of millis() you will have the number of milliseconds since it became closed
I've tried using this code, and I cannot get the millis() value to actually save, and when I put Serial.println(time1 - time2) the value is the same, because both are still counting upwards, and the value is just the permanent difference between them. How can I save the value such that it stops counting upwards?