loading

Step 4: The Code

The code relies on a Fast Fourier Transform Library, which can be found here.  The FFT, in short breaks down the audio signal into 14 frequency bands and from that this project continues with the lowest.  The bass...  Then some if-else statements to light up specific LEDs.

Here is the code as in Arduino 1.0.1

//
//  Beat Sync
//  A music visualiztion device.
//  Created by
//  Carl Smith
//  penguinmagic@hotmail.com
//

#include <fix_fft.h>

int led[] = {5,6,7,8,9,10,11,12};

int x = 0;

char im[128], data[128];

char data_avgs[14];

int i=0,val;

#define AUDIOPIN 3

void setup()
{
  for (int i = 0; i <8; i++)
  {
    pinMode(led[i], OUTPUT);
  }
  Serial.begin(9600);

}

void loop()
{
  for (i=0; i < 128; i++){                                   
    val = analogRead(AUDIOPIN);                                  
    data[i] = val;                                     
    im[i] = 0;                                                   
  };

  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 bars together
  for (i=0; i<14; i++) {
    data_avgs[i] = data[i*4] + data[i*4 + 1] + data[i*4 + 2] + data[i*4 + 3];   // average together
  
    data_avgs[i] = map(data_avgs[i], 0, 30, 0, 9);                              // remap values for LoL
  }
  int value = data_avgs[0];//0 for bass
  ledArray(value);
}
void ledArray(int input)
{
  //
  if (input > 8)
  {
     for (int i = 0; i <8; i++)
     {
       digitalWrite(led[i], HIGH);
     }
  }
  else if (input > 7)
  {
     for (int i = 0; i <7; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 7; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 6)
  {
     for (int i = 0; i <6; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 6; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 5)
  {
     for (int i = 0; i <5; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 5; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 4)
  {
     for (int i = 0; i <4; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 4; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 3)
  {
     for (int i = 0; i <3; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 3; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 2)
  {
     for (int i = 0; i <2; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 2; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else if (input > 1)
  {
     for (int i = 0; i <1; i++)
     {
       digitalWrite(led[i], HIGH);
     }
     for (int i = 1; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
  else
  {
    for (int i = 0; i <8; i++)
     {
       digitalWrite(led[i], LOW);
     }
  }
}
<p>I don't know how to install the fix_fft.h library! HELP</p>
<p>value of 8 resistors??</p>
<p>fatal error: fix_fft: no such file or directory.. can someone help how can i fix this error?</p>
<p>You have to install the fix_fft.h library. (https://www.arduino.cc/en/Guide/Libraries). After that, in your sketch, you will need to select &quot;Include Library&quot; under the &quot;Sketch&quot; menu. Then pick the fix_fft library you installed.</p>
<p>Nice, but i have to use a jack as the input;it is possible to use instead a microphone? like the tiny one you can find everywhere? xD</p><p>(sorry for my english, i'm italian)</p>
<p>can i ask you about the audio part? I don't really understand about audio input/output. How can I input music signal to arduino? Do I must have audio splitter? And I don't know about connecting between ipod and 3.5mm audio jack.</p>
why the sketch not working on me ???
<p>im having the same problem...what to do?</p>
it said,&quot;fix_fft was not declarated&quot; , I already add the fix_fft.h . I have no Idea why this happen
having same problem. did you ever figure it out?
I have only been able to use the Arduino_22 verison with the fix_fft library. Hope that helps!
having trouble with fix_fft.h header file. how is it included in the library? How do I create an &quot;.h&quot; file?
Awesome project !!!
what if I wanna change the channel, instead of using the bass frecuency, what line of the code should I change?, It's a great project btw.
<em>int value = data_avgs[0]; //0 for bass</em><br> The data_avgs[ ] array becomes 14 different frequencies ranging from about 80Hz (the bass) to 12000Hz (the highest treble). So if you change the &quot;0&quot; to a &quot;7&quot; or so, you will get the higher frequencies. &nbsp;Thanks!&nbsp;
thanks!... las doubt jaja, If i connected a microphone instead of the 2.5 plug, do you think it'd work as fine as it does now?
You really need an amplifier circuit for the mircophone, i've been working on a code and a circuit to make it possible. This is one option i've been trying https://www.sparkfun.com/products/9964? please vote for this project on the LED contest! and rate it! :)
Nice project, I did a similar thing using a band-pass filter instead of a FFT (hardware instead of software) <br> <br>Just a tip for the code, you should be able to write: <br> <br>void ledArray (int input) { <br> <br> for (int i = 0; i &lt; input; i++) <br> { <br> digitalWrite(led[i], HIGH); <br> } <br> <br>for (int i = input; i &lt;8; i++) <br> { <br> digitalWrite(led[i], LOW); <br> } <br>} <br> <br>and save yourself some if statements!
That's a good idea. In my latest code I used a switch statement which made it much simpler too! Thanks for your comment.

About This Instructable

31,377views

62favorites

License:

More by akcarl:Simple Hydroponic System Tiny Timer (T^2 for short) Beat Sync 
Add instructable to: