Sum and casting suggest

Hi I'm a newbye in arduino and I need to know where the problem is.

I've a var:

byte seconds = 30;
unsigned long last_time;

then I have inside a loop a comparison IF like this:

if ( millis() - last_time > ((seconds * 1000) + 3000) ) {
...do something
}

execution inside IF never done. After some trying if i use 2000 instead 3000 it works. So I decided to use serial to print

Serial.println((seconds * 1000) + 3000);

and I see a value like: 4294934760. WHY?

with +2000 I see correct value: // 32000 WHY?

After some try I solved with a casting:

if ( millis() - last_time > ((unsigned long)(seconds * 1000) + 3000) ) {

my question is.. why with +2000 is ok, and +3000 is NOT ok?

Better solution to use a compare like mine? thanks

Note that a signed int can hold 32767 at the most

As to solving your problem, why use seconds at all ? Make all of the timing variables unsigned longs and use the value returned by millis() directly. If you need to display seconds then convert the unsigned long value when you need to display it

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

byte seconds = 30;

comparison with millis() must be in unsigned long

Hello @itajackass

@UKHeliBob 's answer #2 is what you need, do the calculation in unsigned long (AKA uint32_t) directly and you won't have the problem. However, to address the problem you are asking about I'll say that this catches me out and has had me recently spending hours trying to find out why a fairly complex calculation was giving the wrong results. For background I consider myself a reasonably good hobby programmer in C, not a professional.

The problem comes from how the compiler deals with numbers where the type is nor explicitly stated, for example if you just put 1000 * 50 what type does the compiler assign to 1000 and what type to 50? It's very easy to get this wrong and get a very different result to what you expect. There are discussions about this on this forum and it is covered in tutorials about C and C++, I will leave you to find those. However, when you have a problem like this you need to break the calculation into smaller chunks and print the result of each step, so, using your original question:

if ( millis() - last_time > ((seconds * 1000) + 3000) ) {
...do something
}

As an experiment instead of using if() try putting the calculation into Serial.print() and see what you get. Maybe have it print once per second so you can see what happens as millis() increases. Print both millis() and the result of the calculation. When you don't get what you expected break the calculation into parts and print each part, you should be able to see how the compiler is handling each number and you should see it's not what you expected.

thanks to all. now I undestand how to compiler "think"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.