Surprise Birthday party- Music Machine

Music Machine:

The ultimate surprise party gadget. I have had this idea after my friend’s surprise birthday party. This gadget detects when the person passes in front of the ultrasonic motion sensor and automatically plays the famous birthday song tune and lights up three LEDs that create the festive vibes. The music machine also played a different tune depending on where the detected person (or teddy bear) is situated. Thus, the gadget will play different tune depending on the where the person is. This allows the music machine to keep playing different melodies throughout the party and set the party mood.

circuit :

the circuit includes ultrasonic motion sensor, buzzer, 3 LEDs, 3 resistors, variable resistor, Arduino uno.

I have included the variable resistor as it allows to adjust the volume of the buzzer. The LEDs are wired in parallel as it allow to for the LEDs to light up alternatively and create a light pattern. The diagram below describes all the wiring in the circuit:

Arduino script:

The biggest challenge I found while setting the script was making the LEDs light up simultaneously as the buzzer is on, but also creating some sort of pattern when the LED light up. I ended up solving that problem by creating a function Playtune() that gets called from inside the void Loop(). This allows both tasks to be done with interfering with each other.

int trig = 10;
int echo = 11;
long duration;
long distance;

// Define the LED pins
int led1 = 6;
int led2 = 7;
int led3 = 8;

// Define the Happy Birthday tune
int tune1[] = {264, 264, 297, 264, 352, 330};
int tune2[] = {264, 264, 297, 264, 396, 352};
int Newtune[] = {392, 330, 330, 349, 392, 392, 392, 440, 392, 349, 330, 294};


int tune1Length = sizeof(tune1) / sizeof(tune1[0]);
int tune2Length = sizeof(tune2) / sizeof(tune2[0]);
int NewtuneLength = sizeof(jaws) / sizeof(Newtune[0]);

void lightUpLed(int led, int duration) {
  digitalWrite(led, HIGH);
  delay(duration);
  digitalWrite(led, LOW);
}

void playTune(int tune[], int tuneLength) {
  for (int i = 0; i < tuneLength; i++) {
    tone(12, tune[i]);
    
    // Light up the LEDs in a rhythmic pattern
    lightUpLed(led1, 150);
    lightUpLed(led2, 150);
    lightUpLed(led3, 150);

    noTone(12);
    delay(50);
  }
}

void setup() {
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) * .0344;    // 344 m/s = speed of sound. We're converting into cm

  if (distance > 0 && distance <= 35) {
    if (distance <= 15) {
      playTune(tune1, tune1Length);
      playTune(tune2, tune2Length);
    } 
    else {
       playTune(Newtune, NewtuneLength);
    }
  }
}



 

Leave a Reply