OK, here is the deal, want to make countdown timer, this code blow will increment time:
seconds++;
if(seconds==60)
{
seconds=0; //let the counter of seconds begin once more
minutes++;
if(minutes==60)
{
minutes=0; //let the counter of minute begin once more
hours++;
}
}
But I'm having trouble to decrease timer, like if i set start time 00:02:03, it will decrease till 00:00:00.
My code glitches, in transitions, is this right:
seconds--;
if(seconds==00)
{
seconds=60; //let the counter of seconds begin once more
minutes--;
if(minutes==00)
{
minutes=60; //let the counter of minute begin once more
hours--;
}
}
vinicaog:
But I'm having trouble to decrease timer, like if i set start time 00:02:03, it will decrease till 00:00:00.
Isn't that what you want it to do?
My code glitches, in transitions, is this right:
segundos--;
if(seconds==00)
{
seconds=60; //let the counter of seconds begin once more
minutes--;
if(minutes==00)
{
minutes=60; //let the counter of minute begin once more
hours--;
}
}
Well, the Arduino is very capable, but it won't automatically translate between Spanish and English. segundos <--> seconds?
Here is my method, which is crazy but works (unless I've made a stupid mistake somewhere):
// Code for counting UP:
seconds++;
if(seconds>=60)
{
seconds-=60; //let the counter of seconds begin once more
minutes++;
}
if(minutes>=60)
{
minutes-=60; //let the counter of minute begin once more
hours++;
}
// Code for counting DOWN:
seconds+=59;
minutes+=59;
if(seconds>=60)
{
seconds-=60; //let the counter of seconds begin once more
minutes++;
}
if(minutes>=60)
{
minutes-=60; //let the counter of minute begin once more
hours++;
}
hours--;
The concept is from very old calculating machines which could not subtract. They could only add, so if you wanted to subtract, you had to convert the subtraction into an addition, as I have done here.
Example calculation:
hr min sec
1 0 1 Counting down: what comes next?
+59 +59 Take care of minutes and seconds
1 59 60 Too many seconds
+ 1 -60 Carry seconds into minutes
1 60 0 Too many minutes
+1 -60 Carry minutes into hours
2 0 0
-1 Remove the extra hour
1 0 0 Result
1 0 0 ... wait for the next second ...
1 0 0 Then what happens?
+59 +59 Take care of minutes and seconds
1 59 59
-1 Remove extra hour
0 59 59 Result
0 59 59 ... wait for the next second ...
0 59 59 And then what?
+59 +59 Take care of minutes and seconds
0 118 118 Too many seconds!
+1 -60 Carry seconds into minutes
0 119 58 Too many minutes!
+1 -60 Carry minutes into hours
1 59 58
-1 Remove extra hour
0 59 58 Result
Yeah but I've never seen a countdown clock that shows you :60. You usually see zero and then 59. So it should be something like if seconds == -1 then seconds =59. You don't want to do it at zero or the countdown will go 5... 4... 3... 2... 1... 60... 59... 58 and that's not what people normally want to see. They normally want 3... 2... 1... 0... 59... 58... 57...
Delta_G:
Yeah but I've never seen a countdown clock that shows you :60. You usually see zero and then 59. So it should be something like if seconds == -1 then seconds =59.
I don't know. What datatype is he using? Will it correctly handle the negative number?
My approach has a weakness of its own. If seconds and minutes are of datatype char, then they need to be kept in the range -127 to 127. This might be all right (because 59 + 59 + 1 < 127), but a timer set to "90 seconds" (as opposed to "1 minute 30 seconds") will defeat this (as 90 + 59 > 127). However, I think it very unlikely that vinicaog is using char for the relevant variables.
The real question is why bother to keep up with minutes and seconds in separate variables and add all this layer of confusion in the first place. If seconds is what you need, then just keep up with that. I don't care if you suck at math, the processor is pretty good at it. Then if you want to see minutes and hours and stuff when you display it, then do that when you display it. That's just for the humans.
unsigned int countdown = 183; //3 minutes and 3 seconds as seconds
boolean countdownEnded = false;
void setup()
{
Serial.begin(9600);
}
void loop(){
if (countdown == 0){
Serial.println("Countdown Ended");
countdownEnded = true;
}
if (!countdownEnded){
printTime(countdown);
countdown--;
delay(1000); // This code has nothing else to do.
}
}
void printTime(unsigned int time){
Serial.print(time /60); // print the number of minutes
Serial.print(':');
int seconds = time % 60;
if (seconds < 10){
Serial.print('0');
}
Serial.print(time % 60);
Serial.println();
}
unsigned long timeAdd (unsigned long x, unsigned long y) {
// no sanity checking of input
// format is hhmmssff with ff being decimal fractions of a second
// "out of range" results are e.g. A0000000 for 100 hours
unsigned long binsum = x + y;
unsigned long carry = ((binsum + 0x06A6A666) ^ x ^ y) & 0x11111110;
return (binsum + ((carry - (carry>>4)) & 0x06A6A666));
}
unsigned long timeSub (unsigned long x, unsigned long y) {
// no sanity checking of input
// format is hhmmssff with ff being decimal fractions of a second
// "negative" results are e.g. F9595999 for -0.01 second
unsigned long bindiff = x - y;
unsigned long borrow = (bindiff ^ x ^ y) & 0x11111110;
return (bindiff - ((borrow - (borrow>>4)) & 0x06A6A666) );
}
/*
Usage example:
2 h 58 min 30.98 s --> 0x02583098
0 h 1 min 44.06 s --> 0x00014406
Addition:
timeAdd(0x02583098, 0x00014406) gives 0x03001504
which means 3 h 0 min 15.04 s
Subtraction:
timeSub(0x02583098, 0x00014406) gives 0x02564692
which means 2 h 56 min 46.92 s
*/