Skip to content

Instantly share code, notes, and snippets.

@domantascibas
Last active December 14, 2015 11:19
Show Gist options
  • Save domantascibas/5078633 to your computer and use it in GitHub Desktop.
Save domantascibas/5078633 to your computer and use it in GitHub Desktop.
The code to synchronize an LED bar to music using an Arduino.
//Arduino Music Lights v1
//by paplaukias
//
//uses the FFT library which can be found on http://arduino.cc/forum/index.php/topic,38153.0.html
//
//[email protected]
//http://www.paplaukias.co.uk
#include <fix_fft.h>
//set AnalogPin for audio input
#define AUDIOPIN 0
//set the sensitivity for the bars
#define HIGHEST 5
char im[128], data[128];
char data_avgs[8];
int i=0, val, bass;
//shift register pins
int latchPin = 3;
int clockPin = 4;
int dataPin = 5;
//explicitly set the LED output
int level[9] = {0b00000000, 0b00000001, 0b00000011, 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111, 0b11111111};
void setup(){
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//read data from the audio input and store it in an array
for (i=0; i < 128; i++){
val = analogRead(AUDIOPIN);
data[i] = val;
im[i] = 0;
};
//use the FFT to convert audio data from
//a voltage domain to a frequency domain
fix_fft(data, im, 7, 0);
for (i=0; i<64; i++){
data[i] = sqrt(data[i] * data[i] + im[i] * im[i]); // this gets the absolute value of the values in the
//array, so we're only dealing with positive numbers
};
//average a couple of frequencies together
data_avgs[0] = data[0] + data[1]; //average together
data_avgs[0] = map(data_avgs[0], 0, HIGHEST, 0, 8); //remap values for the LED bar
//check that the value doesn't exceed 8
while(data_avgs[0] > 8){
data_avgs[0] = data_avgs[0] - 8;
}
int bass = data_avgs[0];
//send the data you want to display to the shift register
shiftOut(dataPin, clockPin, MSBFIRST, level[bass]);
//output the data written to the shift register
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
delay(15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment