Pages: [1]   Go Down
Author Topic: [SOLVED]Problem with unsigned long  (Read 560 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I have a problem with my arduino, calculating numbers.
I am trying to count the number of seconds passed since 00:00..
Code:
unsigned long secondsElapsed = ((hour() * 3600) + (minute() * 60) + second)
Serial.print(secondsElapsed);

if hour is between 0-8 it works, but if it's above that, secondsElapsed returns near the maximum value of an unsigned long, that is a 429496xxxx ish number..

I have almost tried anything, calculating just with numbers instead of using time, (hour * (60^2)), ((hour * 1800)*2), and I've tried with normal long, unsigned int, and so on.. But nothing seems to work, can anyone help me? smiley

Thanks!
« Last Edit: August 18, 2011, 01:33:40 pm by Lasse » Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Make those constants 'long', so the the expression becomes 'long' and avoid overflow:

Code:
unsigned long secondsElapsed = ((hour() * 3600L) + (minute() * 60L) + second);
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7175
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We're beating one another over these recent posts smiley

Just FYI, you should remember from your nature/physical science course that one day has 86,400 seconds, far less than what long int or unsigned long so something else was wrong. 8 hour is about 32767, max of signed int, which is what all numerical constants are treated as without specific type case.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, Anachrocomputer.. It helped smiley-wink

liudr: I know that the unsigned long, shouldn't have reached the limit.

Code:
unsigned long temp = 5 * 3600;
The above code worked, but.
Code:
unsigned long temp = 10 * 3600;

didn't.

And neither of them is anywhere near the limit of a long, so I guess the compiler calculates it as a int.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48567
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so I guess the compiler calculates it as a int.
Correct. The compiler shoved a 10 into a 16 bit register, and 3600 into a 16 bit register, and multiplied the two registers, resulting in an overflow. The 16 bit value is then moved to a 32 bit memory location.

Had you said:
Code:
unsigned long temp = 10UL * 3600UL;
The compiler would have noticed that it needed to use 32 bit registers for the multiply operation, before moving the 32 bit result to a 32 bit memory location.
Logged

Pages: [1]   Go Up
Jump to: