Inspiration
For this assignment, using both analog and digital sensors, we were tasked to create a musical instrument. At first, we thought of creating a drum set. After doing some research we realized that we will have to connect a MIDI tool and use that as an interface in order to make a drum that mimics the actual sounds that come from a drum. Since we had already covered some notes in class (pitches), we decided to make a Piano instead but using the same methods we would use to make the drums.
Implementation
We started by researching all our possible analog sensors. We think the sensor best-suited for this task is a piezoelectric pressure sensor because we wanted to measure the force with which the user presses down on each key.
Next, we wanted to design our piano. For this, we used a cardboard box from the lab which had a piece of cardboard that could be flipped up and down. This flipping part would serve as the piano keys and the rest of the box can be used to hide the Arduino and the circuits. Piezo sensors are stuck underneath each key using double-sided tape. We also soldered wires together to make the circuit compact enough to fit inside the cardboard box and so that the wires may be led to the Arduino through holes in the box. Finally, we used a potentiometer to adjust for the volume of the buzzer.
Initial setup (testing)
Reflection
While we have made a piano that can play 6 notes, it’s biggest drawback is in how limited it is. We believe that this problem can be solved by either having a button (switch) that changes all the notes being played so that every key is now bound to a new note when the button is pressed or by achieving the same thing using a potentiometer.
We ran into a problem at the end. The last piezo sensor does not seem to work. We spent most of our time trying to figure out what’s wrong with our code that triggers the last sensor every time in draw loop. But, in the end, we figured that the sensor was faulty. Since we did not have enough time to fix this and use another sensor (we had already soldered the faulty one), we did not include that sensor in the piano.
Here is how the final setup looked. Of course, this was not how we imagined it to be, but it was the right thing to do since we didn’t want to break the connections and have the wires come loose. Even after all the soldering we did, there were few wires that did not connect properly to the alligator cables and thus the piezo disk would play any sound.
Arduino Code
#include "pitches.h"
int analogpin[6] = {A0, A1, A2, A3, A4, A5};
const int buzpin = 4;
int threshold = 200;
int flag = 0;
bool a5 = false;
double curr;
double goal;
void setup()
{
Serial.begin(9600);
pinMode(buzpin, OUTPUT);
}
void loop()
{
// int test = analogRead(analogpin[4]);
// Serial.println(test);
if (analogRead(analogpin[0]) < threshold && flag == 0) //&& flag == 0
{
flag = 1;
Serial.println("A0");
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_B4, 100); //need the duration here?
}
}
if (analogRead(analogpin[1]) < threshold && flag == 0) //&& flag == 0
{
flag = 1;
Serial.println("A1, pressing");
digitalWrite(buzpin, HIGH);
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_D4, 100); //need the duration here?
}
}
if (analogRead(analogpin[2]) < threshold && flag == 0) //&& flag == 0
{
flag = 1;
Serial.println("A2");
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_C4, 100); //need the duration here?
}
}
if (analogRead(analogpin[3]) < threshold && flag == 0) //&& flag == 0
{
flag = 1;
Serial.println("A3");
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_G4, 100); //need the duration here?
}
}
if (analogRead(analogpin[4]) < threshold && flag == 0) //&& flag == 0
{
flag = 1;
Serial.println("A4");
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_G3, 100); //need the duration here?
}
}
if (analogRead(analogpin[5]) < threshold && flag == 0 && a5 == false) //
{
flag = 1;
a5 = true;
Serial.println("A5");
curr = millis();
while(millis() < curr + 100){
tone(buzpin, NOTE_D3, 100); //need the duration here?
a5 = false;
}
}
flag = 0;
}