venerdì 28 ottobre 2011

Arduino and processing led blinking following music rythm

Hi,
Today I'm writing about a simple project with Arduino and Porcessing to blink 5 LEDs following the music rythm.
To do it we need an audio librari for processing called minim, (created by Damien di Fede) and we need the Firmata library for communicating to Arduino.
For this project you need:
-Arduino board
-5 LEDs (1,8-2v)
-5 220ohm resistors
-breadboard
-some wires
-arduino library for processing (http://www.arduino.cc/playground/Interfacing/Processing)
-minim audio library(http://code.compartmental.net/minim/distro/minim-2.0.2-lib.zip)

First of all prepare the simple connections on your breadboard following this image:






Then open the arduino software and upload the standardFirmata sketch (File>examples>Firmata>StandardFirmata).
Now, before writing the processing code I'd like to tell you something aboute the libraries we're going to use: Firmata is a library made for communciating between processing and Arduino. When you run Firmata in arduino, you can call arduino functions from processing using the arduino library for processing.
The minim library is an audio library for processing, I think it's the best library about audio and sound for processing because it's easy to use but have lots of functions. For the reference chek this link: http://code.compartmental.net/tools/minim/.
Now download the libraries and write the following code in a processing sketch:


import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*; //Import ddf.minim library
import processing.serial.*; //Import serial library
import cc.arduino.*; //Import Arduino library

Arduino arduino;
Minim minim;
AudioPlayer song;
FFT fft;
//I create the objects

//To play another song change the song_file value
String song_file = "song.mp3";
int xmax = 600; //window width
int ymax = 300;//window height

void setup()
{
  size(xmax, ymax);
  minim = new Minim(this);
  arduino = new Arduino(this, Arduino.list()[1], 57600);
  song = minim.loadFile(song_file);
  song.play();
  fft = new FFT(song.bufferSize(), song.sampleRate());
  // in this function I create the minim object, I start
  //communicating with Arduino,I load the song and I play it and
  // I start the Fast Fourier Transofrm
}

void draw()
{
  background(0);
  fft.forward(song.mix);
  stroke(127, 255, 0, 200); //I set the color of my lines
  for(int i = 10; i < fft.specSize(); i++){
    line(i, height-30, i, height - (30+fft.getFreq(i*10)));
    // I create lines that represent the amplitude
    // of each frequency.
    text("Min Freq", 10, height-10);
    text("Max Freq", fft.specSize(), height-10);
  }
  ledcontrol(); //I call the function for the arduino
}

void ledcontrol(){
  //In this function I use arduino analogWrite function
  // to write in PWM pins the amplitude
  // of five general frequency
  arduino.analogWrite(3, int(fft.getFreq(500)));
  arduino.analogWrite(5, int(fft.getFreq(400)));
  arduino.analogWrite(6, int(fft.getFreq(250)));
  arduino.analogWrite(9, int(fft.getFreq(150)));
  arduino.analogWrite(10, int(fft.getFreq(80)));
  }

The code is simple so I think it's not necessary to explain it.
You can download the project files (.zip) here: download.
Here is a test:

For more information about the project comment or send me an e-mail to damianoandre@gmail.com

Bye, Dami

13 commenti:

  1. I'm trying to do this for at least 13 LEDs how can i make it work? please help thanks

    RispondiElimina
  2. You have to change the ledcontrol() function, but the problem is that only Arduino Mega have 13 PWM pins so if you have another arduino board you cannot do this.
    If you haven't an Arduino Mega I suggest you tu use the pwm pins for the high frequencies and the normal digital pins to detect bass frequencies

    RispondiElimina
  3. I'm using a 'spark fun protosnap' (provided by my college) I have it on a bread board. Would i need to get another type of arduino? if yes , which one?.

    (sorry I'm new at this and I'm not the greatest working with this)

    RispondiElimina
  4. Hi, I think you're who comment my post on fritzing.org, I'll answer you by e-mail.
    If you are not him please comment and tell me that.

    RispondiElimina
  5. ive been doing this a couple of hour ago..after i downloaded processing etc.. there's nothng happend on the Leds..help..

    RispondiElimina
  6. Hi, probably your project doesn't function because the processing code can't connect to the arduino, the problem is in the code

    arduino = new Arduino(this, Arduino.list()[1], 57600);

    you have to change the variable in Arduino.list()[1]
    into the correct serial port, to view all available serial port print
    println(Arduino.list());
    If you have more problems please send me an e-mail to damianoandr@gmail.com

    RispondiElimina
  7. Where do you place the song.mp3?

    RispondiElimina
  8. or

    sketch folder>folder called "data"

    or in the sketch folder

    RispondiElimina
  9. Hi just a quick question,

    Do you now what's wrong if leds are not working, but there is no error message at all, and music and the visual window from processing work fine?

    I assume it's the connecting problem but I totally followed the picture above....

    thanks in advance!

    RispondiElimina
  10. there are two possible causes:
    1) check if led's are connected in the right way
    2) the leds' light is too low to be visble so try turning off all other light and using a more energic song

    If these methods don't work, try changing ledcontrol function to

    void ledcontrol(){
    arduino.analogWrite(3, int(fft.getFreq(500)*2));
    arduino.analogWrite(5, int(fft.getFreq(400)*2));
    arduino.analogWrite(6, int(fft.getFreq(250)*2));
    arduino.analogWrite(9, int(fft.getFreq(150)*2));
    arduino.analogWrite(10, int(fft.getFreq(80)*2));
    }

    RispondiElimina
  11. Hi again, Im the one asking for led problem.

    Just figured out that it wasnt the connecting problem.
    I have to use AnalogFirmata not the StandardFirmata to make it work.

    Probably it's just my problem, but I just share this in case.

    Thank you.

    RispondiElimina
  12. hi are you able to tell me if I can use this for all sounds coming from my computer (media player ect..)

    and could I use it in my car off the amplifier analog speaker input.

    thanks so much for your awesome code!!!!

    RispondiElimina
  13. Hi, I'm doing the same project, but, how do you connect it to the computer? I mean, how can you sincronize the music of the computer with the Arduino

    thank you

    RispondiElimina