8

Copied from the Arduino reference - millis()

Tip: Note that the parameter for millis is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints.

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

Could someone clarify this statement and/or give some example?

migrated from electronics.stackexchange.com Mar 5 '15 at 21:30

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

8

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.

  • Just a simple question: Declaring "unsigned long fake_millis;" equals to "uint_32 fake_millis;" ? – user3060854 Mar 5 '15 at 22:53
  • Yes, those are equal on the arduino. The difference is unsigned long can change with different platforms (eg x86), uint32_t will always be 32 unsigned bits everywhere. – BrettAM Mar 6 '15 at 0:47
  • 1
    It should be noted that your second example (32767 + 1) yields undefined behavior, which is almost always a bad thing. Your other examples are documented behavior you can rely upon. – Edgar Bonet Mar 6 '15 at 9:00
6

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

  • Thanks for the edit. I removed the reference to uint32_t as it's something different. uint32_t guarantees that you have a 32-bit unsigned integer, unsigned long doesn't (although on Arduino it is). I also already mentioned that it's a 32-bit type. – user6971 Mar 5 '15 at 21:39
  • Then wouldn't you say its more correct to say it returns an uint32_t which happens to be an unsigned long on arduino? – BrettAM Mar 5 '15 at 21:41
  • @BrettAM according to the docs this function returns an unsigned long. – user6971 Mar 5 '15 at 21:44
  • Removed old comments, since the whole discussion makes little sense with the current version of the answer. Just keeping this for the record: if the conversion is to a signed integer (int time = millis() - 1000), the result is similar: the upper 16 bits are discarded. This time the C standard says the result is implementation-defined, and the behavior is specified in the gcc documentation on the implementation-defined behavior of integers (fourth bullet point). – Edgar Bonet Mar 6 '15 at 21:47
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.