Assignment #3: Piezo Cake Computer Science: 7 th Grade 7-CS: Introduction to Computer Science I Background In this assignment, we will learn how to make sounds by pulsing current through a piezo circuit. We will use a lookup table built using one-dimensional arrays to find the right note to play on our Arduino. Beats will be used on the tempo so that we could make a recognizable song from our experiment. Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch will be. Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C. To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or, as we have used here, a piezo sounder. Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear. Assignment #3: Piezo Cake Computer Science: 7 th Grade Page 1
Assignment Create a melody from one of your favorite songs that uses several different notes. These notes can include flats and sharps. Remember to include a proper tempo in your composition. If your song has half notes, quarter notes or even eighth notes, remember to have the proper beats for the tempo that you choose. Here are some suggestions: Star Trek theme (TOS) Star Wars theme (the original trilogy) Sound of Music (Julie Andrews, et al.) Holiday songs Children s nursery rhymes Anything with Mozart, Bach, Beethoven, Vivaldi, Schumann, Mendelssohn, Chopin, Rossini, Verdi, Donizetti, Puccini, Morricone Computer Program Requirements 1. Use an array to hold the notes of your song, in frequency values (Hz) 2. Use an array to hold the number of beats each note is to be played 3. Use LEDs to light up as your song plays, but make sure the correct LED sequence lights up 4. Use a push button to start and stop your Arduino project 5. Use at following functions in your code: i. int gettonevalue() Calculates the tone to generate based on the formula in Additional Resources ii. void playnote() Plays the tone that you just calculated for a given duration 6. Use loops and if-statements 7. Use variables Expectations When you are finished your composition, you will need to hand in the following: Your finished sketch with proper commenting and good programming style A written report, including the sheet music that you used. This can be taken from anywhere (Internet, book, copied or even hand written). If you didn t use any sheet music but sounded out the melody, write down the notes on a sheet of music paper A circuit diagram of your Arduino project showing all the circuits you used (i.e, piezo circuit, LEDs, wires, pin assignments, etc.). Label all pins and resistor values. Assignment #3: Piezo Cake Computer Science: 7 th Grade Page 2
Prerequisites To successfully complete this assignment, you will need to have completed the prior two assignments. Additional Resources Below is an Octave Chart that shows all the frequencies of the notes (Hz). Notice the sharps and flats too. As a reference, the red box shows the Middle C octave. Formulas to use: Period, P = (1 / f) * 1000000 Time high, TH = P / 2 where, f = frequency of the note in Hz, TH = the tone value to use in your sketch Hint: Pay attention to the use of double, float and int variables. Remember that these variables don t like each other! Assignment #3: Piezo Cake Computer Science: 7 th Grade Page 3
Code Template int notec4 = 261; // middle C, frequency in Hertz (Hz) int notecs4 = 277; // C sharp int noted4 = 293; int notee4 = 329; int notef4 = 349; int noteg4 = 391; int notea4 = 440; int noteb4 = 493; int notes[] = { notec4, noted4, notee4, notef4, noteg4, notea4, noteb4; int beats[] = { 1, 1, 1, 1, 1, 1, 2 ; int tempo = 500; // tempo of the notes, in milliseconds int speakerpin = 2; int pushbuttonpin = 13; void setup() { // initialize your pins pinmode(pushbuttonpin, INPUT); // Push button on pin 13 pinmode(speakerpin, OUTPUT); // piezo speaker pin is on pin 2 // Initialize the rest of the LED pins void loop() { if (digitalread(pushbuttonpin) == LOW) { // Use a for-loop to go through the notes[] array // Loop for 7 times because this is how many notes are in our array for ( int i = 0; i < 8; i++ ) { playnote(notes[i], beats[i] * tempo); // ************************* Functions are defined below ***************************** void playnote(int hertz, int duration) { int tone = gettonevalue(hertz); // tone value is stored in an int data type // Turn on the LEDs // You can use if statements here to turn on the correct LED for each note // The following for-loop will pulse the piezo speaker to create // an approximation of the proper note for (long i = 0; i < duration * 1000L; i += tone * 2) { // The piezo speaker will vibrate at a certain frequency, depending on how fast // we pulse the current. The faster the pulse, the higher the frequency. The higher the // frequency, the higher pitch sound digitalwrite(speakerpin, HIGH); delaymicroseconds(tone); digitalwrite(speakerpin, LOW); delaymicroseconds(tone); // Turn off the LED // Use the same if statements as before but turn off the LEDs this time double gettonevalue(int hertz) { double P = <put your equation here, see above> double TH = <put your equation here, see above> return TH; Assignment #3: Piezo Cake Computer Science: 7 th Grade Page 4
Grading Plan Grades are calculated using the following rubric: Finished working sketch 50% Proper creation and use of the functions: void playnote(), void gettonevalue() 10% Proper comments where needed 10% Good programming style 20% Written report, including sheet music to follow the melody 5% Accurate and labeled circuit diagram 5% Total marks possible: 100% Assignment #3: Piezo Cake Computer Science: 7 th Grade Page 5