# Countdown timer 16x2 lcd

**URL:** <https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228>\
**Category:** Programming\
**Created:** [October 6, 2021, 9:00am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228 "2021-10-06T09:00:20Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2021, 7:03pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/21 "2021-10-07T19:03:09Z")

</div>

> [@blh64](#):
>
> Not the proper way to track elapsed time since millis() rolls over.

yes! (was nagging at me lately)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 7, 2021, 9:05pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/22 "2021-10-07T21:05:26Z")

</div>

> [@gcjr](#):
>
> (minsDelay \* 60 \* 1000)

Warning: On a basic Arduino like an UNO, Nano, or Mega this is likely to result in an integer overflow. The numbers '60' and '1000' are 'int' literals. If 'minsDelay' is an 'int' then the math will all be done in 'int' which can't hold 60 \* 1000, let alone 45 \* 60 \* 1000.

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 8, 2021, 7:27am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/23 "2021-10-08T07:27:59Z")

</div>

thanks, i will try

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 11, 2021, 7:20am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/25 "2021-10-11T07:20:44Z")

</div>

I got this

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned long currentMillis = millis();
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(16,2);
lcd.clear();

Serial.begin(9600);
pinMode (2,OUTPUT);
pinMode (3,OUTPUT);
pinMode (4,OUTPUT);

pinMode (8,OUTPUT);
}

void loop()
{
  

  // Count down once per second
  if (currentMillis - PreviousMillis == 1000)
  {
    PreviousMillis += 1000;

    if (SecondsRemaining > 0)
    {
      SecondsRemaining--;
      lcd.clear();
      lcd.println(SecondsRemaining);
      if (SecondsRemaining == 0)
      {lcd.write("beep");}
    }
  }
int keyVal = analogRead(A0);
Serial.println(keyVal);
  // Check for input:
  if (keyVal == 341)
    {
    SecondsRemaining = 1800; // 30 minutes
    PreviousMillis = currentMillis; // Restart the timer
   }
   else if (keyVal == 1001)
    {
    SecondsRemaining = 900; // 15 minutes
    PreviousMillis = currentMillis; // Restart the timer
   }
   else if (keyVal == 10)
    {
    SecondsRemaining = 2700; // 45 minutes
    PreviousMillis = currentMillis; // Restart the timer
   }
}

```

but I have no idea how to display the time on the LCD, any ideas?

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 11, 2021, 7:40am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/26 "2021-10-11T07:40:01Z")

</div>

I've tried lcd.write(SecondsRemaining); but then i get a weird symbol or something

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 11, 2021, 9:20am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/27 "2021-10-11T09:20:00Z")

</div>

have you tried `lcd.print()`?

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 11, 2021, 10:41am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/28 "2021-10-11T10:41:37Z")

</div>

thanks, that worked, but now it says 1800 on the lcd and it doesnt count down whatsoever

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 11, 2021, 10:44am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/29 "2021-10-11T10:44:23Z")

</div>

can't help w/o seeing some code ???

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 11, 2021, 10:50am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/30 "2021-10-11T10:50:29Z")

</div>

sorry  
here it is:

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned long currentMillis = millis();
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(16,2);
lcd.clear();

Serial.begin(9600);
pinMode (2,OUTPUT);
pinMode (3,OUTPUT);
pinMode (4,OUTPUT);

pinMode (8,OUTPUT);
}

void loop()
{
  

  // Count down once per second
  if (currentMillis - PreviousMillis == 1000)
  {
    PreviousMillis += 1000;

    if (SecondsRemaining > 0)
    {
      SecondsRemaining--;
      lcd.clear();
      lcd.println(SecondsRemaining);
      if (SecondsRemaining == 0)
      {lcd.write("beep");}
    }
  }
int keyVal = analogRead(A0);
Serial.println(keyVal);
  // Check for input:
  if (keyVal == 341)
    {
    SecondsRemaining = 1800; // 30 minutes
    lcd.print(SecondsRemaining);
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
   }
   else if (keyVal == 1001)
    {
    SecondsRemaining = 900; // 15 minutes
    PreviousMillis = currentMillis; // Restart the timer
   }
   else if (keyVal == 10)
    {
    SecondsRemaining = 2700; // 45 minutes
    PreviousMillis = currentMillis; // Restart the timer
   }
}

```

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 11, 2021, 11:54am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/31 "2021-10-11T11:54:05Z")

</div>

You never update currentMillis...

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;

LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();

  Serial.begin(9600);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);

  pinMode (8, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  // Count down once per second
  if (currentMillis - PreviousMillis >= 1000)
  {
    PreviousMillis += 1000;

    if (SecondsRemaining > 0)
    {
      SecondsRemaining--;
      lcd.clear();
      lcd.println(SecondsRemaining);
      if (SecondsRemaining == 0)
      {
        lcd.write("beep");
      }
    }
  }
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Check for input:
  if (keyVal == 341)
  {
    SecondsRemaining = 1800; // 30 minutes
    lcd.print(SecondsRemaining);
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 1001)
  {
    SecondsRemaining = 900; // 15 minutes
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 10)
  {
    SecondsRemaining = 2700; // 45 minutes
    PreviousMillis = currentMillis; // Restart the timer
  }
}

```

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 11, 2021, 12:14pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/32 "2021-10-11T12:14:01Z")

</div>

Thanks this was the answer, but I dont know if I need it in minutes for example (15:00) or in seconds but im gonna show this to my teacher and tell you

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 11, 2021, 1:59pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/33 "2021-10-11T13:59:30Z")

</div>

The key is to always track elapsed time in seconds or milliseconds and then convert it as needed for display purposes. It is far easier to keep track of all the math and then do something like

```cpp
unsigned long t = SecondsRemaining;
int hours = t / 3600; // integer division so rounds down
t = t % 3600;
int minutes = t / 60;
int seconds = t % 60;
// now display hours:minutes:seconds

```

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 14, 2021, 7:56am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/34 "2021-10-14T07:56:05Z")

</div>

I'm trying something like this

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned MinutesRemaining = 0;
unsigned long t = SecondsRemaining;
int hours = t % 3600; // integer division so rounds down
int minutes = t / 60;
int seconds = t % 60;
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);

}

void loop()
{
  unsigned long currentMillis = millis();
  // Count down once per second
  
  if (currentMillis - PreviousMillis >= 1000)
  {
    PreviousMillis += 1000;
    MinutesRemaining = SecondsRemaining % 60;
    if (SecondsRemaining > 0)
    {
      SecondsRemaining--;
      lcd.clear();
      lcd.println(SecondsRemaining);
     
      if (MinutesRemaining == 0)
      {
        lcd.clear();
        lcd.write("beep");
        tone(5, 523, 2000);
        
      }
    }
    if (SecondsRemaining = 0)
  {
   MinutesRemaining - 1;
    SecondsRemaining == 59;
   
  }
  }
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Check for input:
  if (keyVal == 341)
  {
    MinutesRemaining = 44;
    SecondsRemaining = 59; // 30 minutes
   lcd.print(MinutesRemaining);
    lcd.print(":");
    lcd.print(SecondsRemaining);
   delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 1001)
  {
    SecondsRemaining = 900; // 15 minutes
    lcd.print(SecondsRemaining);
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 10)
  {
    SecondsRemaining = 2700; // 45 minutes
    lcd.print(SecondsRemaining);
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
}

```

Am I on the right path  
(im trying to make it in (minutes:seconds) )

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 14, 2021, 11:46am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/35 "2021-10-14T11:46:56Z")

</div>

sometimes the code is over-complicated to see how simple it is. and there were various problems

- the compiler reported several warnings

should be "=="  
` if (SecondsRemaining = 0)`

should be "MinutesRemaining --;"  
` MinutesRemaining - 1;`

should be "="  
` SecondsRemaining == 59;`

- whenever the pot value changes -- and it will dither between values -- SecondsRemaining gets reset.

need to check for a change in value beyond some threshold

```cpp
    static int keyValLst;
    int keyVal = analogRead (A0);

    if (abs (keyValLst - keyVal) > 2) {

```

- and the comparison to a specific value when checking for a change beyond some threshold needs a threshold

```cpp
        if (abs(keyVal - 341) < 3)

```

- PreviousMillis should be set to currentMillis when it expires, after

```cpp
    if (currentMillis - PreviousMillis >= 1000) {
        PreviousMillis = currentMillis;

```

- i believe lcd.write() writes a single character. i think lcd.print() is what is needed

consider

