Showing the progress of a delay or millis() on LCD

How do from England,

I've been banging my head against a wall trying to understand timers so I can continue with a project.

After scanning google and the forum for 2 days I think its time to ask for help.

I've cracked relays, lcd's and using buttons throughout but this has me stumped.

I know its been covered alot. I have tried the countdown scripts and I think I understand them.

What I don't understand is how to print a live countdown to the LCD, then continue the rest of the function after that time.

The user has no interaction, the timings would be preset. I intend doing around 20 custom timings throughout my whole sketch.

For example, how would I put say a 30 second countdown in the below to show on screen/represent the 30sec delay in the code. Then continue the rest of the function after the 30 seconds?

void fillTankDev() {
  digitalWrite(Relay_2, RELAY_ON);// set the Relay ON (Dev Valve)
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON (The Pump)
  lcd.clear();
  lcd.print("Pump On");
  lcd.setCursor ( 0, 1 );       
  lcd.print("Filling - Dev");
  delay(30000);              // wait for tank to fill
  digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF  (Dev Valve)
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF  (The Pump)
  lcd.clear();
  lcd.print("Pump Off");
}

Any directions to help would be welcomed. I'm keen to learn so I don't expect things done for me.

Any example script or help page would be fine!. Would there be a way to use the Time.h library, declare the 20 or so preset times at the top then just call/print them when desired?

I dont mind using delay or millis, the whole process I'm using it for is based on a linear flow of relays opening and closing in order so doing other things at the same time isn't an issue (unless this would stop a timer working?)

how would I put say a 30 second countdown in the below to show on screen/represent the 30sec delay in the code. Then continue the rest of the function after the 30 seconds?

If you really don't mind the program doing nothing for 30 seconds then replace the delay(30000); with a for loop of 30 steps each with a delay(1000); and an output message containing the for loop variable.

for (int count = 30; count > 0; count--)
{
  Serial.println(count);
  delay(1000);
}

Great Ill try that now :slight_smile:

Plan B was to try and use some of this code from another example.

#include <LiquidCrystal.h>          // lib. for LCD
#include <Time.h>

time_t elapsedTime;

//to restart the time counter to Zero:  setTime(0);

LiquidCrystal lcd(7,8,9,10,11,12);  // pins connected to LCD

void setup()
{
 lcd.begin(20,4);                   // open the LCD
}
void loop()
{
 elapsedTime = now();
 lcd.setCursor(0,2);                // sets cursor to 3rd line
 lcd.print("Hour ");
 lcd.print(hour(elapsedTime));
 lcd.print("   ");
 lcd.setCursor (0,3);               // sets cursor to 4th line
 lcd.print("DAYS ");             // elapsed days
 lcd.print(elapsedTime / SECS_PER_DAY);
 lcd.setCursor (0,1);               // sets cursor to 2nd line
 lcd.print(minute(elapsedTime));
 lcd.print(" min ");
 lcd.setCursor(0,0);               // sets cursor to 1st line
 lcd.print(second(elapsedTime));
 lcd.print(" SEC ");
}

Plan B was to try and use some of this code from another example.

Don't complicate things if you don't need to. If delay(30000); does what you want then you don't need to use millis() which would need a complete rewrite of your program and have no benefit, although you might learn something.

Thanks for the quick replies.

I think I will try and learn Millis anyway but for other projects. This ones quite basic so as you mentioned would overcomplicate issues.

Ive changed it for lcd, here for 10 seconds

  for (int count = 10; count > 0; count--)
{
  lcd.clear();
  lcd.setCursor ( 0, 1 ); 
  lcd.print (count);
  delay(1000);
}

Is there a way to easily display in Mins/Seconds as well? and print text on the same line as the countdown? Tried lcd.print ("Time left"count); and lcd.print ("TimeLeft")(count); without luck.

Im used to powershell scripting and its a bit 'fisherprice' compared to C stuff :slight_smile:

Use two prints, one for the TimeLeft and one for the count.

sterretje:
Use two prints, one for the TimeLeft and one for the count.

Many thanks, sorted.

Could I change this int count to down min/secs or are they limited to seconds only?

Is there a way to easily display in Mins/Seconds as well?

What do you want to display in mins/seconds ? Do you have the current time being got from somewhere in your program ?

Could I change this int count to down min/secs or are they limited to seconds only?

You are counting seconds only, but, hey, minutes are made of seconds, you know.

If there are 914 seconds left, can you figure out how many minutes that involves? How many seconds? The / and % operators could be useful.

For example say 3minutes.

How do I show 180seconds as min/seconds format?

I'm guessing some kind of if seconds=>59 print 1min or something?

It would be the value taken shown above in the 30 second example. But this time 3mins.

Use divide by 60 and modulo 60.

I've never understood how people can have so much trouble with time on a computer. It's really the one thing that you need in programming that you're almost guaranteed to already know how to do. You deal with time every single day but for some reason as soon as codenis involved people glass over and forget everything they've known since they were little kids.

Don't try to make it hard. It's not esoteric. It's just regular time like you've always dealt with. If I told you it was 180 seconds you could tell me in a second how many minutes it was. Why can't you think of doing that exact same math in code?

Delta_G:
I've never understood how people can have so much trouble with time on a computer. It's really the one thing that you need in programming that you're almost guaranteed to already know how to do. You deal with time every single day but for some reason as soon as codenis involved people glass over and forget everything they've known since they were little kids.

To be fair neither have I, including myself. I work in IT, deal with scripts, minor coding every other day but only really powershell,wmi,vbs etc. None really ever deal with timings except the odd pause.

Learning curve for me, although I have managed to sort this one using the advice above and some common sense which I generally lack lol.

  for (int count = 90; count > 0; count--)
{
  lcd.clear();
  lcd.setCursor ( 0, 1 ); 
  lcd.print ("Time:"); 
  lcd.print (count / 60);
  lcd.print (":");
  lcd.print (count % 60);
  delay(1000);
}

So out of curiosity as I am new to simpletimer... and new to arduino so don't take my advice as gospel :slight_smile:

Could you use a simpletimer like this? This code is in no way complete, just a concept idea.

// define variable

long countDownRemain = 0; // remaing time in timer

// inserted this in program when you want to start the timer


timer.deleteTimer(countDownRemain); // remove timer in case its already running, assume you could use disable.timer instead
countDownRemain = timer.setTimeout ( 5000, dummytimer); // begin the countdown from 5000 milliseconds to zero and put remaining time in the countDownRemain variable, then run dummytimer() at the end of countdown
displaytimer = timer.setInterval(1000, displayTimerFunction); // update display with remaining time every # milliseconds which is currently 1000

dummytimer()
{
insert what you want to do when the timer finishes in here
}

displayTimerFunction()
{
insert code to display countDownRemain on display or however you are displaying it.
}