I am stumped by the calculation for delay; could you explain this? Why not just use a fixed delay as defined in LOG_INTERVAL?
#define LOG_INTERVAL 1000 // mills between entries
In the "Timestamping" section:
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // <-- What is being calculated here?
The "delay" formula
I ran this formula independently with fixed delay and got these results:
Code: Select all
#define LOG_INTERVAL 1000 // millis between entries
unsigned long timeNow;
void setup() {
Serial.begin(9600);
}
void loop() {
float delayTime;
timeNow = millis();
Serial.print(timeNow);
Serial.print("\t");
Serial.print(timeNow % LOG_INTERVAL);
Serial.print("\t");
Serial.println((LOG_INTERVAL -1) - (timeNow % LOG_INTERVAL));
delay(500);
}
Col 2 = millis % LOG_Interval
Col 3 = (LOG_INTERVAL -1) - (timeNow % LOG_INTERVAL)
1439978 978 21
1440479 479 520
1440979 979 20
1441480 480 519
The values are being correctly calculated. I then ran using the variable delay per your provided code and got these results:
Code: Select all
#define LOG_INTERVAL 1000 // millis between entries
unsigned long timeNow;
void setup() {
Serial.begin(9600);
}
void loop() {
float delayTime;
timeNow = millis();
Serial.print(timeNow);
Serial.print("\t");
Serial.print(timeNow % LOG_INTERVAL);
Serial.print("\t");
Serial.println((LOG_INTERVAL -1) - (timeNow % LOG_INTERVAL));
delay((LOG_INTERVAL -1) - (timeNow % LOG_INTERVAL));
}
Col 2 = millis % LOG_Interval
Col 3 = (LOG_INTERVAL -1) - (timeNow % LOG_INTERVAL)
17000 0 999
17999 999 0
17999 999 0
18000 0 999
19000 0 999
19999 999 0
20000 0 999
21000 0 999
21999 999 0
22000 0 999
23000 0 999
23999 999 0
Would appreciate any insights as to your thinking about the variable Delay calc. Thanks!