```cpp
#undef MyHW
#ifdef MyHW
struct LiquidCrystal {
    LiquidCrystal (int a, int b, int c, int d, int e, int f, int g) { };

    void begin (int a, int b) { };
    void clear (void) { };
    void setCursor (int a, int b) { };

    void write (const char *s) { 
        Serial.print ("write -- ");
        Serial.println (s);
    };

    void print (int i) { Serial.print (i); };
    void print (const char *s) { Serial.print (s); };

    void println (int i) { Serial.println (i); };
    void println (const char *s) { Serial.println (s); };
};

#else
# include <LiquidCrystal.h>
#endif

unsigned long PreviousMillis = 0;

unsigned SecondsRemaining = 0;

LiquidCrystal lcd (6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight

char s [80];

// -----------------------------------------------------------------------------
void setup () {
    pinMode (backLight, OUTPUT);
    digitalWrite (backLight, HIGH);
    lcd.begin (16, 2);
    lcd.clear ();

    Serial.begin (9600);
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long currentMillis = millis ();

    // Count down once per second
    if (currentMillis - PreviousMillis >= 1000)
    {
        PreviousMillis = currentMillis;

        if (0 < SecondsRemaining) {
            SecondsRemaining--;

            sprintf (s, " %2d:%02d",
                SecondsRemaining / 60, SecondsRemaining % 60);
            lcd.println (s);
        }
    }

    static int keyValLst;
    int keyVal = analogRead (A0);

    if (abs (keyValLst - keyVal) > 2) {
        keyValLst = keyVal;

        if (abs(keyVal - 341) < 3)
            SecondsRemaining = 30 * 60;
        else if (abs(keyVal - 1001) < 3)
            SecondsRemaining = 15 * 60;
        else if (abs(keyVal - 10) < 3)
            SecondsRemaining = 45 * 60;

        sprintf (s, " key %4d, secs %4d",
            keyVal, SecondsRemaining);
        lcd.println (s);
    }
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 14, 2021, 5:02pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/36 "2021-10-14T17:02:25Z")

</div>

> [@gcjr](#):
>
> - i believe lcd.write() writes a single character. i think lcd.print() is what is needed

Depends on the arguments:  
`lcd.write(char);` Single character  
`lcd.write(char *);` Writes characters until it reaches a null.  
`lcd.write(char *, int);` Writes 'int' characters.

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 15, 2021, 7:35am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/37 "2021-10-15T07:35:14Z")

</div>

Thanks all for your answers, I wrote this myself and it works  
for anyone curious:

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned MinutesRemaining;
int t = SecondsRemaining;
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);

}

void loop()
{
  unsigned long currentMillis = millis();
  // Count down once per second
  
  if (currentMillis - PreviousMillis >= 1000)
  {
    PreviousMillis += 1000;
    if (SecondsRemaining > 0)
    {
      SecondsRemaining--;
    }else if (SecondsRemaining == 0)
    {
      MinutesRemaining -= 1;
      SecondsRemaining += 59;
    }
    lcd.clear();
    lcd.print(MinutesRemaining);
    lcd.print(":");
    if(SecondsRemaining < 10)
    {lcd.print("0");}
    lcd.print(SecondsRemaining);
    if (MinutesRemaining == 0)
    {
      lcd.clear();
      lcd.write("beep");
      tone(5, 523, 2000);
    }
  }
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Check for input:
  if (keyVal == 341)
  {
    MinutesRemaining = 30;
    SecondsRemaining = 00; // 30 minutes
   lcd.print(MinutesRemaining);
    lcd.print(":");
    lcd.print(SecondsRemaining);
   delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 1001)
  {
   MinutesRemaining = 15;
   SecondsRemaining = 0;
   delay(1000);
   PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 10)
  {
    MinutesRemaining = 45;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
}

```

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 15, 2021, 11:46am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/38 "2021-10-15T11:46:20Z")

</div>

Your code will beep when there are 59 seconds, 0 minutes left on your timer and continue to do so until you reset it again. Is that the behavior you want?

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 18, 2021, 7:09am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/39 "2021-10-18T07:09:13Z")

</div>

No, but I updated it again

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned MinutesRemaining;
int t = SecondsRemaining;
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);
lcd.clear();
lcd.write("Select your time");
}

