Intermediate
...


Learning Objectives

  • Emphasis: Software
  • Hardware: LCD Display (Become more comfortable)
  • Software: Print stored text to LCD screen

Materials

  • Arduino Uno
  • Breadboard and wires
  • 16x2 LCD Display
  • Potentiometer
  • Piezo buzzer
  • 100 ohm resistor
  • Pushbutton switch
  • 10kohm resistor
Reference Files

Breadboard Layout

  1. 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.
  2. Speaker: Connect one end of the speaker to pin 9. Connect the other end through the 100 ohm resistor to ground.
  3. Button: Connect one side of the button to power and the other through the 10kohm resistor to ground. Connect a wire from pin 8 on the Arduino to the joint between the button and the resistor. This will read the value from the resulting voltage divider.
  4. You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
...

...

Software

  1. Getting started: Import pitches.h and the LCD display and initialize variables for the LCD, button, and speaker.
  2. Store song values: To store our song, we need three arrays. One will store the lyrics. One will store the note names. Those will be used for the LCD display. The third array will store the pitches that will be played by the speaker. You can either use the same values in the code on the left or make up your own song. If you want silence, put a 0 in the pitches array.
  3. Setup: Set the speaker pin to output and the button pin to input. Initalize the LCD with lcd.begin(16,2).
  4. Start playing song: We want to check if the button has been pressed to start the song. If so, we will loop through our arrays and output the values to the LCD and speaker. Start out by writing an if statement to check if the button has been pressed.
  5. Loop through LCD values: Our song has four lines, so we need to run an if statement four times. For each loop, we want to print one line of lyrics and one line of notes to the LCD. To print to the LCD, you first set your cursor to the space you want to start, then print. We'll need to set our cursor to the top line, print one line of lyrics, set our cursor to the second line, and then print one line of notes.
  6. Play song notes: Each one of our lines corresponds to four notes, so for each time through our outer loop, we'll need an inner loop to run four times. Inside our inner loop, check to see if the current note is 0 - that means silence. If it is, output noTone to your speaker pin. Otherwise, play the note using tone. The duration of the note will be 250 msec. Then, delay for 500 msec to let the note play out. Outside the inner loop, but still in the outer loop, clear the LCD display for the next time through the loop.
  7. Experiment: Try writing different songs with new lyrics. Can you add more buttons so you can choose different songs? If you want to get more complicated and write a longer song, experiment with storing the lyrics and notes in an outside text file and loading it in later.
Reference Code

The Finished Product




<-- Previous: Scroll Next: Stopwatch -->