Below are the on and off states of the Digital Input and Output with Arduino lab. It was fairly simple to set up physically on the breadboard and digitally in the code.
The creative lab was to create a combination lock of sorts. This reminded me of a scene in the movie Batman Begins. Bruce Wayne opens a secret entrance to the Batcave by playing several notes on a piano. I knew I couldn't unlock a secret entrance (yet), so I decided to tweak the idea a bit. However, I first needed to create the "keyboard."
I repeated the switch portion of the lab multiple times to create the keys on a typical piano. I then connected them to different pins so that I am able to digitally read whenever each switch was pressed. The coding portion was also relatively easy since it was just repeating the same lines of code for the additional pins.
I then moved on to the audio portion of the project. I wasn't sure how sound worked in Arduino, so I looked at the toneMelody example in the IDE. This example played a simple melody. In this example, it used an external file called pitches.h that contained all the musical notes. I only needed a couple (C-G) so I just looked for the values of these constants to use in my project.
Now that I learned how to implement these sounds in the code, I needed to figure out how to physically play the sound. I didn't have a speaker, so I went to the shop to look for one. The person at the help desk (Aaron) helped me greatly by hacking together this makeshift speaker from a broken pair of headphones. I treated the speaker like an LED light since I figured that digital output, sound or visual, should be the same. I then assigned each switch with it's own pin. And by using digitalRead, I would play a specific tone out of the speaker whenever a respective switch is pressed.
I found that the switches didn't really give me the feel of a piano keyboard. So I got some cardboard, cut some flaps to represent keys, and created a very crude keyboard. I then took off the switches for each pin, and taped the negative wire to the bottom of each flap. I then used a long piece of foil and connected a single wire that connected it to the positive current. By pressing down on the flap, the ends of the wire would touch the foil and complete the circuit for the pin.
I learned that wire management is an artform in itself. Although it worked, my project looked incredibly messy with all the different length wires. I decided to clean it up a bit, which took quite a bit of time since I had to rewire everything on the breadboard. This taught me that I definitely need to prepare and carefully plan the positioning of my components and wires for any future projects.
As for the combination lock assignment, I coded it so that when a specific sequence of notes are played, a secret song would play out. In this case, if the first five notes of "Mary Had a Little Lamb" was played in the correct sequence, the combination is complete and it "unlocked" the rest of the song.
The most difficult part of the project was to figure out how to detect when a note is played once. Since everything is in a very fast loop, I couldn't just digitallyRead whenever a certain pin was read as HIGH because then the combination sequence wouldn't work. I needed to figure out exactly when a note was pressed AND released. I eventually figured it out by keeping track of two global values that detect when a key is pressed and released. Another obstacle was debugging my project. My combination lock logic was not completely working at first. I ended up adding a lot of printlns to the console in order to figure out what was going wrong. I have added the code below.