Computer Architecture and Organization Electronic Keyboard By: John Solo and Shannon Stastny CPSC - 42, Merced College Professor Kanemoto, December 08, 2017
Abstract Our arduino project consists of an electronic keyboard that has seven piano keys. The piano keys are as follows: B5, A5, G5, F5, E5, D5, and C5. The user is able to freely play a song by utilizing these seven keys. The second part of our project includes a piano player that plays preset songs. We have an LCD screen designed with a user interface that allows the user to navigate to which song that they want to listen to by pressing the eight button and any piano key. We implemented four preset songs: Super Mario Theme, Super Mario Underworld Theme, Star Wars Main Theme, and the Zelda Main Theme. We found the original sheet music for these songs and converted it into compliable code for the arduino to playback to the user with the piezo capsule. 2
Table of Contents Abstract 2 1.0 Introduction 4 2.0 Hardware and Components 4 2.1 Components Listing 5 2.2 Hardware Setup 5 2.3 Completed Hardware 6 3.0 Diagrams 7 3.1 Circuit Diagram 7 3.2 Schematic 8 4.0 Sheet Music 9 5.0 Software 10 5.1 electronic_keyboard.ino 10 Frequencies 11 Void loop() 11 Song1title() 12 5.2 pitches.h 13 5.3 song1.ino: Super Mario Theme 14 Melodies 14 Note Durations 15 For Loop 16 6.0 Code Additions 17 6.1 Song3: Star Wars Main Theme 17 6.2 Song4: Zelda Main Theme 18 7.0 Conclusion 19 References 20 3
1.0 Introduction Our project expands on the Electric Piano Keyboard with Preset Songs by Lindsay Fox on Hackster.io. We also drew inspiration from Arduino Lab 7 Keyboard Instrument to implement the piezo buzzer and additional buttons. The electronic keyboard acts as a piano that has seven piano keys that are programmed to the middle of B/A/G/F/E/D/C. It can also act as a pianola or a player piano that plays premade piano songs. The original code came packaged with five preset songs, but we scrapped three of the songs to add two of our own. 2.0 Hardware and Components (Images taken from Hackster.io) 4
2.1 Components Listing 1x Arduino Uno 1x Adafruit Standard LCD - 16x2 8x Pushbutton 4x 220 ohm resistor 1x 560 ohm resistor 1x 1k ohm resistor 3x 10k ohm resistor 1x 1M ohm resistor 1x Piezo buzzer 1x LED 1x Potentiometer 1x Pack of jumper wires 2x Breadboard Piano Keys 2.2 Hardware Setup There s seven piano keys each connected to a resistor. The resistors used for the switches are 220, 560, 1k, 10k, and 1M ohm. The leftmost switch starts with the smallest resistor and each consecutive switch is connected to a resistor larger than the previous switch. Essentially, we have a resistance ladder that are connected to A0. LCD Screen The LCD screen is connected to the potentiometer and connected to pins 2-5. Piezo Buzzer The piezo buzzer is connected to a resistor and pin 8, and it is in its own separate breadboard. LED (generic) The LED is connected to pin 6 with a push button connected to pin 7. Both are connected to a resistor. 5
2.3 Completed Hardware One of the challenges that we faced when completing the hardware was supplying voltage to the entire breadboard. For some reason, our breadboard was separated into two halves so we had to jump wires from one half of the breadboard to the other. Another challenge was completing the circuit itself. As soon as we connected the wire to ground, the arduino turned off. We broke one of our initial counterfeit arduino r3 because we hooked it to a usb power supply and a 12v power supply. We eventually ended up using the official product. Shannon designed a box to make our hardware more stylish and user friendly. She handled most of the user interface design and made the piano key buttons intuitive by making them longer/higher as the note gets higher. We re both music enthusiasts and we wanted to blend our passion for music with our passion for computer science. Overall, we completed a product that plays preset songs and allows the user to play their own songs. It serves as an entertainment device. Here s our hardware with its circuitry completed: 6
3.0 Diagrams Diagrams were made with fritzing software. The first diagram is from the hackster.io website but the schematic was entirely done by us. 3.1 Circuit Diagram (Taken from Hackster.io) 7
3.2 Schematic 8
4.0 Sheet Music Adding additional preset music wouldn t have been possible without finding the sheet music for the songs. The sheet music provided below are sample sheets that can be found at musicnotes.com and acts as a reference for us when coding preset songs. We transcribed the sheets and converted it into compilable code for the arduino to interpret and playback to the piezo buzzer. Our program utilizes an array that contains the pitches that the song depends on and a for loop that traverses through the array. (Taken from musicnotes.com) Song 3 was coded with the sheet music on the left while song 4 was coded using the sheet music to the right. 9
5.0 Software The main part of the software was written by Lindsay Fox on hackster.io, but the other two individual preset Mario songs (song1 and song2) were coded by Dipto Pratyaksa. There s five program files in total with one of the files being an external library called pitches.h that contains individual constant variables referencing different pitches. We added two new songs to the code: Zelda Main Theme and Star Wars Main Theme; replacing Songs: 3, 4, and 5. Our project files include: electronic_keyboard.ino, pitches.h, song1.ino, song2.ino, song3.ino, song4.ino 5.1 electronic_keyboard.ino Lindsay Fox imported two external libraries: LiquidCrystal.h for the LCD screen and pitches.h for the piezo buzzer. The opening code defines the pins and their matching hardware components and an array of notes for buttons. 10
Frequencies These if-else statements defines the frequencies associated with each pushbutton. It utilizes the array of notes and associates them with different frequencies. The notes are as follows: C,D,E,F,G,A,B. There s a final else if that does not play notes if none of the switches are pressed. Each switch has a different resistance and plays a different note from the array defined in the beginning of the code. Void loop() Here s the void loop code that loops for the LCD screen and the song titles. 11
Song1title() This is the song title loop part code that s part of electronic_keyboard.ino. It references the song1.ino file that contains the mario song and it defines the words that ll appear on the LCD screen. It operates when the menu button is pressed and another key from one the seven piano keys are pressed. 12
5.2 pitches.h This is an important external library that allows us to utilize music notes in our arduino program. This file contains constant variables that are associated with different pitches. The file does not contain any flats but it does have sharp notes. Our array melodies in each file contains these music notes and with the help of note duration arrays, we are able to create a song for our arduino program. 13
5.3 song1.ino: Super Mario Theme The song s arduino code was written by Dipto Pratyaksa, and there s a link to the original article in the code file. This song file serves as a basic structure for us to manipulate and personalize so we can code our own songs. Here is the basic code anatomy for a song file which includes: melodies, note durations, and loops. Melodies We have an int melody array that contains the note values of the song. These notes are referencing the constant variables that are in the pitches.h file. The notes are modeled from the original sheet music for the Super Mario Theme. The 0 represents a stop time and still has a note duration associated with it. 14
Note Durations The note durations has the same amount elements as the melody array because it defines the delay for each note. The note duration are coded as follows: a four is a quarter note, an eighth is an eighth note, a nine is a triplet, and a twelve is a sixteenth note. The note duration can be found by reading the original sheet music. 15
For Loop This for loop is what glues the melody array and note duration together. The size variable grows proportionally to the size of the array and terminates the loop as soon as the index reaches the size. This part of the code loops through both the note duration and melody array and outputs them to pin 8 which is connected to the piezo buzzer. Most of our code additions follows this basic structure. 16
6.0 Code Additions Our code additions follows the code anatomy of song1 and song2. We have a melody array, note duration array, a for loop, and some LCD print statements to indicate what song the user is listening to. Song3 is the Star Wars main theme and Song4 is the Zelda Main Theme. We based our code on the sample music sheets from musicnotes.com and images are located in section 4.0 Sheet Music. Song3 has fifty new lines of code and song 4 has fifty new lines of code. In total, we added one hundred new lines of code. 6.1 Song3: Star Wars Main Theme This entire file contains fresh new code from us, but follows the same structure as song1. Most of the code looks really similar, but the constant values and note durations are entirely different from the other songs since its unique to this song. 17
6.2 Song4: Zelda Main Theme The Zelda Main Theme was the first song we coded. It was longer and more challenging than the Star Wars Main Theme since there were note durations that were counterintuitive to our code. We troubleshooted the audio playback by ear and that was how we debugged the song. The code is separated into paragraphs to help the coder identify where in the sheet music we derived our notes from. Here s the for loop for the melody and note duration code: 18
7.0 Conclusion The Electronic Piano was able to evolve through a series of collaborative efforts that allowed the project to grow into a fully functional product that can entertain its users with hardware produced music. From the hardware to the software, we meticulously researched and implemented ways to transform how music is perceived. Electronic instruments isn t a new phenomenon, but showing the relationship between applied mathematics and music theory illustrates how intertwined the humanities and the sciences are. The two needed each other. Each pitch had a constant numerical value, but without the proper arrangement of the pitches, the sound that played back wouldn t make sense. Music is simply a collection of organized sounds and without the mathematical backing of the pitches, there wouldn t be any sounds to organize. Our project allowed us to venture outside of strictly performing computational analysis for computers, and allowed us the creative freedom to implement music with Computer Science principles. 19
References Fox, Lindsay. Electronic Piano Keyboard with Preset Songs. Hackster, 11 Feb. 2016 Kondo, Koji. The Legend of Zelda Main Theme from 'The Legend of Zelda' Sheet Music (Easy Piano) (Piano Solo) in C Major - Download & Print. Musicnotes.com, www.musicnotes.com/sheetmusic/mtd.asp?ppn=mn0109469. Williams, John. Star Wars (Main Theme)" from 'Star Wars' Sheet Music (Easy Piano) 20