Concept:
Our musical is sort of a 3 note piano with 3 octaves. There are three buttons that each play a specific note (A, B and C), and an ultrasonic sensor to measure the distance which in turn specifies the octave the user wants to play the notes in (3,4,5). The nearest interval plays the notes in the third octave, the next plays the notes in the fourth interval which sounds higher than the first interval and the last interval plays the notes in the fifth interval which sound higher than the middle interval. The buttons are controlled by switches that act as our main digital input and trigger specific notes depending on the secondary input which is the analog input from the ultrasonic sensor that specifies the octave to play the notes. We have also included an LED light that blinks when the instrument is first started and when the user has exceeded the range of the input from the ultrasonic sensor.
Notes played: A3, A4, A5, B3, B4, B5, C3, C4, C5.
Process & Highlights:
It was an interesting process trying to figure out how to connect everything together the right way and to get the ultrasonic sensor to work as expected, but once we figured everything out it was pretty easy to follow through the code. It helped to work as a team because we brainstormed the idea together and worked on the logic of the code and implemented it together which was more fun than working individually. I would say the highlight was finally getting it to work the way we wanted it to.
Here is a video demo of our instrument:
Code:
const int trigPin = 9; const int echoPin = 10; const int ledPin = 13; const int buzzerPin = 8; const int switchPin1 = 2; // Pin for the first switch const int switchPin2 = 3; // Pin for the second switch const int switchPin3 = 4; // Pin for the third switch float duration, distance; // Melodies #include "pitches.h" int melody11 = NOTE_C3; int melody12 = NOTE_C4; int melody13 = NOTE_C5; int melody21 = NOTE_A3; int melody22 = NOTE_A4; int melody23 = NOTE_A5; int melody31 = NOTE_B3; int melody32 = NOTE_B4; int melody33 = NOTE_B5; void playNote(int melody){ tone(buzzerPin, melody, 800); delay(1000); } void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(buzzerPin, OUTPUT); pinMode(switchPin1, INPUT_PULLUP); pinMode(switchPin2, INPUT_PULLUP); pinMode(switchPin3, INPUT_PULLUP); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration * 0.0343) / 2; if (distance < 10) { Serial.println("Distance: 1"); digitalWrite(ledPin, HIGH); // Turn on the LED if (digitalRead(switchPin1) == LOW) { playNote(melody11); } else if (digitalRead(switchPin2) == LOW) { playNote(melody21); } else if (digitalRead(switchPin3) == LOW) { playNote(melody31); } } else if (distance < 20) { Serial.println("Distance: 2"); digitalWrite(ledPin, HIGH); // Turn on the LED if (digitalRead(switchPin1) == LOW) { playNote(melody12); } else if (digitalRead(switchPin2) == LOW) { playNote(melody22); } else if (digitalRead(switchPin3) == LOW) { playNote(melody32); } } else if (distance < 30) { Serial.println("Distance: 3"); digitalWrite(ledPin, HIGH); // Turn on the LED digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH); if (digitalRead(switchPin1) == LOW) { playNote(melody13); } else if (digitalRead(switchPin2) == LOW) { playNote(melody23); } else if (digitalRead(switchPin3) == LOW) { playNote(melody33); } } else { Serial.println("Distance: 4"); //Blink digitalWrite(ledPin, LOW); // Blink the LED digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW); noTone(buzzerPin); // Turn off the buzzer } }
Reflections:
We found this exercise a bit harder than the previous one but it was more fun to implement. If we could change one thing about our instrument, it would be to maybe have a screen display the note being played as well as more buttons to replicate an actual piano with more octaves.Additionally, we would love to find a way to incorporate more creativity within.
Done by Mirette & Keya
References:
pitches.h used from tone()