Learning Objectives
- Emphasis: Hardware
- Hardware: Multiple buttons/switches with speaker
- Software: Longer conditional statements
Materials
- Arduino Uno
- Breadboard and wires
- 3 different-colored LEDs
- 3 pushbutton switches
- 8 ohm speaker
- 10 kohm photoresistor
- 1-100 ohm resistor
- 3-330 ohm resistors
- 3-10 kohm resistor
Reference Files
Breadboard Layout
- Power and ground: This project is going to require a lot of wiring, so don't worry if your board gets a little messy. Make sure when you start wiring that the buttons and LEDs are lined up so you can tell which is which - it'll make things easier on the user. Start by connecting a power and ground rail on the breadboard to the Arduino.
- Buttons: First create your Arduino-less button circuits by connecting the power rail to one end of the buttons and the other through the 10k resistors to ground. Then include the Arduino by connecting pins 10-12 into the middle of the circuits between the buttons and the resistors.
- Speaker: Connect the 100 ohm resistor from pin 9 to the speaker and then to ground. Speakers allow bidirectional current, so it doesn't matter which direction it's plugged in.
- LEDs: Connect the 330 ohm resistors from pins 6-8 to the long legs of the LEDs. Connect the short legs of the LEDs to ground.
- You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
Software
- Getting started: Initialize variables for the speaker, LEDs, and button pins. Remember, we have the buttons in 6-8, the speaker in 9, and the LEDS in 10-12. In your setup method, declare the LED and speaker as output and the photoresistor as input.
- Choose your pitches: First, include pitches.h the same way we've been doing it - save the file in your folder, then #include it at the top of your sketch. We're going to choose three pitches to play on the keyboard and save them into an array, similar to what we did in the harmonica program. We chose a major scale, but you can do whatever you like. Declare int notes[] = {NOTE_C4, NOTE_E4, NOTE_G4}.
- Read button input: In the loop method, declare variables to store the result of calling digitalRead on each button pin. We're going to use this information to determine which note to play.
- Turn on lights: Create an if-else block that accounts for 4 cases: one for each button being on and one for all being off. In each block, turn on the corresponding LED and turn the other two LEDs off. In the final block, make sure you turn off all the LEDs.
- Play notes: Inside of your if-else blocks, you're also going to call tone on the speaker and play a note from your selected notes array. For example, if button1State == HIGH, you would say tone(speakerPin, notes[0]). In the default method, write noTone(speakerPin) so it'll turn off if nothing is happening.
- Running the program: Upload your sketch and try it out! If the LEDs don't match the buttons, check your wiring or change the numbers in the sketch to make it work.
- Experiment: It's not hard to add more notes to your keyboard - it just requires a little extra wiring. You can also replace the normal LEDs with one RGB LED and have it light up different colors instead of having a separate LED for each note. Can you figure out how to play multiple notes at once? What about changing octaves with a fourth button?
Reference Code
<-- Previous: Motion Detector
Start Intermediate Projects!