Learning Objectives
- Emphasis: Software
- Hardware: LCD with additional components
- Software: Find, parse, and display current time
Materials
- Arduino Uno
- Breadboard and wires
- 16x2 LCD Display
- Potentiometer
- Piezo buzzer
- 100 ohm resistor
- 3 pushbutton switches
- 3-10kohm resistor
- LED
- 330ohm resistor
Reference Files
Breadboard Layout
- LCD: Hook up the LCD and the potentiometer the same way as we did in the Scroll tutorial. You should be using 10 of the 14 pins, hooked up to power, ground, the potentiometer, and Arduino pins 2-5, 11, and 12.
- Speaker: Connect one end of the speaker to pin 9. Connect the other end through the 100 ohm resistor to ground.
- Buttons: For each button, connect one side the power and the other through a 10-kohm resistor to ground. Connect Arduino pins 6-8 in between the buttons and the resistors.
- LED: Connect pin 10 through the 330-ohm resistor to the long leg of the LED. Connect the short leg to ground.
- You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
Software
- Getting started: We want our timer to start counting when we press one button, pause when we press another, and reset when we press the third button. The LED will be on when tme is counting and off when it's not. The speaker will buzz whenever a button is pressed. The Arduino library has a function to keep track of milliseconds, but we're going to need an extra function to convert the raw time reading to something we can use. Let's get started! Initalize variables for your buttons, speaker, LED, and LCD.
- Time variables: We need variables to keep track of time started, finished, elapsed, and paused. Initalize four longs (decimal values) to store these numbers. We also need to know whether the timer is on or off. Initalize an integer that will be 0 when the timer is off and 1 when it's on. Inside your setup method, set your start and finished variables equal to millis(), the current time reading.
- DisplayResult Method: Create a new method called displayResult(). Inside this method, declare four floats (decimal numbers) to store the hour, minute, second, and millisecond. Then declare four integers to store the four digits we'll be displaying on our LCD. Finally, declare another long that will store the overflow.
- Find time elapsed: We need to find time elapsed in order to display it on the LCD, but time elapsed is different if the timer is going or paused. If timer is going, we can find time elapsed simply by subtracting our start time from the current millis() reading. If the timer is paused, we've stored the time paused into our finished variable, so elapsed = finished - start instead.
- Convert milliseconds to displayable time: We're going to need some math to change the milliseconds into minutes and seconds. The % operator allows us to store the remainder from division instead of simply the integer result. Use the formula in the code to the left to get your numbers into a usable form. The next set of if statements is necessary in case your timer has gone over an hour. We have one integer for each digit. After we have the digits, we concatenate them to form a string, then print the string to the console. Voila! You have displayable time.
- Loop method: Onto the next step! Inside our loop method, we're going to need conditional statements that let us define what happens when we start, pause, and reset the timer. Start out by storing the states of your three buttons into three variables.
- Reset: In this state, we need to reset all our times and turn off the timer and LED. Change start and finished to millis() so they store the same time. Turn off the LED, and change timerOn to 0. Then delay a few hundred milliseconds. This is called debouncing; it ensures that the program doesn't read the button as being pressed multiple times.
- Start timer: If we pressed the button to start the timer while the timer isn't running, we need to play a short tone on the speaker, turn the LED on, and change our time variables. We need to determine whether we're starting from scratch or from a paused state. If your start and finished variables are the same, it's starting from scratch, so you can just store start as the current millis(). Otherwise, you need to account for the time lost during the pause. Add the time paused (current time - pause) to start to move up your start time. Then turn on your timer variable and debounce the button.
- Pause timer: If we pressed the pause button while the timer is running, we need to store the time paused, turn off the LED and timer, and play a short note on the speaker. Store the current time in both finished and pause. Debounce the button to finish.
- Display time: You've already written a method to display the time on the LCD - call it at the end of your loop method by writing displayResult(). Congratulations, you have a functioning timer!
- Experiment: Add a few more LEDs to light up different colors for different states of your timer. What if you wanted to count down instead of up? Could you have more than one timer going at once?
Reference Code
<-- Previous: Karaoke
Next: Random Drawing Robot -->