Making a timer

Hello, I am making a Timer with my LCD Display.
I do not have any input connected at this point in time.
I am trying to make my timer display count down but I do not know how I can do this.
I was hoping someone may assist me with the coding of this. I have not touched arduino in a while so I am a little dusty.

Set an int variable to the start value, say 10, and a second unsigned long variable to the current value of millis(). This is the start time.

In loop() check each time through whether the current value of millis() minus the start time is greater than or equal to 1000. If it is then decrement the counter variable, display its value and save the current time from millis(). Keep going until the counter reaches zero then do whatever you need at that point.

Where are you stuck ?

1 Like

UKHeliBob:
Set an int variable to the start value, say 10, and a second unsigned long variable to the current value of millis(). This is the start time.

In loop() check each time through whether the current value of millis() minus the start time is greater than or equal to 1000. If it is then decrement the counter variable, display its value and save the current time from millis(). Keep going until the counter reaches zero then do whatever you need at that point.

Where are you stuck ?

May you please give me an example of what this may look like?

It would run on the principles of Blink Without Delay.

You can run this on an UNO or most any Arduino as is, no jumpers or extra parts needed.
See how it works and how to make timers will be yours.

For a more full explanation, try going to the first Nick Gammon blog address at the bottom of this post, in my signature space.

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 --- and some more small fixes by GoForSmoke 2015
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval = 1000UL;           // interval at which to blink (milliseconds)

