How can I handle a situation when the max number that can be store is passed?
I have a code that should run for 100 days straight. From what I understand after around 47 days the unsigned long will not be able to hold the millis() related timing
code:
const byte heartbeatLED = 13;
unsigned long heartbeatMillis;
unsigned long previousMillis = 0;
unsigned long interval = 20; //reading every x ms
//mux pin for reading//
int s0 = 2;
int s1 = 3;
int s2 = 4;
int s3 = 5;
const int NUMSENSORS = 14; // change this variable to set the number of sensors
int SIG_pin = A0;
int previousValue[NUMSENSORS] = {0};
bool isOn[NUMSENSORS] = {false};
int pressThreshold = 25; // Change this variable to set the pressThreshold value
byte filter = 5; //whatever is needed
void setup() {
Serial.begin(115200);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
readSensors();
checkHeartbeatTIMER();
}
}
void readSensors() {
// Loop through all the sensors
for (int i = 0; i < NUMSENSORS; i++) {
// Set the multiplexer to select the correct channel
digitalWrite(s0, (i & 1));
digitalWrite(s1, (i & 2) >> 1);
digitalWrite(s2, (i & 4) >> 2);
digitalWrite(s3, (i & 8) >> 3);
// Read the sensor value
int sensorValue = 1023 - analogRead(SIG_pin);
// Check if the sensor value has changed
if (sensorValue != previousValue[i]) {
// Check if the change is greater than the filter value
if (abs(previousValue[i] - sensorValue) > filter) {
// Update the previous value
previousValue[i] = sensorValue;
// Print the sensor value
Serial.print("value_ch");
Serial.print(i);
Serial.print(" ");
Serial.println(sensorValue);
// Check if the sensor value is greater than the press threshold
if (sensorValue > pressThreshold) {
// Check if the sensor is not already on
if (!isOn[i]) {
// Update the on/off state
isOn[i] = true;
// Print the on/off state
Serial.print("ch");
Serial.print(i);
Serial.print("on ");
Serial.println(isOn[i]);
}
}
else {
if (isOn[i]) {
isOn[i] = false;
Serial.print("ch");
Serial.print(i);
Serial.print("on ");
Serial.println(isOn[i]);
}
}
}
}
}
}
void checkHeartbeatTIMER()
{
//********************************* heartbeat TIMER
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500ul)
{
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
} //END of checkHeartbeatTIMER()
So you think you can't test the subtraction of one number from another?
Look, the millis() counter rolls over after ~47 days. Nothing else happens. It just goes to 0 and keeps counting up.
So, test several end-of-count scenarios.
(very large number) - (small number)
(small number) - (very large number)
etc. etc.
And look at what the results might be. Then apply that knowledge to your millis() math.
Hi, as far as I understand your problem, I had to solve it for me as well and I believe many other people had as well. I believe that your problem is related to the fact that you want to subtract two values of millis() and see if the result is bigger than a certain interval you established in order to do some action. This works fine except for each 47 days when the millis() resets to zero and the difference between the two values doesn't work. Is your problem is this then I have good news for you. I have a very simple solution. Instead of subtracting the two values of millis like you do:
if (currentMillis - previousMillis >= interval)
You call a function instead like this:
if (difTempoMillis(currentMillis, previousMllis) >= interval)
and the trick is in this function.....
The function looks like this:
// ********************************************
unsigned long difTempoMillis(unsigned long tMillis, unsigned long tMillisAnt) {
// ********************************************
// Determines the deference between two readings of millis() providing for
// the case of the reset to zero of the millis() function
if (tMillis >= tMillisAnt) return tMillis - tMillisAnt;
else return 0xFFFFFFFF - tMillisAnt + tMillis; // Situação de overflow da função millis()
// WARNING: Don't change the order of operations in this statement as it can lead
// to unexpected results due to variable overflow.
}