Countdown with LCD and button

I have this piece of code, wondering if there is an easier way of making a countdown timer when button is pressed. ANY REPLY WOULD HELP EVEN A SUGGESTION :confused:

//Night light sketch
//Made by Alex (AyItsHusky) 2018
//Arduino coded!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int tenmin = 4;
int thirtymin = 3;
int led = 13;

void setup()
{
  pinMode(led,OUTPUT); // LED output
  pinMode(thirtymin,INPUT); // Button input
  pinMode(tenmin,INPUT); // Button input
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("Select a time.");
}

void loop()
{
  while(digitalRead(tenmin)==LOW);
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.print("Time left: 10min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 9min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 8min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 7min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 6min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 5min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 4min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 3min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 2min");
  delay(60000);
  lcd.clear();
  lcd.print("Time left: 1min");
  delay(60000);
  lcd.clear();
  lcd.print("Select a time.");
  digitalWrite(led,LOW);
  
  while(digitalRead(thirtymin)==LOW);
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Time selected:");
  lcd.setCursor(0,1);
  lcd.print("30 minutes");
  delay(1800000);
  digitalWrite(led,LOW);

}

A reply with a piece of code that could help me would be great! :slight_smile:

Have a look at Demonstration code for several things at the same time - Project Guidance - Arduino Forum or search for "delay using millis()" to get some ideas on how to do this.

Basically, you note the time when something starts and then inside loop() you calculated the elapsed time, and when it reaches some point, you do something.

In your case, when the button is pressed, you note the start time. You then calculate the elapsed time, and every minute (or second or whatever) you update your display. If the critical amount of time hasn't elapsed, you don't update the display.

Thank you. I will look into it! :slight_smile: