Saturday, August 2, 2014

Music Controlled LED Strip (Arduino Code)

I swear this wasn't filmed with a potato, I have awful bandwidth. 
Song credit: The Yeah Yeah Yeah's - Heads Will Roll

I finally got a chance to clean up some of the test code I wrote for my LED strip controller, which can be found here.  With this code and my LED strip controller circuit, you can use an Arduino to control a 5050 LED strip so that it changes colors in response to music.  I have it set to pulse and fade through a combination of red and blue hues in response to changes in volume.

I sought to create my lighting effects solely through code, and simply attached my audio signal straight to analog pin 3 on my Arduino Uno. I'm able to sense my signal through the use of the analogRead() command.  The audio signal that I feed my Arduino changes in analog value based on a couple of factors.  The Arduino sees this signal as a voltage source, and reads it as a value between 0 and 1024.  Changing the volume of the input source and also variations in the rhythm and beat of the music cause this voltage value to fluctuate. For example, when I'm listening to house music and the bass drops, the volume rises and the voltage of the signal will rise up sharply.  I can detect this rise, and change the color of my LED strip based on it.

My code is as follows:

#define REDPIN 5    //define what PWM pins are connected to the LED strip
#define GREENPIN 3
#define BLUEPIN 6

int b = 100;        //define the variables that will be used to control red, green, and blue.  
int r = 0;          //these values don't matter too much
int g = 0;

void setup() {
  pinMode(REDPIN, OUTPUT);   //set the three PWM pins for output
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  
  pinMode(A3, INPUT);  //set Analog pin 3 up for the audio signal
}
void loop() {
  
   analogWrite(BLUEPIN, b);  //change the color of the LED strip based on the values of r, g, and b.
   analogWrite(REDPIN, r);
   analogWrite(GREENPIN, g);
    
  //using just my ipod, the max signal I get into A3 is 400 or so.  If I had a signal that could reach 5v...
  //... the input values I would get would range between 0 and 1024
  
 if (analogRead(3)>325){ //if the signal from my ipod is greater in value than 325
   r=255; //strobe white
   b=255; 
   g=255;
   }
 else {                     //otherwise...
   if (analogRead(3)>200) { //if the signal is greater than 200
    r=(r+100); //increase the red and blue hues of the strip
    b=(b+50);
    }
   
   else { //if the signal from the ipod is below 200
     if (r>0 & b>0){ //and the values of red and blue are greater than 0
     r=(r-25); //decrease each slowly (this creates a fading effect after each beat)
     b=(b-10);
     }
     else { //if the values of red and blue fall below 0
     r=0; //set them all equal to 0
     b=0; //this is necessary to prevent each color from cycling through its min->max brightness
     g=0;
     }
        }
      }

In the beginning of my sketch, I create the variables r, g, and b and define which digital pwm pins I'll be using to control each color of the LED strip.  In my setup loop, I declare that the pins I'll be using to control the colors of the strip are for output, and that I'll be using analog pin 3 for my input. The main loop of my code is the most important part. Here I tell the Arduino to pulse the LED strip's red and blue colors based on the volume of my audio input. I also tell it to flash white when a high pulse in volume is detected.  This way, my strip will pulse red, blue, and all colors in between to music at normal volume, and flash white when peaks in the volume are detected. 

In order to change the colors produced by this code, change around the values of rg, and b, as well as changing the ways that the values of each of these variables are manipulated. Or just replace them with each other! The possibilities here really are endless. I found that varying one or two colors in response to the volume looked much better than changing three, and that fading each color rather than pulsing it made things more visually appealing.

There are many improvements that could be made to this LED strip controller when it comes to my audio input. In order to get accurate responses to changes in my music, the Arduino must be able to act as quickly as possible. Otherwise the LED strip will appear to respond to nothing, or will respond erratically to an input. I feel as if I've found a happy medium with the code I present here. I have attempted to make my code run as fast as possible by avoiding creating a variable for the volume of my music and simply using the analogRead() command instead.  I have also sped things up by not including any type of serial debugging, such as writing the value of my music's volume to the serial port. 

No comments:

Post a Comment