Music Box
We made a music box using the Sparkfun kit. This music box can compete in the 4H Robotics Competition.
Supplies
The Sparkfun kit, carboard, hot glue, dowel rods, and an exacto knife were used in the construction of the project.
Wiring the Ardiuno
- The first part of the construction is to use the Arduino and breadboard given in the spark fun kit.
- Next, place the buttons in pins 1-9 as the diagram shows.
- Use the black wires to ground the buttons to the rails and connect them to pins 12, 11, and 10 on the Arduino.
- Moving onto the next section you will add the red, green, and blue LEDs which will occupy pins 10-15.
- You should ground the shorter leg of the LEDs.
- Use a 330V resistor over the bridge (on the longer leg of the LEDs).
- You will then wire the red LEDs to pin 9, the green LEDs to pin 8, and the blue LEDs to pin 7.
- Onto the next section we will wire the speaker. We will occupy pins 16-21 on the breadboard and make sure you ground the correct side of the speaker. We will connect the positive side to pin 5 on the Arduino.
- The last section will be the motor and you will need a motor driver, power 1 & 2 will be wired to the 5V, ground will be wired to ground, ¾ will go to pin 1, input 4 will go to pin 2, and input 3 will go to pin 3. Output 3 will go to red (positive) on the motor and 4 will go to black (negative) on the motor.
Physical Construction
Create a box out of carboard. A carboard horn can be made along with a CD if desired. Create webbing in the bottom of the box so the top of the LED barely pokes through the carboard. Poke a hole in the carboard for the motor and attach the carboard CD to the motor. Cut out holes above the buttons and use wooden dowels to press the buttons. Either use the battery kit that comes with the spark fun kit or cut a hole to supply power to the Arduino.
Code Instructions
Before Setup:
You need to define the buttons, LEDs, speaker, and motors as integers, assigning each its own pin. Additionally, store the data from the buttons, and assign each note its corresponding frequency and define note lengths to facilitate coding the songs.
Setup:
In the setup section of the code, designate the LEDs as outputs and the buttons as inputs. Also, configure the motor as an output and ensure it is initially turned off.
Loop:
Begin by reading the state of the first button; if pressed, activate the motor and set the speaker's speed. Simultaneously, illuminate the red LED and play the first song. Code the notes for the song, utilizing sheet music for simplicity. Upon completion of the song, deactivate the motor and turn off the red LED. Repeat this process for the second and third buttons, adjusting pins and variables accordingly.
Lastly you will need to add a function below the entire code. This won’t loop and it will correct the timing for the songs .
This is the complete code:
// C++ code
// This code is for a music box.
// Three different buttons can be pressed which correspond to three different songs.
// While each song is playing a different colored LED will light up.
// While each song is playing a motor will turn. The motor is used to spin a CD.
// declaring variables
// the integers are assigned to their port values
// integers for buttons
int Button_1 = 12;
int Button_2 = 11;
int Button_3 = 10;
// integers for LEDs
int Red_LED = 9;
int Green_LED = 8;
int Blue_LED = 7;
// integer for speaker
int Speaker = 5;
// integers for motor
//Declare integers for AIN1, AIN2, PWMA and assign to pins
int AIN1 = 3;
int AIN2 = 2;
int PWMA = 6;
// these integers will be used to save data from the buttons
int buttonval_1;
int buttonval_2;
int buttonval_3;
// declaring variables to use in functions
// declaring the input variables
int Note;
int Type;
// Creating variables for the notes
// Assigning the notes equal to their frequencies
int B = 494;
int A = 440;
int G = 392;
int F = 349;
int E = 330;
int D = 294;
int C = 262;
// Creating variables for different note types
// Assigning note types to appropriate durations
int EI = 250; // eighth note
int Q = 500; // quarter note
int H = 1000; // half note
void setup()
{
// assigning pins to be either output or input
//pinMode_LED Output
pinMode(Red_LED, OUTPUT);
pinMode(Green_LED, OUTPUT);
pinMode(Blue_LED, OUTPUT);
//pinMode_Buttons INPUT
pinMode(Button_1, INPUT_PULLUP);
pinMode(Button_2, INPUT_PULLUP);
pinMode(Button_3, INPUT_PULLUP);
//pinMode the motor components to output
pinMode (AIN1, OUTPUT);
pinMode (AIN2, OUTPUT);
pinMode (PWMA, OUTPUT);
// turn the motor off
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 0);
// allows you to read and check the serial board
Serial.begin(9600);
}
void loop()
{
//read button 1
buttonval_1 = digitalRead(Button_1);
// if the button is pressed
if (buttonval_1 == LOW) {
// turn the motor on
// To go forward, use digitalWrite to set AIN1 HIGH, AIN2 LOW
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
//Set speed using analogWrite to value between 0 and 255
analogWrite(PWMA, 50);
//turn LED blue
analogWrite(Red_LED, 0);
analogWrite(Green_LED, 0);
analogWrite(Blue_LED, 225);
//Play Mary Had A Little Lamb
// Measure 1
NoteFunction(E, Q);
NoteFunction(D, Q);
NoteFunction(C, Q);
NoteFunction(D, Q);
// Measure 2
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
delay(100);
NoteFunction(E, H);
// Measure 3
NoteFunction(D, Q);
delay(100);
NoteFunction(D, Q);
delay(100);
NoteFunction(D, H);
// Measure 4
NoteFunction(E, Q);
NoteFunction(G, Q);
delay(100);
NoteFunction(G, H);
// Measure 5
NoteFunction(E, Q);
NoteFunction(D, Q);
NoteFunction(C, Q);
NoteFunction(D, Q);
// Measure 6
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
// Measure 7
NoteFunction(D, Q);
delay(100);
NoteFunction(D, Q);
NoteFunction(E, Q);
NoteFunction(D, Q);
// Measure 8
NoteFunction(C, H);
//turn motor off
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 0);
//turn LED off
analogWrite(Red_LED, 0);
analogWrite(Green_LED, 0);
analogWrite(Blue_LED, 0);
}
//read button 2
buttonval_2 = digitalRead(Button_2);
// if the button is pressed
if (buttonval_2 == LOW) {
// turn the motor on
// To go forward, use digitalWrite to set AIN1 HIGH, AIN2 LOW
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
//Set speed using analogWrite to value between 0 and 255
analogWrite(PWMA, 50);
//turn LED green
analogWrite(Red_LED, 0);
analogWrite(Green_LED, 225);
analogWrite(Blue_LED, 0);
//Play The ABC's
// Meaure 1
NoteFunction(C, Q);
delay(100);
NoteFunction(C, Q);
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
// Measure 2
NoteFunction(A, Q);
delay(100);
NoteFunction(A, Q);
NoteFunction(G, H);
// Measure 3
NoteFunction(F, Q);
delay(100);
NoteFunction(F, Q);
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
// Measure 4
NoteFunction(D, EI);
delay(100);
NoteFunction(D, EI);
delay(100);
NoteFunction(D, EI);
delay(100);
NoteFunction(D, EI);
delay(100);
NoteFunction(C, H);
// Measure 5
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
NoteFunction(F, H);
// Measure 6
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
NoteFunction(D, H);
// Measure 7
NoteFunction(G, EI);
delay(100);
NoteFunction(G, EI);
delay(100);
NoteFunction(G, Q);
NoteFunction(F, H);
// Measure 8
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
NoteFunction(D, H);
// Measure 9
NoteFunction(C, Q);
delay(100);
NoteFunction(C, Q);
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
// Measure 10
NoteFunction(A, Q);
delay(100);
NoteFunction(A, Q);
NoteFunction(G, H);
// Measure 11
NoteFunction(F, Q);
delay(100);
NoteFunction(F, Q);
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
// Measure 12
NoteFunction(D, Q);
delay(100);
NoteFunction(D, Q);
NoteFunction(C, H);
//turn motor off
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 0);
//turn LED off
analogWrite(Red_LED, 0);
analogWrite(Green_LED, 0);
analogWrite(Blue_LED, 0);
}
//read button 3
buttonval_3 = digitalRead(Button_3);
// if the button is pressed
if (buttonval_3 == LOW) {
// turn the motor on
// To go forward, use digitalWrite to set AIN1 HIGH, AIN2 LOW
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
//Set speed using analogWrite to value between 0 and 255
analogWrite(PWMA, 50);
//turn LED red
analogWrite(Red_LED, 225);
analogWrite(Green_LED, 0);
analogWrite(Blue_LED, 0);
// Play Old McDonald Had a Farm
// Measure 1
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
delay(100);
NoteFunction(D, Q);
// Measure 2
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
NoteFunction(D, H);
// Measure 3
NoteFunction(B, Q);
delay(100);
NoteFunction(B, Q);
NoteFunction(A, Q);
delay(100);
NoteFunction(A, Q);
// Measure 4
NoteFunction(G, H);
delay(Q);
NoteFunction(D, Q);
// Measure 5
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
delay(100);
NoteFunction(G, Q);
delay(100);
NoteFunction(D, Q);
// Measure 6
NoteFunction(E, Q);
delay(100);
NoteFunction(E, Q);
NoteFunction(D, H);
// Measure 7
NoteFunction(B, Q);
delay(100);
NoteFunction(B, Q);
NoteFunction(A, Q);
delay(100);
NoteFunction(A, Q);
// Measure 8
NoteFunction(G, H);
//turn motor off
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 0);
//turn LED off
analogWrite(Red_LED, 0);
analogWrite(Green_LED, 0);
analogWrite(Blue_LED, 0);
}
}
// Function to play different notes
void NoteFunction(int Note, int Type){
tone(5,Note,Type);
delay(Type);
}
Video Explaining Completed Project
Below is a link to a video where we discuss how we created our project.
https://drive.google.com/file/d/1nB04TGOraOs8rQcVavBG7xhYyVCDTsLJ/view?usp=drive_link