LED Blink Clock

Step 1 - Build the Project

The Arduino software has a built-in timer called millis() that starts counting from 0 as soon as a program starts. In this code, you can use the millis() function to count up time, one millisecond at a time, and build a clock to display the hours and minutes.

Step 2 - Upload the Code

/* * Use LEDs and a software clock to blink out the current hour and minutes * when you press a button */ //Set the time here. Set it 5 seconds ahead to account for upload time! byte hour = 9; byte minute = 42; byte second = 05; unsigned long timer = millis(); //Function to check the time. //Call it by typing getTime(); any time you want updated time in your code void getTime() { //Make 'second' equal only to a whole number second = second + ((millis() - timer) / 1000); //Add remainder of the second to next check timer = timer + (((millis() - timer) / 1000) * 1000); if (second >= 60) { minute++; second = 0; } if (minute >= 60) { hour++; minute = 0; } if (hour > 12) { //delete this 'if' to use 24 hour time style. hour = 1; } } //Function blinks a light given the pin number and which variable to blink void blinkTime(byte _pin, byte _time) { for (int i = 0; i < _time; i++) { int wait = 200; //time between blinks in milliseconds digitalWrite(_pin, HIGH); delay(wait); digitalWrite(_pin, LOW); delay(wait); } } void setup () { pinMode(13, OUTPUT); //Hours LED pinMode(8, OUTPUT); //Minutes' 10s place LED pinMode(4, OUTPUT); //Minutes' 1s place LED pinMode(A5, INPUT_PULLUP); //Button to check time } void loop() { getTime(); //If 'check time' button is pressed, run the blinkTime function for if (digitalRead(A5) == LOW) { blinkTime(13,(hour)); delay(400); blinkTime(8,(minute / 10)); //blink 10s digit of minutes delay(400); blinkTime(4,(minute % 10)); //blink the 1s digit of minutes } } // Using an 'if' statement and boolean &&, try to create an alarm // for a certain hour and minute // Try to convert this to a clock that beeps tones for the time! // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense

Step 3 - Read the Walkthrough

The first step in creating the clock is to set the time using variables. You create the byte variables hour, minute, and second and give them the current time. This is the starting place for your clock’s count. Next, create an unsigned long variable called timer. You will subtract this variable from the millis() count to determine how much time has passed.

A function will make it very easy to grab the time value at any point in the sketch. This function is a void type, meaning it just does a process or task when it is called. This function simply updates the second, minute, and hour variables and ‘rolls over’ seconds into minutes and minutes into hours.

The next function you create reduces repetitive code by taking in the LED pin number you want to illuminate and the variable that LED represents and running the digitalWrite() function with those two numbers. When you create the function, you need to tell it what type of variables it should expect and name those variables. These variables exist only within the function, so if you pass the function another variable from the loop, it will get renamed in the function.

Your next step is setting up the hardware. You have only input for the three outputs, but the outputs are all tied together and will all execute at one time in the loop.

The loop section of this code is focused on checking the status of the button and updating the functions. The first thing it does every loop is run the getTime() function so that the time is very up-to-date.

Until the ‘if’ statement becomes true (i.e. the button is pressed), the loop will continue to update the time variables. When the button is pressed, the loop calls the function blinkTime() with the LED pin and the variable to blink on that pin. Then, the function runs ‘variable’ number of times and jumps back to the loop, delays 400 milliseconds, and then calls the function again with the next LED pin and variable to blink on that pin. The third call to blinkTime uses a ‘modulo’ (%), which essentially says “Take the value of minutes, divide it by ten, and give me the remainder of that equation”. So if the time is 8:52, the minute %10 is (52/10= 5 with a remainder of 2). The two is what is passed to the blinkTime() function to be blinked.

Once everything has blinked through, the loop starts again by updating the time and checking the status of the button over and over until it’s pressed again.