# Week 10 – Musical Instrument: Piano

Concept

After going over several examples of musical instruments that we could create using the specified digital and analog sensors, Sanjana Nambiar and I decided to settle on the old but gold piano. The concept behind our project is simple and straightforward: our piano will consist of 8 buttons that will correspond to piano tiles and the buzzer will play a corresponding musical note when the user presses one of the eight switch buttons. We integrated an analog input to our project by getting the value from the potentiometer and  mapping it to one of the four octaves. Based on this mapping, the button will play different variations of the same note i.e. A or a or a’ or a’’. Below is the illustration of the four octaves that we used and the corresponding notes and frequencies.

Code

```// arduino-equivalent frequencies of the musical notes for each tile of the piano
// each note is stored in a list consisitning of three other notes of  different pitch
int T_C[4] = {65, 130, 262, 523};
int T_D[4] = {73, 147, 294, 587};
int T_E[4] = {82, 165, 330, 659};
int T_F[4] = {87, 175, 349, 698};
int T_G[4] = {98, 196, 392, 784};
int T_A[4] = {110, 220, 440, 880};
int T_H[4] = {123, 247, 493, 988};
int T_c[4] = {130, 262, 523, 1046};

//names of each tile and the corresponding digital  pins
const int A = 11;
const int C = 5;
const int D = 6;
const int E = 7;
const int F = 8;
const int G = 10;
const int H = 12;
const int c = 13;

const int Buzz = A0;

void setup()
{
Serial.begin(9600);

//declare the switches as digital input devices
pinMode(C, INPUT);
pinMode(D, INPUT);
pinMode(E, INPUT);
pinMode(F, INPUT);
pinMode(G, INPUT);
pinMode(A, INPUT);
pinMode(H, INPUT);
pinMode(c, INPUT);
}

void loop()
{

//map that input to the the value in the range 0-4
potPosition = map(potPosition, 0, 1023, 0, 4);
Serial.println(potPosition);

//value 4 is just a dummy value that will be used to detect the end of the potentiometer,
//so when we get value 4 from the poentiometer, we treat it as 3
if(potPosition == 4){ potPosition = 3; }

//play the correspondting tone for each of the 8 swithces

noTone(Buzz);
}
```

Schematic

Demo

Future Improvements

Since we were limited in the use of sensors that we could use, our piano doesn’t really look like a real-world piano. So, one of the improvements we could make is to create a separate portable piano i.e. a cardboard piano covered with aluminum foil or equipped with force sensors, and adapt our code to play the same sounds when the corresponding tile will get pressed. We could also work on implementing the logic to handle the case when two tiles are pressed simultaneously.