Timer using millis()

Hi everyone :wink: ,

I’m trying to implement a timer using the millis() command for my project, the original code is shown below:

int timeLength;
int timeStart;
bool flagTimer;
void setup() {

  Serial.begin(9600);
  SetTimer(10);
}

void loop() {
  while (flagTimer) {
    //    motor.runSpeed();
    Serial.println(millis() - timeStart);
    if (millis() - timeStart > timeLength) {

      flagTimer = false;
    }
  }
}
void SetTimer(int timeLe) {
  timeLength = timeLe * 1000;
  flagTimer = true;
  timeStart = millis();
}

The code I made above works just fine for any SetTimer below than 60 seconds, for some reasons, whenever I try to set the time above 60 seconds, the timer doesn’t stop but continues counting, I’m not sure what the problem is so any opinions would be greatly appreciated. Thank you.

An int variable can only hold up to 32000-and-a-bit milli seconds. Change all your int variables to unsigned long variables.

SparkFun:

Defining Data Types
The Arduino environment is really just C++ with library support and built-in assumptions about the target environment to simplify the coding process. C++ defines a number of different data types; here we'll talk only about those used in Arduino with an emphasis on traps awaiting the unwary Arduino programmer.

Below is a list of the data types commonly seen in Arduino, with the memory size of each in parentheses after the type name. Note: signed variables allow both positive and negative numbers, while unsigned variables allow only positive values.

boolean (8 bit) - simple logical true/false

byte (8 bit) - unsigned number from 0-255

char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results

unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity

word (16 bit) - unsigned number from 0-65535

unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity

int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE

unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running

long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647

float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.

The code I made above works just fine for any SetTimer below than 60 seconds, for some reasons, whenever I try to set the time above 60 seconds, the timer doesn't stop but continues counting, I'm not sure what the problem is so any opinions would be greatly appreciated. Thank you.

An int variable can only hold up to 32000-and-a-bit milli seconds.

Problem description needs a bit of clarification/revision :confused:

wow I made such a stupid mistake, thank you so much :wink: