arduino - millis()

Whenever you write an equation in C/C++, the data types being operated on have a very real effect on the equation's output.

Each type like int, float, and unsigned long have different behaviors, and take a certain amount of space in memory to store.

int (on arduino) is store in 16 bits, with half of its values being given to negative numbers, half-1 given to positive values, and one value given to 0. That gives it a range of -2^15 (-32,768) to +2^15-1 (32,767).

unsigned long (on arduino) is 32 bits, but none are designated as negative. its range is then 0 to 2^32-1 (4294967295).

What kind of math? What kind of other type of processing is excluded while working with millis?

The crux of the issue is that it the time millis returns ever got past 32767 and you tried to store it in an int, the arduino couldn't do it, because an int can't hold that big of a number. The type of math this is off limits is math happening to smaller data types, not any specific operations. Maybe these examples will help:

  int i = 32767;
  Serial.println(i);
  //No problems here; it fits just fine

32767

  i = 32767 + 1;
  Serial.println(i);
  //Oh no, the value didn't fit

-32768

  unsigned long fake_millis = 42000;
  i = fake_millis;
  Serial.println(i);
  //This is an example of millis going past an int

-23536

  i = -10;
  unsigned int j = i;
  Serial.println(j);
  //no way to put a negative number in an unsigned value

65526

  uint32_t k = fake_millis;
  Serial.println(k);
  //unsigned long is a uint32_t on arduino; this works great!

42000

The way this is implemented is really quite genius; If you are interested in where these numbers come from and why they spill over in the way they do you should look into same explanations of two's complement number representations.


millis() returns a unsigned long, which is a 32-bit unsigned integer on the Arduino. When you then try to do something like unsigned int time = millis() - 1000, you try to store that in a 16-bit unsigned integer unsigned int. A 16-bit integer can never hold a 32-bit value.

According to the C specification, paragraph 6.3.1.3, the upper 16 bits are discarded.

If possible, keep the millis() output in a unsigned long and only use data types with less bits when you're absolutely sure you will not lose bits.

There's more information on explicit casts in C here: https://stackoverflow.com/a/13652624/1544337


When you want to do stuff with millis() just remember to initialize your variable with type "uint32_t"

So do something like "uint32_t last_millis" where you will store the output of the "millis()" function.

Otherwise like the others said, it would overflow when going past 31,000 which will happen pretty quickly.