Published using Google Docs
3. Arduino Piezo - The Piano
Updated automatically every 5 minutes

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.

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.

void twinkle( ){     }

But that is too easy.  So now you are going to alter the Twinkle function to take in a parameter

                void twinkle( ){  int times   }  //remember that in some languages you have to specify

//the type of variable, in this case int.

                for(int plays = 0; plays<times; plays++){   }


Part 3: Add a new Song - 30%

Add a second function for a second song.

 

//  ******** 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

DO NOT DISMANTLE THE PIANO PROJECT.  YOU WILL NEED IT FOR THE NEXT ASSIGNMENT.  Save your file..

Part 4: Playing Live - Final 10%


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