Countdown timer (millis) on serial monitor

Hello,

For a project I have to built a countdown timer using the serial printer. I need to use the function “millis” and the countdown have to be from 10 seconds to zero with using “delay”. It has to countdown in seconds. The countdowntimer also has to check if the point of “zero” has been reached and then has to stop or print zereo as I wrote in the code.
I have written a code but it doesn’t work. It countsdown but when reaching "zero"it doesn’t stop. I don’t know what is wrong. Can anybody help me? It is important to use the millis, delay and an “if” to check or zero has been reached.

Test_program.ino (474 Bytes)

Please post your code, in code tags, of course

millis is an unsigned long and so therefore is millis()-60000. It can never be less than zero, hence why your if gives bizarre results.

Also, as posted, your code has at least one unicode character in it. Even with that fixed, it does not compile.

@joery81

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

I don’t think I understand what you mean. Can you tell me what I have to do with the code so it starts counting down and at 0 it stops. What happens now is it will go on, not by going -1,-2 and so on but it starts from they highest countable numbre for the arduino uno. I wrote the code below. I think you mean the IF situation has to be changed, but I don’t know how…

/* Countdown timer

*/

void setup() {
Serial.begin(9600);
}
void loop() {
// Check if 60 seconds have elapsed

if
(millis () – 60000 >=0)

{
// If 60 seconds have passed print “0”
Serial.print (“0”);

} else {

// If there did not pass 60 seconds print the countdown timer to 0
Serial.print (" there are “);
Serial.print (“((60000-millis())/1000) “)
Serial.println (“to go ");
delay (1000);
}
}

@joery81

Please use code tags as mentioned in the links I gave you.

Bob.

That still doesn't compile - how are you testing this?

Also, turn up the warnings to the max in the IDE preferences - it tells you what is wrong with your if.

It makes no sense to do a countdown with millis() but also throw delay() in there.

When you have problems with if statements it is often instructive to print the value being tested just before the test to see whether the value is what you expect it to be

Way to complex. Just use an integer counter and a delay.

@Bob, I hop I got the code right this time

/* Countdown timer
  
*/

void setup() {
   Serial.begin(9600);
}
void loop() {
// Check if 60 seconds have elapsed

  if 
(millis () – 60000 >=0)

    {
      // If 60 seconds have passed print “0”
    Serial.print ("0");
    
  } else {
  
// If there did not pass 60 seconds print the countdown timer to 0
Serial.print (" there are “);
Serial.print (“((60000-millis())/1000) “) 
Serial.println (“to go ");
   delay (1000);  
  }
}

The problem is I have to use millis and delay. I searched google the last days for help but I can only find countdown timers with a LCD or other timers where there is not described how to let the countdown stop. It has to be like 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 (and maybe 0 going on or the timer has to stop that is also a possibillity.

joery81:
The problem is I have to use millis and delay.

Why?

joery81:
The problem is I have to use millis and delay. I searched google the last days for help but I can only find countdown timers with a LCD or other timers where there is not described how to let the countdown stop. It has to be like 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 (and maybe 0 going on or the timer has to stop that is also a possibillity.

No you do not have to use millis(). If you decrement an integer every second then when it gets to 0 then print a 0 and restart the timer.

ToddL1962:
No you do not have to use millis(). If you decrement an integer every second then when it gets to 0 then print a 0 and restart the timer.

I have to use millis, delay and an if function for this program. It is not because I think that it is the best way to it, but this is what the assignment is. Built a countdown timer from 10 seconds counting by 1 second and use the function millis, delay and if. The two single codes work, countdown and write 0. But with the if I want to check if there have already past 10 seconds so the countdown will stop.

joery81:
I have to use millis, delay and an if function… this is what the assignment is.

Well that’s a very unrealistic thing to get you to do. And in my opinion it’s a hella waste of a folks’ time in the forum to help you do stuff a “bad” way.

vilmabergmann:
Well that’s a very unrealistic thing to get you to do. And in my opinion it’s a hella waste of a folks’ time in the forum to help you do stuff a “bad” way.

I think it is because of it is in the beginning of the course. This is the first lesson with the serial monitor. Later on we will learn newer things, but this for a start. We had already if/else and delay. The serial monitor and the millis are new.

Do you now what I am doing wrong? I have to use the described functions in the code.

If you have to use delay(), use it once, like “delay (1);” in setup(), then forget you ever heard of it.

joery81:
Do you now what I am doing wrong?

Haven't had a proper look sorry, actually just browsing while I'm on a google meet work call.....

If you MUST use millis() then there are multiple problems with this if statement:

  if (millis () - 60000 >=0)
  1. millis() returns unsigned long. It can never be negative.
  2. millis() continues to count up until it overflows in 49.7 days (value of 4,294,967,295).

For millis() you should start with the Blink Without Delay example. That example is so famous, some shorten it to "BWD".

Do you see the "if (currentMillis - previousMillis >= interval) {" ?
That is important. That's how to use millis().

You can keep the code for blinking the led in the sketch. Can you add a 'int' variable that increments every second ? And print that to the serial monitor ?

Once you can do that, then you can perhaps decrement a value and stop at zero.