Hello there,
I ran into this little problem using the Arduino IDE but not sure if it is only with that IDE or with AVR in general. It happens to be the Uno this time.
The code is for creating two simple clock timers. They count the seconds, minutes, hours, and days. The days might be limited for the data type used, but that is not a concern here. The main concern here is how the math is being evaluated for a particular calculation.
The code in question is:
unsigned long offset=0;
and then later the offending statements:
offset=1*86400; //set 1 day (in units of seconds) (the '1')
offset=offset+13*3600; //set 13 hours (in seconds)
offset=offset+53*60; //set 53 minutes (in seconds)
offset=offset*1000;//set clock 1 offset, converting to milliseconds
The above code, surprisingly, does not work. It sets the clock to some seeming random time but im sure it is not random, the compiler must not be interpreting the code correctly.
I broke the calculation down into separate lines as shown in order to track down the offending statement and also to avoid using too much stack space just in case it was using too much stack space (doubtful but tried anyway).
The offending line was found to be line 2, the calculation of the hours into units of seconds.
What works is:
offset=offset+1*3600;
offset=offset+2*3600;
offset=offset+3*3600;
all the way to:
offest=offset+9*3600;
but when i get to:
offset=offset+10*3600;
and above like:
offset=offset+13*3600; //the max for this would be 23 hours, here is shown 13 hours.
it totally screws up the time, including the days and minutes and seconds (totally incorrect when for every other one shown it works good).
For some reason as soon as i go to "10" hours, the compiler cant interpret this statement correctly.
What else works but seems a little unnecessary is:
offset=offset+10UL*3600UL; //UL stands for "type unsigned long"
So in other words, without the UL it wont set the time correctly, but with the UL's it works fine.
The other statements work fine without the UL.
Any ideas?
I ran into this little problem using the Arduino IDE but not sure if it is only with that IDE or with AVR in general. It happens to be the Uno this time.
The code is for creating two simple clock timers. They count the seconds, minutes, hours, and days. The days might be limited for the data type used, but that is not a concern here. The main concern here is how the math is being evaluated for a particular calculation.
The code in question is:
unsigned long offset=0;
and then later the offending statements:
offset=1*86400; //set 1 day (in units of seconds) (the '1')
offset=offset+13*3600; //set 13 hours (in seconds)
offset=offset+53*60; //set 53 minutes (in seconds)
offset=offset*1000;//set clock 1 offset, converting to milliseconds
The above code, surprisingly, does not work. It sets the clock to some seeming random time but im sure it is not random, the compiler must not be interpreting the code correctly.
I broke the calculation down into separate lines as shown in order to track down the offending statement and also to avoid using too much stack space just in case it was using too much stack space (doubtful but tried anyway).
The offending line was found to be line 2, the calculation of the hours into units of seconds.
What works is:
offset=offset+1*3600;
offset=offset+2*3600;
offset=offset+3*3600;
all the way to:
offest=offset+9*3600;
but when i get to:
offset=offset+10*3600;
and above like:
offset=offset+13*3600; //the max for this would be 23 hours, here is shown 13 hours.
it totally screws up the time, including the days and minutes and seconds (totally incorrect when for every other one shown it works good).
For some reason as soon as i go to "10" hours, the compiler cant interpret this statement correctly.
What else works but seems a little unnecessary is:
offset=offset+10UL*3600UL; //UL stands for "type unsigned long"
So in other words, without the UL it wont set the time correctly, but with the UL's it works fine.
The other statements work fine without the UL.
Any ideas?