Hello, I'm building a timer for something, it has an input of seconds and displays time in hh:mm.
For some reason the time goes wrong at the 10hr mark and I'm really confused as to why, any help would be appreciated.
long sec = 35999;
int Hour = sec / 3600;
Serial.print("Hour: "); Serial.println(Hour);
unsigned long val = Hour * 3600;
Serial.print("val: "); Serial.println(val);
long Minutes = sec - val;
Serial.print("Minutes: "); Serial.println(Minutes);
Minutes /= 60;
Serial.print("Minutes: "); Serial.println(Minutes);
int minLsd = Minutes % 10;
Serial.print("minLsd: "); Serial.println(minLsd);
int minMsd = Minutes / 10;
Serial.print("minMsd: "); Serial.println(minMsd);
Serial.print(Hour); Serial.print("h"); Serial.print(minMsd); Serial.println(minLsd);
sec = 36000;
Hour = sec / 3600;
Serial.print("Hour: "); Serial.println(Hour);
val = Hour * 3600;
Serial.print("val: "); Serial.println(val);
Minutes = sec - val;
Serial.print("Minutes: "); Serial.println(Minutes);
Minutes /= 60;
Serial.print("Minutes: "); Serial.println(Minutes);
minLsd = Minutes % 10;
Serial.print("minLsd: "); Serial.println(minLsd);
minMsd = Minutes / 10;
Serial.print("minMsd: "); Serial.println(minMsd);
Serial.print(Hour); Serial.print("h"); Serial.print(minMsd); Serial.println(minLsd);
Serial port output:
Hour: 9
val: 32400
Minutes: 3599
Minutes: 59
minLsd: 9
minMsd: 5
9h59
Hour: 10
val: 4294937760
Minutes: 65536
Minutes: 1092
minLsd: 2
minMsd: 109
10h1092
Apart form
void setup() {
Serial.begin(9600);
Above it, and
}
void loop() {
// put your main code here, to run repeatedly:
}
There isn't anything else.
Consider dividing seconds by 60 to get minutes, before printing.
Run this:
char tBuf[20];
void setup() {
Serial.begin(38400);
long sec = 3599;
int minutes = (sec/60)%60;
int hours = sec/3600;
sprintf(tBuf,"%02d:%02d",hours,minutes);
Serial.println(tBuf);
sec = 3600;
minutes = (sec/60)%60;
hours = sec/3600;
sprintf(tBuf,"%02d:%02d",hours,minutes);
Serial.println(tBuf);
}
void loop() {
}
jaddion82052:
For some reason
This
val = Hour * 3600;
overflows the sixteen bit maths when Hour is 10.
Use an online regular calculator and evaluate
2^32 - 4294937760 - 65536
or
2^32 - 4294937760 - 2^16
to begin explaining the big number that got printed. I just posted that in the browser URL window and google calculate gives the answer.
Do the calculation using 3600 as an unsigned long:
val = Hour * 3600ul;
a7
1 Like
kolaha
June 21, 2024, 1:56am
7
void setup() {
Serial.begin(38400);
unsigned long sec = 3599;
pRint(sec);
sec = 3600;
pRint(sec);
}
void pRint(unsigned long Value){
static char tBuf[20];
byte minutes = (Value/60)%60;
byte hours = (Value/3600)%24;
sprintf(tBuf,"%02u:%02u:%02u",hours,minutes,Value%60);
Serial.println(tBuf);
}
void loop() {
}
kolaha:
static char tBuf[20];
byte minutes = (Value/60)%60;
byte hours = (Value/3600)%24;
sprintf(tBuf,"%02u:%02u:%02u",hours,minutes,Value%60);
Serial.println(tBuf);
That's perfect, thank you.
system
Closed
December 18, 2024, 8:28am
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.