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)));
}
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