Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
JaJasonckyJacky880211
Published

Christmas Music Box

lovely and sweetly box for christmas

BeginnerShowcase (no instructions)2,574
Christmas Music Box

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×4
sound amplifier module LM386
×1
SD card module(SPI)
×1
Speaker 8ohms, 2W
×2
button
×2
Jumper wires (generic)
Jumper wires (generic)
×30
Breadboard (generic)
Breadboard (generic)
×1

Hand tools and fabrication machines

welding torch

Story

Read more

Schematics

Circuit

Connects the speaker output pin to the sound amplifier module LM386. Then connects the output of LM386 to the speaker. Use the buttons to trigger the play of the music.(Use pull-up resistor to the pins connected to the buttons)

Code

Arduiyes

C/C++
#include <TMRpcm.h>
#include <SD.h>

File myFile;
TMRpcm music;

void setup() {
  Serial.begin(9600);
  Serial.println("Initial SD Card...");
  pinMode(10, OUTPUT);
  
  // chekc if the SD card exists and is usable
  if (!SD.begin(4))
  {
    Serial.println("Card failed, or not present");
    // stop sketch
    return;
  }
  Serial.println("SD card is ready");
  music.speakerPin = 9;  // set speaker pin for tmrpcm
  music.setVolume(5); // set the volume to level 5
  delay(100);
  pinMode(8, INPUT_PULLUP);  // pull pin high for checking button pushed
  pinMode(7, INPUT_PULLUP);
  
  // LED pin definition
  randomSeed(analogRead(0));
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

// 4 leds shine ramdomly 
void ledShine() {
  int randomNumber = random(1, 5);
  switch(randomNumber) {
    case 1:
      ledBlink(2);
      break;
    case 2:
      ledBlink(3);
      break;
    case 3:
      ledBlink(5);
      break;
    case 4:
      ledBlink(6);
      break;
      }
}

void ledBlink(int ledpin) {
  digitalWrite(ledpin, HIGH);
  delay(400);
  digitalWrite(ledpin, LOW);
  delay(50);
}
 
void loop() { 
  if(digitalRead(7) == LOW) {
    delay(100);
    music.play("1.wav");
    Serial.println("Song1 is playing...");
  }
  else if(digitalRead(8) == LOW) {
    delay(100);
    music.play("2.wav");
    Serial.println("Song2 is playing...");
    }
  else
  ledShine();
}

Credits

JaJasoncky
1 project • 0 followers
Jacky880211
1 project • 0 followers

Comments