Arduino Christmas Light With Music Sync

 by psychodot5415
xmas trails.jpg
xmas components.png
I came across two great instructables and decided to combine them.  http://www.instructables.com/id/Arduino-Christmas-Light-Controller/  takes eight lines of lights and uses code on the Arduino to "play" "Joy To The World".  I tested it using LEDs, and it is very accurate.  I wanted to make this project, but I wanted to have the lights synchronized to music.  So, I took a look at http://www.instructables.com/id/Yet-Another-Daft-Punk-Coffee-TableDisplay/.  It takes musical input and uses it to trigger a sequence of patterns on an LED matrix.  So, I followed the steps in the first project and added the musical input from the second.  Then, I modified the code to work with the eight trees of lights (see code below).  The good thing about this project is if you can't get the synchronization portion to work, you can still use what you have built to run the original Arduino Christmas Light Controller project.  I included two drawings -- the first is the copper trails of the PCB.  These are not to scale.  As the author of the first project pointed out, you use an etching permanent marker to draw the trails.  I recommend drilling the holes for the chip and relays first.  I found that I needed to erase with rubbing alcohol and re-draw some of the trails after drilling.  The second drawing is the component side of the board. 


#define SENSOR_PIN A0 // analog input pin for music
//void showScenario(int);
// trigger next scenario when music amplitude > threshold
// increase threshold to decrease sensitivity
// decrease threshold to increase sensitivity
int threshold = 5;
// debouncing - filter out consecutive triggers that are too close
// together to slow down the animation.  decrease this value
// to make it more sensitive to shorter beat intervals
int minBeatInterval = 1000;
int threshCrossed = 0;
int scenario = 1;
int cnt = 32768;
int tree1 = 6;
int tree2 = 7;
int tree3 = 8;
int tree4 = 9;
int tree5 = 2;
int tree6 = 3;
int tree7 = 4;
int tree8 = 5;
void setup()
{
pinMode(tree1, OUTPUT);
pinMode(tree2, OUTPUT);
pinMode(tree3, OUTPUT);
pinMode(tree4, OUTPUT);
pinMode(tree5, OUTPUT);
pinMode(tree6, OUTPUT);
pinMode(tree7, OUTPUT);
pinMode(tree8, OUTPUT);
digitalWrite(tree1,HIGH);
delay(1000);
digitalWrite(tree2,HIGH);
delay(1000);
digitalWrite(tree3,HIGH);
delay(1000);
digitalWrite(tree4,HIGH);
delay(1000);
digitalWrite(tree5,HIGH);
delay(1000);
digitalWrite(tree6,HIGH);
delay(1000);
digitalWrite(tree7,HIGH);
delay(1000);
digitalWrite(tree8,HIGH);
delay(1000);
digitalWrite(tree1,LOW);
digitalWrite(tree2,LOW);
digitalWrite(tree3,LOW);
digitalWrite(tree4,LOW);
digitalWrite(tree5,LOW);
digitalWrite(tree6,LOW);
digitalWrite(tree7,LOW);
digitalWrite(tree8,LOW);
Serial.begin(9600);
}
void showScenario(int s){
int theScenario;
theScenario = s;
 if (theScenario == 1){
digitalWrite(tree1, LOW);
digitalWrite(tree8, LOW);
digitalWrite(tree1, HIGH);
digitalWrite(tree3, HIGH);
}
if (theScenario == 2){
digitalWrite(tree1, LOW);
digitalWrite(tree3, LOW);
digitalWrite(tree6, HIGH);
digitalWrite(tree8, HIGH);
}
if (theScenario == 3){
digitalWrite(tree6, LOW);
digitalWrite(tree8, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree4, HIGH);
}
if (theScenario == 4){
digitalWrite(tree3, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree6, HIGH);  
}
if (theScenario == 5)
{digitalWrite(tree5, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree4, HIGH);
}
if (theScenario == 6){
digitalWrite(tree3, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree6, HIGH);  
}
if (theScenario == 7){
digitalWrite(tree5, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree1, HIGH);
digitalWrite(tree3, HIGH);
}
if (theScenario == 8){
digitalWrite(tree1, LOW);
digitalWrite(tree3, LOW);
digitalWrite(tree2, HIGH);
digitalWrite(tree4, HIGH); 
}
if (theScenario == 9){
digitalWrite(tree2, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree5, HIGH); 
}
if (theScenario == 10){
digitalWrite(tree3, LOW);
digitalWrite(tree5, LOW);
digitalWrite(tree4, HIGH);
digitalWrite(tree6, HIGH);
}
if (theScenario == 11){
digitalWrite(tree4, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree7, HIGH); 
}
if (theScenario == 12){
digitalWrite(tree5, LOW);
digitalWrite(tree7, LOW);
digitalWrite(tree6, HIGH);
digitalWrite(tree8, HIGH);
}
if (theScenario == 13){
digitalWrite(tree6, LOW);
digitalWrite(tree8, LOW);
digitalWrite(tree1, HIGH);
digitalWrite(tree4, HIGH);  
}
if (theScenario == 14){
digitalWrite(tree1, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree8, HIGH);
}
if (theScenario == 15){
digitalWrite(tree5, LOW);
digitalWrite(tree8, LOW);
digitalWrite(tree2, HIGH);
digitalWrite(tree4, HIGH); 
}
if (theScenario == 16){
digitalWrite(tree2, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree7, HIGH);
}
if (theScenario == 17){
digitalWrite(tree5, LOW);
digitalWrite(tree7, LOW);
digitalWrite(tree1, HIGH);
}
if (theScenario == 18){
digitalWrite(tree1, LOW);
digitalWrite(tree2, HIGH);
}
if (theScenario == 19){
digitalWrite(tree2, LOW);
digitalWrite(tree3, HIGH);
}
if (theScenario == 20){
digitalWrite(tree3, LOW);
digitalWrite(tree2, HIGH);
digitalWrite(tree4, HIGH);
}
if (theScenario == 21){
digitalWrite(tree2, LOW);
digitalWrite(tree4, LOW);
digitalWrite(tree3, HIGH);
}
if (theScenario == 22){
digitalWrite(tree3, LOW);
digitalWrite(tree4, HIGH);
}
if (theScenario == 23){
digitalWrite(tree4, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree5, HIGH); 
}
if (theScenario == 24){
digitalWrite(tree3, LOW);
digitalWrite(tree5, LOW);
digitalWrite(tree4, HIGH);
}
if (theScenario == 25){
digitalWrite(tree4, LOW);
digitalWrite(tree5, HIGH);
}
if (theScenario == 26){
digitalWrite(tree5, LOW);
digitalWrite(tree4, HIGH);
digitalWrite(tree6, HIGH); 
}
if (theScenario == 27){
digitalWrite(tree4, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree5, HIGH);
}
if (theScenario == 28){
digitalWrite(tree5, LOW);
digitalWrite(tree6, HIGH);
}
if (theScenario == 29){
digitalWrite(tree6, LOW);
digitalWrite(tree5, HIGH);
digitalWrite(tree7, HIGH); 
}
if (theScenario == 30){
digitalWrite(tree5, LOW);
digitalWrite(tree7, LOW);
digitalWrite(tree6, HIGH);
}
if (theScenario == 31){
digitalWrite(tree6, LOW);
digitalWrite(tree7, HIGH);
}
if (theScenario == 32){
digitalWrite(tree7, LOW);
digitalWrite(tree8, HIGH); 
}
if (theScenario == 33){
digitalWrite(tree8, LOW);
digitalWrite(tree4, HIGH);
digitalWrite(tree5, HIGH); 
}
if (theScenario == 34){
digitalWrite(tree4, LOW);
digitalWrite(tree5, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree6, HIGH); 
}
if (theScenario == 35){
digitalWrite(tree3, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree2, HIGH);
digitalWrite(tree7, HIGH); 
}
if (theScenario == 36){
digitalWrite(tree2, LOW);
digitalWrite(tree7, LOW);
digitalWrite(tree1, HIGH);
digitalWrite(tree8, HIGH); 
}
if (theScenario == 37){
digitalWrite(tree1, LOW);
digitalWrite(tree8, LOW);
digitalWrite(tree4, HIGH);
digitalWrite(tree5, HIGH); 
}
if (theScenario == 38){
digitalWrite(tree4, LOW);
digitalWrite(tree5, LOW);
digitalWrite(tree3, HIGH);
digitalWrite(tree6, HIGH); 
}
if (theScenario == 39){
digitalWrite(tree3, LOW);
digitalWrite(tree6, LOW);
digitalWrite(tree2, HIGH);
digitalWrite(tree7, HIGH); 
}
if (theScenario == 40){
digitalWrite(tree2, LOW);
digitalWrite(tree7, LOW);
digitalWrite(tree1, HIGH);
digitalWrite(tree8, HIGH); 
}
}void loop() {
int sensorValue = analogRead(SENSOR_PIN);
cnt++;
if (sensorValue > threshold) {
if (!threshCrossed && (cnt > minBeatInterval) ) {
showScenario(scenario);
scenario++;
}
if (scenario > 40){
scenario = 1;

threshCrossed = 1;
cnt = 0;

if (sensorValue < threshold){
threshCrossed = 0;
}
}








































































 
fibean says: Feb 23, 2012. 11:17 AM
A lot of DigitalWrite's! PRO programmers may want to use port registers for smaller code. (http://www.arduino.cc/en/Reference/PortManipulation)
psychodot5415 (author) in reply to fibeanFeb 24, 2012. 7:25 PM
This was my first arduino project. Interesting reading on port registers. Certainly an option.

fibean in reply to psychodot5415Feb 26, 2012. 11:07 AM
And btw you could use Switch/Case instead of IF's. Just ideas :D (http://arduino.cc/en/Reference/SwitchCase)
fibean in reply to psychodot5415Feb 26, 2012. 11:05 AM
Ok, just an idea :3 Nice project man. The port registers are more difficult to debug, I would not recommend to write the first code with them: if something go wrong you will take a lot of time to debug. But after the v1.0 of the code you can convert to the port registers, it will make the code smaller as I already said and you can add more code in the future if you want to :D
Blakamiss48 says: Nov 4, 2011. 1:59 PM
Great PCB design. Are you using a 4 pin relay or a 5 pin relay. Or is it the same as the one used in this project http://www.instructables.com/id/Arduino-Christmas-Light-Controller/. Im just confused on which pin the 120V is conneced to on each relay . Thanks.
psychodot5415 (author) in reply to Blakamiss48Nov 6, 2011. 6:46 PM
It is the same relay from the other project. It is a five pin, but only four are actually used. The 120v input goes to the middle of the three pins on the one side. The pin is marked "1" and "com". The 120v leaves the relay through the pin marked "3" and "no" ("normally open"). That pin is on the opposite side of the relay. The 5v is connected to pins "2" and "3" which sandwich pin "1". On my relay, pins 2 and 3 have lines leading to a squiggly line, representing a coil. Pin "4" ("nc" = "normally closed") is not used. It is soldered to a trail that goes nowhere. Hope this helps.
Blakamiss48 in reply to psychodot5415Nov 6, 2011. 7:25 PM
Ok excellent. Thanks for your help!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!