void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
 
  if (millis() - previousMillis > interval) 
  {
    // save the last time you blinked the LED
    previousMillis += interval;  

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

GoForSmoke:
It would run on the principles of Blink Without Delay.

You can run this on an UNO or most any Arduino as is, no jumpers or extra parts needed.
See how it works and how to make timers will be yours.

For a more full explanation, try going to the first Nick Gammon blog address at the bottom of this post, in my signature space.

/* Blink without Delay

Turns on and off a light emitting diode(LED) connected to a digital 
pin, without using the delay() function.  This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:

  • LED attached from pin 13 to ground.
  • Note: on most Arduinos, there is already an LED on the board
    that's attached to pin 13, so no hardware is needed for this example.

created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
--- and some more small fixes by GoForSmoke 2015

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;            // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval = 1000UL;          // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.

if (millis() - previousMillis > interval)
  {
    // save the last time you blinked the LED
    previousMillis += interval;

// if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }

// set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

How can I make this work on the LCD Display? Ik I display with lcd.print();

I have a basic idea to do this.

Create an 3 integers. (HH/MM/SS)

I put the time in those integers.

I start a loop displaying the time then taking 1 off Seconds. Once seconds reaches '00' I take 1 off of Minutes. Once minutes reached '00' I take 1 off Hours.

If Hours = 00 & minutes = 00 and seconds = 00 then I flash 'Timer Up!' and flash an Led until a button is pressed or arduino reset/turned off.

Problem is I dont know how to code that.

Have you made the LCD work using example code?
That should show you what needs to be #included and then set up (in setup) and how to print.
So you put the #include and the set up parts and where you want it to print, it should.

Expect to spend possibly hours or less to get it to print what you want, that is learning.

Please look more into the code or go to that first blog page in my signature space below if you need more explained, Nick did a beautiful job there as he does in all his pages.

We keep time in milliseconds and sometimes microseconds.

1 second is 1000 milliseconds. Put into Arduino scale, 1 milli is 16000 CPU operations.
1 minute is 60000 milliseconds.
1 hour is 3600000 milliseconds.

Why deal with separate counters for seconds, etc, and all the code to maintain and use those when you can simply use millisecond values? You would do it the hard way.

The Arduino millis() function returns unsigned long variables. Use unsigned long variables to deal with time (until you get past beginner) and you can time intervals as long as 49.710... DAYS.

thankyou.
I have gotten this to work. I am unsure on how to convert the time to be exact numbers.

For the sake of others, could you post your solution? And maybe what/where you made your breakthrough?

The posts in the forum are for reference for all as well as a channel to get things solved.

You could also go to the first post and edit the title with SOLVED: in front.

BowWhalley:
thankyou.
I have gotten this to work :slight_smile:

Well done.

When you post your code please use code tags (</> icon above the editor window). Expect to get comments about your code but don't be put off by anything that may be said, rather learn from it and use the lessons learned.

GoForSmoke:
Why deal with separate counters for seconds, etc, and all the code to maintain and use those when you can simply use millisecond values? You would do it the hard way.

With separate counters, the arithmetic is simpler. But yes, either way works.

If you have a sense of humor, you could even store the time as its string representation. This is actually surprisingly simple to code.

odometer:
With separate counters, the arithmetic is simpler. But yes, either way works.

If you have a sense of humor, you could even store the time as its string representation. This is actually surprisingly simple to code.

My code sense of humor changed since 1980.

None of that takes less code or runs as fast as using millis as intended.
For one thing, how do you get seconds? Would that be millis() 32-bit divided by 1000UL?

You do know that just because you can call a function with 1 short line that the function still runs?

I don't know what you do with time as strings but I use millis in debounce code that runs loop() up around 50 KHz.

Sorry for not linking the code before. Here it is. #include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 7, 6, 5, 4);int Dela - Pastebin.com

As you can see I have an nasa theme the clock as I am really into rockets :P. My clock now sounds a tone when time reaches 0/LiftOff!

I have not yet displayed the time with mm/ss or hh/mm/ss. If someone may help me out with that, would be great.

I apologise for the late response. I was searching around on getting the tone to work ect :P.

Thankyou for your help with this project.

Don't take this the wrong way (I did warn you !) but I have some comments on your code.

The main one is that you have not done yourself a favour in the long run by using delay() instead of using millis() as advised. Sure, delay() is easy but it does what it's name implies and delays execution of the program for the period specified. During the delay() no other code in your program can run.

You may say that this is no big deal, but what if I said that I wanted you to implement countdown abort or hold buttons that could happen at any time. As it stands you could only read the abort or hold buttons once a second whereas if you used millis() for the timing you could read them thousands of times per second.

Thankyou, I have searched arround on millis, I could not find a how exactly to use them. I am still using delay for now until I can fix that. If someone can show me where I can get it to display the time in hh:mm:ss that would be great

I have searched arround on millis, I could not find a how exactly to use them

Read reply #1 in conjunction with the BlinkWithoutDelay example and the idea will suddenly click.

Delta_G:
That part is really simple. If you want to break seconds down into hours, minutes, and seconds, all you need to do is math. If you divide the number of seconds by 3600, the integer result is the number of hours and the remainder is the number of odd seconds left. If you divide that remainder by 60, that gives you the minutes and the remainder of that is the seconds.

void displayTime(unsigned long timeInSeconds){

int hours = timeInSeconds / 3600;
 
  int remainder = timeInSeconds % 3600;
 
  int minutes = remainder / 60;
 
  int seconds = remainder % 60;
 
  Serial.print("Hours = ");
  Serial.println(hours);
  Serial.print("Minutes = ");
  Serial.println(minutes);
  Serial.print("Seconds = ");
  Serial.println(seconds);
}





It's not some trick of computer code. It's just basic arithmetic. You know, that class in school that everyone thought they could sleep through because surely they would never need it.

Thankyou very much. I knew how to do it just I didn't know how to display it without decimal. Thankyou.

I cleaned up the code and added comments to make it more user friendly.
Here is a link to the code if anyone needs it :slight_smile:

I am still using delay atm as I don't have button inputs plugged in.
Others should probably be using millis.

I will implement them in the future however it is late where I am. (5 am) and I need to finally goto bed.

Now marking project title as [Somewhat Solved] Thankyou :slight_smile:

UKHeliBob:
Read reply #1 in conjunction with the BlinkWithoutDelay example and the idea will suddenly click.

I attempted adding millis but the clock went too fast. If you may assist me with the process???
Code: #include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 7, 6, 5, 4);long tim - Pastebin.com

  prevTime += 1000;

...or the clock will drift.

I attempted adding millis but the clock went too fast. If you may assist me with the process???

It's difficult to tell what is wrong with your code when you don''t post what you tried. The code you linked to (why not post it here ?) does not use millis()

HazardsMind:
So this doesn't work?

static unsigned long prevTime = millis();

unsigned long milliseconds = millis();
if((milliseconds - prevTime) >= 1000) // 1000 = 1 second
{
  timeInSeconds--;
  if(timeInSeconds < 0)
    timeInSeconds  = 0;

prevTime = milliseconds;
}

Here is my code. The display is looking glitchy now.
Image of its display: Timer Finished & Counting.
Code: clickme.