How can I use millis() instead of delay() on a 2 minute timer

This code is used to display 2 minute timer on a LCD display but you can also print it on your
serial monitor.

Requirements for the 2 minute timer:

  1. It should only run once when the code is compiled not ever again

-I tried looking over "Blink without Delay" example and I understood it but I don't know how to apply in my case. Can someone please help?


/* 2 min Countdown timer using liquid crystal display */

// importing library to control display
#include <LiquidCrystal.h>

// importing time library
#include <Time.h>

#define SHUTDOWN_TIMEOUT 120000

unsigned long int previousTime = 0;
int start_time;

// initialize the library by associating with LCD interface pin
// with arduino pin number
const int rs = 11, en = 12, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
// set up LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
twominTimer();
}
// helper method to get 2min timer
int twominTimer() {
char cSec[3];

/* My ATTEMPT to use millis()
while(millis() <= SHUTDOWN_TIMEOUT) {
int minutes = millis() / 6000;
int seconds = millis() % 1000;
Serial.print(minutes);
Serial.print(':');
sprintf(cSec, "%02d", seconds);
Serial.println(cSec);

*/

for (int totalSeconds = 0; totalSeconds <= 120; totalSeconds++) {
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60

// Print on LCD display
lcd.setCursor(2,1);
lcd.print("Doom bot: ");
lcd.print(minutes);
lcd.print(':');
// lcd.print(seconds);
sprintf(cSec, "%02d", seconds);
lcd.print(cSec);

// print on serial monitor
Serial.print("Doom Boot ");
Serial.print(minutes);
Serial.print(':');
sprintf(cSec, "%02d", seconds);
Serial.println(cSec);
delay(1000); // note - not to use delay*
}

What you should try and do is use millis to work out when a second has passed. e.g.

if (currentTime - startTime > 1000)

When that happens you need to

  • subtract 1 from the seconds remaining.
  • calculate the value to display on the LCD based on the seconds remaining.
  • display the value.
  • if the seconds remaining is 0, then stop.

currentTime (updated every loop) and startTime (when the second started) are unsigned long, and get their values from millis().

Hello jay76
Take a view in this small event controlled timer sketch based on the BLINKWITHOUTDELAY example aka mother of all Arduino applications.
Test it - check it!

unsigned long stamp;
const unsigned long Duration = 5000;
int onOff;
void setup()
{
  Serial.begin(9600);
  stamp = millis();
  onOff = true;
  Serial.println(F("start timer"));
}
void loop()
{
  if (millis() - stamp >= Duration && onOff)
  {
    onOff = false;
    Serial.println(F("timer fired"));
  }
}

might as well display the time as it tics down

/* 2 min Countdown timer using liquid crystal display */

#include <LiquidCrystal.h>
LiquidCrystal lcd (11, 12, 5, 4, 3, 2);

unsigned long MsecPeriod = 1000;
unsigned long msecLst;
unsigned long msec;

int seconds;
char s [40];

// -------------------------------------
void disp ()
{
    sprintf (s, "Doom bot: %2d:%02d", seconds / 60, seconds % 60);

    lcd.setCursor (2,1);
    lcd.print (s);

    Serial.println (s);
}

// -------------------------------------
void loop ()
{
    msec = millis ();
    if (msec - msecLst >= MsecPeriod)  {
        msecLst = msec;
        
        if (seconds > 0)
            seconds--;
        disp ();
    }
}

// -------------------------------------
void setup ()
{
    Serial.begin (9600);
    lcd.begin (16, 2);

    msecLst = millis ();
    seconds = 120;
    disp ();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.