Concept
In this assignment, I collaborated with @Ruslan and we both love Christmas. The famous song Jingle Bells brings memories of the the times. So we explored various possibilities and decided to come up with speed variation of the Jingle Bells melody with respect to distance.
Here is the demonstration Video:
Schematic
Here is the Schematic for our Arduino connections:
Code:
In the implementation of our our idea, we searched for possible combinations of the notes and durations to match the Jingle Bells melody and stored them in an array. We then implemented the code mapping distance with durations. The variations in durations for each note make it seem playing faster or slower. Here is the code:
#include "pitches.h" #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0])) // Notes and Durations to match the Jingle Bells int JingleBells[] = { NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4, }; int JingleBellsDurations[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; const int echoPin = 7; const int trigPin = 8;; const int Speaker1 = 2; const int Speaker2 = 3; int volume; void setup() { // Initialize serial communication: Serial.begin(9600); pinMode(echoPin, INPUT); pinMode(trigPin, OUTPUT); pinMode(Speaker1,OUTPUT); } void loop() { long duration,Distance; // Distance Sensor reading digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(5); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); Distance = microsecondsToCentimeters(duration); // Map Distance to volume range (0 to 255) volume = map(Distance, 0, 100, 0, 255); volume = constrain(volume, 0, 255); // Play melody with adjusted volume playMelody(Speaker1 , JingleBells, JingleBellsDurations, ARRAY_LENGTH(JingleBells), volume); // Debug output to Serial Monitor Serial.print("Distance: "); Serial.print(Distance); Serial.print(" Volume: "); Serial.print(volume); Serial.println(); } // Get Centimeters from microseconds of Sensor long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } // PlayMelody function to accept volume and adjust note duration void playMelody(int pin, int notes[], int durations[], int length, int volume) { for (int i = 0; i < length; i++) { // Adjust the note Duration based on the volume int noteDuration = (1000 / durations[i]) * (volume / 255.0); // Play the note with adjusted Durations tone(pin, notes[i], noteDuration); // Delay to separate the notes delay(noteDuration * 1.3); noTone(pin); } }
Reflections
Reflecting on this project, I learned a lot about working with notes, melodies. I was interested my the fact that even complex music arrangements are made up of simple notes. The song “Jingle Bells” in particular really made me appreciate the structure of music on a new level. Each note represents a small part of the song, and adjusting the timing or pitch make the whole melody.
Working with @Ruslan made the process even more interesting, as we were both curious and explored various aspects before coming up with the final decision on music’s speed. I hope to continue working on musical notes in future projects.