how to display HH:MM:SS on LCD

hey everyone, i’m doing a countdown on my 16x2 LCD which is working fine but its displaying in terms of seconds.
how can i display it as HH:MM:SS since i’ll be countdown from 6 hours

here is my code

#include <LiquidCrystal.h>

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here

void setup()
  {
  lcd.begin(16, 2);                          // put your LCD parameters here
  lcd.print("Next Feeding: ");
  delay (2000);
  }

void loop()
  {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(21600000-millis()/1000);
  }

do help! thanks :smiley:

#include <LiquidCrystal.h>

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here
int hours, minutes, seconds;
long counter;
void setup()
{
  lcd.begin(16, 2);                          // put your LCD parameters here
  lcd.print("Next Feeding: ");
  delay (2000);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  // print the number of seconds since reset:
  counter = (21600000 - millis()) / 1000;
  hours = counter / 3600;
  counter -= (hours * 3600);
  minutes = counter / 60;
  counter -= (minutes * 60);
  seconds = counter;
  lcd.print(hours);
  lcd.print(":");
  if (minutes < 10) {
    lcd.print("0");
    lcd.print(minutes);
  }
  else
    lcd.print(minutes);
  lcd.print(":");
  if (seconds < 10) {
    lcd.print("0");
    lcd.print(seconds);
  }
  else
    lcd.print(seconds);
}

Untested, but something like that might work.

Happy new year,

Graham

PS: That will flicker quite badly due to constant re-draw of the display. But you can play with it from there?

ghlawrence2000:

#include <LiquidCrystal.h>

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here
int hours, minutes, seconds;
long counter;
void setup()
{
  lcd.begin(16, 2);                          // put your LCD parameters here
  lcd.print("Next Feeding: ");
  delay (2000);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  // print the number of seconds since reset:
  counter = (21600000 - millis()) / 1000;
  hours = counter / 3600;
  counter -= (hours * 3600);
  minutes = counter / 60;
  counter -= (minutes * 60);
  seconds = counter;
  lcd.print(hours);
  lcd.print(":");
  if (minutes < 10) {
    lcd.print(“0”);
    lcd.print(minutes);
  }
  else
    lcd.print(minutes);
  lcd.print(":");
  if (seconds < 10) {
    lcd.print(“0”);
    lcd.print(seconds);
  }
  else
    lcd.print(seconds);
}




Untested, but something like that might work.

Happy new year,

Graham

PS: That will flicker quite badly due to constant re-draw of the display. But you can play with it from there?

thanks so much! i’ve tried it using 20s but as soon as its finished, it starts to 1194 hrs. how can this be?
is it possible to repeat the task for several times with 6 hours interval?

#include <LiquidCrystal.h>

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here
int hours, minutes, seconds;
long counter, mytime, interval = 2160000;
void setup()
{
  lcd.begin(16, 2);                          // put your LCD parameters here
  lcd.print("Next Feeding: ");
  delay (2000);
  mytime = millis() + interval;
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  // print the number of seconds since reset:
  if (millis() > mytime)
    mytime = millis() + interval;
  counter = (mytime - millis()) / 1000;
  hours = counter / 3600;
  counter -= (hours * 3600);
  minutes = counter / 60;
  counter -= (minutes * 60);
  seconds = counter;
  lcd.print(hours);
  lcd.print(":");
  if (minutes < 10) {
    lcd.print("0");
    lcd.print(minutes);
  }
  else
    lcd.print(minutes);
  lcd.print(":");
  if (seconds < 10) {
    lcd.print("0");
    lcd.print(seconds);
  }
  else
    lcd.print(seconds);
}

Something like that will work for a while, but beware that millis() will eventually reset to zero… At which point you or your hens will go hungry :wink:

Regards,

Graham

i dont quite understand the mytime function :confused:

If you want a repeating countdown, you need a counter that is independent of millis().

At first run, in setup() mytime=now+interval(6 hours/ 20 seconds etc);

When loop() first runs mytime is sometime in the future, but when millis() becomes greater than mytime, your interval has passed, so you need to reset your future time..... which restarts the count.

Not the best explanation, but I hope it helps....

Regards,

Graham

#include <LiquidCrystal.h>

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here
int hours, minutes, seconds, dispsec = 61;
long counter, mytime, interval = 20000;
void setup()
{
  Serial.begin(115200);
  lcd.begin(16, 2);                          // put your LCD parameters here
  lcd.print("Next Feeding: ");
  delay (2000);
  mytime = millis() + interval;
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(4, 1);
  // print the number of seconds since reset:
  if (millis() > mytime)
    mytime = millis() + interval;
  counter = (mytime - millis()) / 1000;
  hours = counter / 3600;
  counter -= (hours * 3600);
  minutes = counter / 60;
  counter -= (minutes * 60);
  seconds = counter;
  if (seconds != dispsec)
  {
    //Serial.print(hours);
    //Serial.print(":");
    lcd.print(hours);
    lcd.print(":");
    if (minutes < 10) {
      //Serial.print("0");
      //Serial.print(minutes);
      lcd.print("0");
      lcd.print(minutes);
    }
    else
      //Serial.print(minutes);
      lcd.print(minutes);
    //Serial.print(":");
    lcd.print(":");
    if (seconds < 10) {
      //Serial.print("0");
      //Serial.print(seconds);
      lcd.print("0");
      lcd.print(seconds);
    }
    else
      //Serial.print(seconds);
      lcd.print(seconds);
      //Serial.println();
    dispsec = seconds;
  }
}

That’s my final effort. No more display flicker…

Best wishes.

Graham

thank u so much for your help! it means a lot :smiley:

happy new year (although its already 2nd January here)

Best Wishes.