I have an uno r3 board and I'm using the millis() function to create a timer that counts to 600000 milliseconds, this is the code:
unsigned long time;
int contr;
int timer;
int temp;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
timer = time - contr;
contr = time;
Serial.print("Timer: ");
Serial.println(timer);
temp = abs(temp) + timer; //should be the same as "temp += timer" but I can't explain the change to negative at approximatly 32768
Serial.println(temp);
if(temp >= 60000){
Serial.println("a minute has passed");
temp = 0;
}
}
The problem that I have here is that the timer goes really well untill it hits 32768 milliseconds at that point it becomes negative and starts subtracting itself, I tried solving using an abs() function or multiplying by -1 but the error persists.
unsigned long time;
unsigned long contr;
unsigned long timer;
unsigned long temp;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
timer = time - contr;
contr = time;
Serial.print("Timer: ");
Serial.println(timer);
temp = abs(temp) + timer; //should be the same as "temp += timer" but I can't explain the change to negative at approximatly 32768
Serial.println(temp);
if(temp >= 60000){
Serial.println("a minute has passed");
temp = 0;
}
}