-1

I am trying to create a timer that outputs the time remaining in minutes. I am using an Adafruit Circuit Playground Express. For example, if the timer is 5 minutes, then every minute it should output the time remaining, until the time ends.

5 minutes timer started.

Delay: 5 minutes

Delay: 4 minutes

Delay: 3 minutes

Delay: 2 minutes

Delay: 1 minute

This is my attempt to implement this. I would like it to start by displaying "Delay: 5 minutes"; however, this is not output until 1 minute has passed.

What am I doing incorrectly?

const long interval = 1 * 60 * 1000UL;  
unsigned long previousMillis = 0;           
int minutes = 5 ;

 unsigned long currentMillis = millis();

  while (minutes >= 0){
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    Serial.print(F("Delay: "));
    Serial.print(minutes);
    Serial.println(F(" minute(s)"));
    minutes = minutes - 1;
  }
  }
  • 2
    it does not output the correct remaining time is not a useful description of the output .... it would be much more useful if you say what the output actually is – jsotola Aug 23 '19 at 23:16
  • Also, h ow is previousMillis defined and initialized? – Michel Keijzers Aug 23 '19 at 23:18
  • 1
    Same question was asked and is being discussed here forum.arduino.cc/index.php?topic=632900.msg4285177#msg4285177 – CrossRoads Aug 24 '19 at 3:10
  • why are you posting your question here? ... the Arduino forum is giving you good suggestions – jsotola Aug 24 '19 at 6:32
-1

Try this: (I don't have an Arduino here so i can't test it)

int minutes = 5;
int passedminutes = 0;
void setup() {
 Serial.begin(9600);
}
void loop() {
  while((millis() - minutes*60000) < 0)
  {
    if(millis()%60000 == 0)
    {
      Serial.print("Remaining Time: ");
      Serial.println(minutes-passedminutes);
      Serial.println();
      passedminutes++;
      delay(1);
    }
  }
}
|improve this answer|||||
  • millis()%60000 is fragile, because the millis() counter skips some values. Roughly one every 42 values. – Edgar Bonet Aug 26 '19 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.