Assign 3 Arduino Piezo - The Piano
Part 1: Twinkle Twinkle - 30%
Note: The Piezo produces an output as a mini buzzing speaker but it has the interesting property of also acting as an INPUT. It can detect vibrations and therefore is often described as a KNOCK sensor.
- Go to Page 20 of this PDF and read pages 18 & 19.Be sure to read the Making it Better section at the bottom.
- The image does not do a good job of showing how a Piezo is wired, but it is really simple:
- One wire to a DIGITAL pin
- One wire to Ground.
- Copy the code FROM HERE and Save the File as As3_Piano.
- Test the code, making sure your Digital Pin matches what is in the code.
Part 2: Creating the Twinkle Song Function - 30%
Note: we did part 2 together. If you were not here, you can get Couprie's code for all of Part 2 from the T: drive. Open Arduino, then work your way to: T(Studapps):/Computer Science/Computer Science 20/Arduino2022/As3_Piano. |
- Create a new function called Twinkle.
void twinkle( ){ }
- Then cut and paste all the code from within the LOOP function into your new Twinkle Function
- Call the Twinkle function at the end of SETUP to test that it still works. Because you are calling from SETUP, it should only play once
But that is too easy. So now you are going to alter the Twinkle function to take in a parameter
- Alter the twinkle function so that it takes in a parameter representing the number of times the song should loop.
void twinkle( ){ int times } //remember that in some languages you have to specify
//the type of variable, in this case int.
- Add a second FOR loop surrounding the existing FOR loop that simply plays the song that many times.
for(int plays = 0; plays<times; plays++){ }
- Test the function by playing the song twice.
Part 3: Add a new Song - 30%
Add a second function for a second song.
- It must be a real song but can NOT be Happy Birthday. May I recommend something by the Black Keys? Search Notes for Jingle Bells or Notes for Rudolph or Piano Songs for kids or Songs for Arduino if you need help.
- You may not use the same song as your neighbours.
- Add the song by copying the three song lines from the top of your code:
// ******** Change this code below for your new song
int length2 = 15; // the number of notes of the new song
char notes2[] = "ccggaagffeeddc "; // notes from the new song
int beats2[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //lengths of each note
- Again, create a function that accepts a parameter for the number of times to play the song..
- Call your new song from SETUP
- Demonstrate and show your code to your teacher
DO NOT DISMANTLE THE PIANO PROJECT. YOU WILL NEED IT FOR THE NEXT ASSIGNMENT. Save your file..
Part 4: Playing Live - Final 10%
- Earlier, you used the Serial Monitor to print output from the Arduino’ s sensors. This section shows you how to use Serial Input (from the computer - the keyboard in this case) to send the Arduino instructions.
- Create a new sketch called As3_Piano_Final 10
- Add the Serial Input starter code on the next page. Test it, reading the instructions that get printed in the Serial Monitor.
- Now for the hard part. Combine the knowledge learned from the above sections so that the user can enter the 7 different letters on the keyboard and the Arduino chooses the proper tone to play. Play each note with a length of 1.
- If it works, you should be able to enter an entire song at once into the SEND box.
Serial INPUT Starter Code - Needed for Part 4 Only
void setup() {
// initialize serial communication:
Serial.begin(9600);
Serial.println("Instructions");
Serial.println("Serial Input does not directly read the keyboard.");
Serial.println("Instead, you need to use the SEND box at the top of the Serial Monitor window.");
Serial.println("It reads one character at a time but multiple characters can be entered at once");
Serial.println("");
}//setup
void loop() {
if (Serial.available() > 0) { //needed in case of conflicting signals
int inputChar = Serial.read();
if (inputChar == 'a') {
Serial.println("typed a");
} else if (inputChar == 'b') {
Serial.println("typed b");
} else if (inputChar == '$') {
Serial.println("typed $");
} else {
Serial.println("");
}
}
}//loop