Week 10 Group Assignment

whimsical musical mixer box

Concept

In this project, we designed a unique musical box with a playful twist. Rather than a conventional music box, our creation serves as a musical instrument. The distinctive feature lies in its utilization of an ultrasonic sensor to generate melodies. Additionally, the musical box incorporates a servo motor and an LED light for night use, both controllable digitally. It features an original melody, namely “Ode to Joy,” adding a personalized touch to the musical experience. Inspired by the spirited character Vanellope from the animated movie “Wreck-It Ralph,” we aimed for a funky and whimsical vibe. To enhance its visual appeal, we crafted the box using cardboard and adorned it with starry A4 paper, giving it a delightful and eye-catching appearance, and used a heart-shaped object to play the original song.

Video: 

Musical instrument Project

Code & Highlights
#include "pitches.h"


// Defining the pins for the ultrasonic sensor and buzzer
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 11;


// Background melodies
int backgroundMelody1[] = {262, 294, 330, 349, 392, 440, 494, 523};
int backgroundMelody2[] = {330, 349, 392, 440, 523, 587, 659, 698};
int backgroundMelody3[] = {392, 440, 494, 523, 587, 659, 698, 784};
int backgroundBeat[] = {200, 200, 200, 200, 200, 200, 200, 200};


// Setup function runs once at the beginning
void setup() {
 // Setting the pin modes for the ultrasonic sensor and buzzer
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(buzzerPin, OUTPUT);
 //  serial communication for debugging
 Serial.begin(9600);
}


// Loop function runs repeatedly
void loop() {
 // This sends a short pulse to trigger the ultrasonic sensor
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);


 // Measure the duration of the echo
 long duration = pulseIn(echoPin, HIGH);
 int distance = duration * 0.034 / 2;


 // Print the distance to the serial monitor for checking
 Serial.print("Distance: ");
 Serial.println(distance);


 // Play different notes or melodies based on distance
 if (distance < 7) {
   playMelody();
 } else if (distance < 20) {
   playNote(330);
   playBackgroundMelody1();
 } else if (distance < 30) {
   playNote(392);
   playBackgroundMelody2();
 } else if (distance < 40) {
   playNote(440);
   playBackgroundMelody3();
 } else {
   noTone(buzzerPin);
 }


 delay(500); // Adjust the delay based on your preference
}


// Function to play a single note
void playNote(int frequency) {
 tone(buzzerPin, frequency);
 delay(500);  // Adjust the duration based on your preference
 noTone(buzzerPin);
}


// Functions to play background melodies
void playBackgroundMelody1() {
 for (int i = 0; i < 8; i++) {
   tone(buzzerPin, backgroundMelody1[i]);
   delay(backgroundBeat[i]);
   noTone(buzzerPin);
 }
}

 

Reflection  and Areas of improvement

When we first implemented this project, we were aiming to have more than one song or melody that the user can switch to with a button, unfortunately, we faced a lot of difficulties and issues, when it came to the code and hardware, specifically the buttons connection to switching from one song to another, including how it was affected when other hardware devices were added. Another area we struggled with is the servo motor’s ability to move in a 360 loop. If we were to create this again, we would have added more songs instead of just one original song, and include a switch to control the volume.

 

References:

https://github.com/robsoncouto/arduino-songs/tree/master

 

Leave a Reply