void loop()
{
  unsigned long currentMillis = millis();
  // Count down once per second
  
  if (currentMillis - PreviousMillis >= 1000)
  {
    PreviousMillis += 1000;
    if (SecondsRemaining > 0)
    {
      lcd.clear();
      SecondsRemaining--;
    }else if (SecondsRemaining == 0)
    {
      MinutesRemaining -= 1;
      SecondsRemaining += 59;
    }
    lcd.clear();
    lcd.print(MinutesRemaining);
    lcd.print(":");
    if(SecondsRemaining < 10)
    {lcd.print("0");}
    lcd.print(SecondsRemaining);
    if (MinutesRemaining + SecondsRemaining == 0)
    {
      lcd.clear();
      lcd.write("beep");
      tone(5, 523, 2000);
    }
  }
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Check for input:
  if (keyVal == 544)
  {
    MinutesRemaining = 30;
    SecondsRemaining = 00; // 30 minutes
   lcd.print(MinutesRemaining);
    lcd.print(":");
    lcd.print(SecondsRemaining);
   delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 1023)
  {
   MinutesRemaining = 15;
   SecondsRemaining = 0;
   delay(1000);
   PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 784)
  {
    MinutesRemaining = 45;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
}

```

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 18, 2021, 1:49pm UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/40 "2021-10-18T13:49:20Z")

</div>

Rather than all that code dealing with SecondsRemaining and MinutesRemaining, you should just track SecondsRemaining as your one countdown variable and then convert it to MM:SS for display purposes. You will find it will make your code much more straight forward

---

<div class="post-metadata">

**Author:** ![sjoerd17](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sjoerd17](https://forum.arduino.cc/u/sjoerd17)\
**Post date:** [October 20, 2021, 8:12am UTC](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228/41 "2021-10-20T08:12:36Z")

</div>

I have this now

```cpp
#include <LiquidCrystal.h>

unsigned SecondsRemaining = 0;
unsigned long PreviousMillis = 0;
unsigned MinutesRemaining;
int t = SecondsRemaining;
LiquidCrystal lcd(6, 7, 8, 9, 10, 11, 12);
int backLight = 13; // pin 13 will control the backlight
void setup() {
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);
lcd.clear();
lcd.write("Select your time");
}

void loop()
{
  unsigned long currentMillis = millis();
  // Count down once per second
  
  if (currentMillis - PreviousMillis >= 1000)
  {
    PreviousMillis += 1000;
    if (SecondsRemaining > 0)
    {
      lcd.clear();
      SecondsRemaining--;
    }else if (SecondsRemaining == 0)
    {
      MinutesRemaining -= 1;
      SecondsRemaining += 59;
    }
    lcd.clear();
    lcd.print(MinutesRemaining);
    lcd.print(":");
    if(SecondsRemaining < 10)
    {lcd.print("0");}
    lcd.print(SecondsRemaining);
    if (MinutesRemaining + SecondsRemaining == 0)
    {
      lcd.clear();
      lcd.write("beep");
      tone(5, 523, 2000);
      delay(60000);
      tone(5, 400, 2000);
      
    }
  }
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Check for input:
  if (keyVal == 544)
  {
    MinutesRemaining = 30;
    SecondsRemaining = 00; // 30 minutes
   lcd.print(MinutesRemaining);
    lcd.print(":");
    lcd.print(SecondsRemaining);
   delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
  else if (keyVal == 1023)
  {
   MinutesRemaining = 15;
   SecondsRemaining = 0;
   delay(1000);
   PreviousMillis = currentMillis; // Restart the timer
  }

  else if (keyVal == 784)
  {
    MinutesRemaining = 45;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer 
      }
      else if (keyVal == 783)
  {
    MinutesRemaining = 45;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
    else if (keyVal == 782)
  {
    MinutesRemaining = 45;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
    } 
      else if (keyVal == 543)
  {
    MinutesRemaining = 30;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  }
    else if (keyVal == 545)
  {
    MinutesRemaining = 30;
    SecondsRemaining = 00;
    delay(1000);
    PreviousMillis = currentMillis; // Restart the timer
  } 
}

```

Every time it starts it says "Select your time" but then it clears out and gives the number "65535:60", I have looked through my code and I don't see any reason why it says that.  
anyone knows?

[Previous page](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228.md?page=1)

[Next page](https://forum.arduino.cc/t/countdown-timer-16x2-lcd/912228.md?page=3)
