Global variable has gone awal

EDIT: Problem Solved :slight_smile:

Hello, I am new to this, just brought a elegoo started kit 2 weeks ago but loving it.

I’m working on a clock will hopefully eventually have this mechanical style illustration of all the planets in the solar system.

but for now I’ve been working on a normal clock with the second hand running of a stepper motor but I’ve ran into this problem that seems to be happening a few layers of code deeper. So there is this variable that counts the time of the last second, but when it reaches 32 seconds it starts counting backwards and messes everything up.

I made a simple example to show you what I mean:

int sec = 1000;
int lastSec;
int timeNow;
int tSec;

unsigned long time;

void setup() {
Serial.begin(9600);
}

void loop() {
  tSec  = sec + lastSec;
  timeNow = millis();

  if (timeNow >= sec + lastSec)
    {  
      lastSec += 1000;
                      Serial.print("(B)timeNow    ");
                      Serial.println(timeNow);
                      Serial.print("(B) lastSec  ");
                      Serial.println(lastSec);
                      Serial.print("(B) tSec");
                      Serial.println(tSec);
                      Serial.print("(B)sec ");
                      Serial.println(sec);
      
    }
}

Then it gives:
timeNow 1000: lastSec 1000: tSec1000: sec 1000
timeNow 2000: lastSec 2000: tSec2000: sec 1000
timeNow 3000: lastSec 3000: tSec3000: sec 1000
timeNow 4000: lastSec 4000: tSec4000: sec 1000
timeNow 5000: lastSec 5000: tSec5000: sec 1000
timeNow 6000: lastSec 6000: tSec6000: sec 1000
timeNow 7000: lastSec 7000: tSec7000: sec 1000
timeNow 8000: lastSec 8000: tSec8000: sec 1000
timeNow 9000: lastSec 9000: tSec9000: sec 1000
timeNow 10000: lastSec 10000: tSec10000: sec 1000
timeNow 11000: lastSec 11000: tSec11000: sec 1000
timeNow 12000: lastSec 12000: tSec12000: sec 1000
timeNow 13000: lastSec 13000: tSec13000: sec 1000
timeNow 14000: lastSec 14000: tSec14000: sec 1000
timeNow 15000: lastSec 15000: tSec15000: sec 1000
timeNow 16000: lastSec 16000: tSec16000: sec 1000
timeNow 17000: lastSec 17000: tSec17000: sec 1000
timeNow 18000: lastSec 18000: tSec18000: sec 1000
timeNow 19000: lastSec 19000: tSec19000: sec 1000
timeNow 20000: lastSec 20000: tSec20000: sec 1000
timeNow 21000: lastSec 21000: tSec21000: sec 1000
timeNow 22000: lastSec 22000: tSec22000: sec 1000
timeNow 23000: lastSec 23000: tSec23000: sec 1000
timeNow 24000: lastSec 24000: tSec24000: sec 1000
timeNow 25000: lastSec 25000: tSec25000: sec 1000
timeNow 26000: lastSec 26000: tSec26000: sec 1000
timeNow 27000: lastSec 27000: tSec27000: sec 1000
timeNow 28000: lastSec 28000: tSec28000: sec 1000
timeNow 29000: lastSec 29000: tSec29000: sec 1000
timeNow 30000: lastSec 30000: tSec30000: sec 1000
timeNow 31000: lastSec 31000: tSec31000: sec 1000
timeNow 32000: lastSec 32000: tSec32000: sec 1000
timeNow 32001: lastSec -32536: tSec-32536: sec 1000
timeNow 32054: lastSec -31536: tSec-31536: sec 1000
timeNow 32116: lastSec -30536: tSec-30536: sec 1000
timeNow 32179: lastSec -29536: tSec-29536: sec 1000
timeNow 32241: lastSec -28536: tSec-28536: sec 1000
timeNow 32304: lastSec -27536: tSec-27536: sec 1000
timeNow 32366: lastSec -26536: tSec-26536: sec 1000
timeNow 32429: lastSec -25536: tSec-25536: sec 1000
timeNow 32491: lastSec -24536: tSec-24536: sec 1000
timeNow 32553: lastSec -23536: tSec-23536: sec 1000
timeNow 32616: lastSec -22536: tSec-22536: sec 1000
timeNow 32678: lastSec -21536: tSec-21536: sec 1000
timeNow 32741: lastSec -20536: tSec-20536: sec 1000

Any advice would be awsome :slight_smile:

Thanks

Check out: int - Arduino Reference

The millis() function does NOT return an int.

gfvalvo:
Check out: int - Arduino Reference

"When signed variables are made to exceed their maximum or minimum capacity they overflow. The result of an overflow is unpredictable so this should be avoided. A typical symptom of an overflow is the variable "rolling over" from its maximum capacity to its minimum or vice versa, but this is not always the case. If you want this behavior, use unsigned int."

Ah, cool, thanks. What should I use instead? unsigned long?

What should I use instead? unsigned long?

Did you look at the type that millis() returns?

Sometimes it's useful to read the manual.

There are limitations in the real world read this discussion on 'type'.

Data Types:
https://learn.sparkfun.com/tutorials/data-types-in-arduino

Ok, I tried unsigned long and it worked.

"timeNow 25000: lastSec 25000: tSec25000: sec 1000
timeNow 26000: lastSec 26000: tSec26000: sec 1000
timeNow 27000: lastSec 27000: tSec27000: sec 1000
timeNow 28000: lastSec 28000: tSec28000: sec 1000
timeNow 29000: lastSec 29000: tSec29000: sec 1000
timeNow 30000: lastSec 30000: tSec30000: sec 1000
timeNow 31000: lastSec 31000: tSec31000: sec 1000
timeNow 32000: lastSec 32000: tSec32000: sec 1000
timeNow 33000: lastSec 33000: tSec33000: sec 1000
timeNow 34000: lastSec 34000: tSec34000: sec 1000
timeNow 35000: lastSec 35000: tSec35000: sec 1000
timeNow 36000: lastSec 36000: tSec36000: sec 1000
timeNow 37000: lastSec 37000: tSec37000: sec 1000
timeNow 38000: lastSec 38000: tSec38000: sec 1000"

:slight_smile: I suppose that lesson I had to learn the hard way. I was attempting to debug this for about 4 hours in total. thanks for the fast responses.

I will read Data Types in Arduino - learn.sparkfun.com now.

lesson I had to learn the hard way

Experience is a great teacher!