I am making an interactive light using Arduino uno, a RGB LED and without the ethernet shield.My aim is to make it flash with music beats.I have been reading various blogs by other makers and i have short-listed few ways of doing this.

I am making this lamp using the Processing and Minim audio library.In the previous blog i have discussed about this minim library.Using this library you can make BeatDetector and BeatListner code that you would be running in processing sketch.

Programming Arduino

We won't be writing any separate codes for Arduino IDE and Processing IDE.All we need to do is to load firmata on Arduino like this go to Arduino-->File-->Examples--->Firmata--->OldStandardFirmata.Upload that sketch on Arduino uno board.

Working with Processing

Follow these steps while writing code in Processing :

Import libraries: Make sure you have imported the following libraries as below:-

import processing.serial.*;// for serial communication with arduino board covered in very first blogimport ddf.minim.*; //import minim libraryimport ddf.minim.analysis.*;import cc.arduino.*;//since we won't be writing program for arduiono to trigger our leds so we need to import this lib as wellimport org.firmata.*;

Creating Objects :Now moving onto next part that is creating objects to access the methods within those libraries.

Minim minim; //Minim objectAudioPlayer song; // AudioPLayer library in minim using song as objectBeatDetect beat; //BeatDetect is a class part of minim analysisBeatListener bl; //class BeatListner implements AudioListner from minim libraryArduino arduino; // arduino object to access the led pins for controlling them

To use this class, inside of draw() you must first call detect(), passing the AudioBuffer you want to analyse. You may then use the isXXX functions to find out what beats have occurred in that frame. For example, you might use isKick() to cause a particular combination of led to pulse.BeatDetect has two modes: Sound energy tracking and Frequency energy tracking.

In sound energy mode, the level of the buffer, as returned bylevel(), is used as the instant energy in each frame. Beats, then, are spikes in this value, relative to the previous one second of sound.

In frequency energy mode, the same process is used but instead of tracking the level of the buffer, an FFT is used to obtain a spectrum, which is then divided into average bands using logAverages(), and each of these bands is tracked individually. The result is that it is possible to track sounds that occur in different parts of the frequency spectrum independently (like the kick drum and snare drum).In sound energy mode you use isOnset() to query the algorithm and in frequency energy mode you use isOnset(int i), isKick(), isSnare(), andisRange() to query particular frequnecy bands or ranges of frequency bands. It should be noted that isKick(), isSnare(), and isHat() merely callisRange() with values determined by testing the algorithm against music with a heavy beat and they may not be appropriate for all kinds of music. If you find they are performing poorly with your music, you should use isRange() directly to locate the bands that provide the most meaningful information for you.

BeatWrite Code

/** * This sketch demonstrates how to use the BeatDetect object in FREQ_ENERGY mode.<br /> * You can use <code>isKick</code>, <code>isSnare</code>, </code>isHat</code>, <code>isRange</code>, * and <code>isOnset(int)</code> to track whatever kind of beats you are looking to track, they will report * true or false based on the state of the analysis. To "tick" the analysis you must call <code>detect</code> * with successive buffers of audio. You can do this inside of <code>draw</code>, but you are likely to miss some * audio buffers if you do this. The sketch implements an <code>AudioListener</code> called <code>BeatListener</code> * so that it can call <code>detect</code> on every buffer of audio processed by the system without repeating a buffer * or missing one. * <p> * This sketch plays an entire song so it may be a little slow to load. */

void setup() { size(512, 200, P3D); minim = new Minim(this); arduino = new Arduino(this, Arduino.list()[2], 9600); song = minim.loadFile("Uptown Funk.mp3", 2048); song.play();// a beat detection object that is FREQ_ENERGY mode that // expects buffers the length of song's buffer size // and samples captured at songs's sample rate beat = new BeatDetect(song.bufferSize(), song.sampleRate());// set the sensitivity to 300 milliseconds // After a beat has been detected, the algorithm will wait for 300 milliseconds // before allowing another beat to be reported. You can use this to dampen the // algorithm if it is giving too many false-positives. The default value is 10, // which is essentially no damping. If you try to set the sensitivity to a negative value, // an error will be reported and it will be set to 10 instead. beat.setSensitivity(400); kickSize = snareSize = hatSize = 16;// make a new beat listener, so that we won't miss any buffers for the analysis bl = new BeatListener(beat, song); textFont(createFont("Helvetica", 16)); textAlign(CENTER); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.pinMode(ledPin2, Arduino.OUTPUT); arduino.pinMode(ledPin3, Arduino.OUTPUT); }

void draw() { background(0); fill(255); if(beat.isKick()) { arduino.digitalWrite(ledPin, Arduino.HIGH); // set the LED on delay(100); arduino.digitalWrite(ledPin2, Arduino.HIGH);// set the LED on kickSize = 32; } if(beat.isSnare()) { arduino.digitalWrite(ledPin2, Arduino.HIGH); // set the LED on delay(100); arduino.digitalWrite(ledPin3, Arduino.HIGH);// set the LED on snareSize = 32; } if(beat.isHat()) { arduino.digitalWrite(ledPin3, Arduino.HIGH); // set the LED on delay(100); arduino.digitalWrite(ledPin, Arduino.HIGH); // set the LED on hatSize = 32; } arduino.digitalWrite(ledPin, Arduino.LOW); // set the LED off arduino.digitalWrite(ledPin2, Arduino.LOW); // set the LED off arduino.digitalWrite(ledPin3, Arduino.LOW); // set the LED off textSize(kickSize); text("KICK", width/4, height/2); textSize(snareSize); text("SNARE", width/2, height/2); textSize(hatSize); text("HAT", 3*width/4, height/2); kickSize = constrain(kickSize * 0.95, 16, 32); snareSize = constrain(snareSize * 0.95, 16, 32); hatSize = constrain(hatSize * 0.95, 16, 32); }void stop() {// always close Minim audio classes when you are finished with them song.close();// always stop Minim before exiting minim.stop(); // this closes the sketch super.stop();}

If you have played mp3 files in visualizers of windows media players or any other media player then you must have came across various design patterns , visualization running in background.Processing IDE also provides such capabilities where you can create any graphical effects.The code focuses on fetching the mp3 file performing various file handling operations on it (reading,writing) also performing a spectral analysis or fast fourier transform (FFT) of the audio file.

Minim uses a BeatDetect class.The BeatDetect class allows us to analyze an audio stream for beats (rhythmic onsets). Beat Detection Algorithms by Frederic Patin describes beats in the following way:The human listening system determines the rhythm of music by detecting a pseudo periodical succession of beats. The signal which is intercepted by the ear contains a certain energy, this energy is converted into an electrical signal which the brain interprets. The more energy the sound transports, the louder the sound will seem. But a sound will be heard as a beat only if his energy is larger than the prior sound's energy history, that is to say if the brain detects a brutal variation in sound energy. Therefore if the ear intercepts a monotonous sound with sometimes big energy peaks it will detect beats, however, if you play a continuous loud sound you will not perceive any beats. Thus, the beats are big variations of sound energy.

Minim

Minim is an audio library that uses the JavaSound API , a bit of Tritonus, and Javazoom's MP3SPI to provide an easy to use audio library for people developing in the Processing environment. The philosophy behind the API is to make integrating audio into your sketches as simple as possible while still providing a reasonable amount of flexibility for more advanced users.

For beginners they can play around with QuickStart guide to Minim and load sketches in processing like one i did as an example.So follow these steps :-

In your processing sketch import the minim library

import ddf.minim.*;

To begin using minim with processing first make an object of minim class which can be used to load audio files or acquire inputs or outputslike this:-

Minim minim;AudioPlayer song;AudioInput input;

It is very easy to play an audio file in processing using minim all you need to make sure is that you have copied your desired audio (mp3 file in data folder of your sketch so that it can easily access the file).If your sketch is giving null pointer exception it is because the audio file is not in the sketch's folder make sure you have copied file properly.Below is a small code you can try :-

In above vizulaiser you can see the waveform generated the values returned by left.get() and right.get() will be between -1 and 1,so we need to scale them up to see the waveform.Note that if the file is MONO, left.get() and right.get() will return the same value

Frequency Spectrum

While your mp3 plays you can see and analyze your mp3 file's frequency spectrum using FFT class.The fft object created will be analysing the buffer size and the sample rate at which the analysis takes place.I would be using this code in next blog to make a beat listening light.

Just to cut short the length of the article i decided to break the making of Moodles- An interactive lamp into several parts in this way anyone can follow various modules in a step-wise fashion.In this post i am gonna discuss making RGB connection with arduino board and write code for fading it.Nothing fancy here but it's preliminary to next step of making RGB fade with music beats.If you can work your way around one LED you can do same for the array for LEDs or make a matrix of them and control the load at arduino i/o ports.

Hardware

The hardware part in this project is a simple RGB LED. I am using 4-leged flat head 5mm RGB LED common anode .Specifications:-

Max reverse current:20mA

RGB LED 5mm

Max Forward Voltage :3.0V-3.4V

Max Forward Current : 20mA

Supply Voltage :5V (From Arduino board)

While tinkering with the RGB i figured out that i was using common anode RGB Led so it's wiring will be little different from that of common cathode.The 4-leged LED with the longest leg is our common anode and we will be connecting it to +5V power supply from our Arduino board.Rest all legs are different colours(Red,Blue and Green) for each colour there is different value of resistance(200-330 ohms) required.There will be three different resistors for each colour and one leg of resistor will be connected to the output pins on Arduino board(Generally they're 9,10 & 11).Follow this simple rule in case you get confused in common anode and common cathode led wiring:

A RGB Common Anode LED should have it's longest leg (leg 2) connected to the 5V pin on your Arduino (Current sink)

A RGB Common Cathode LED should have it's longest leg (leg 2) connected to the ground pin on your Arduino (Current source)

Schematics

This simulation was made using 123D circuits https://123d.circuits.io/

PWM: 3, 5, 6, 9, 10, and 11 are available to us on are Arduino uno board and provide 8-bit PWM output with the analogWrite() function.I'm using pins 9,10 and 11. Using the PWN pins and analogWrite() allows access to wide spectrum of colours.Now im going to include analogWrite() and Random()to set a random brightness levels for each of the colours in the LED. The three colours will mix in different proportions (depending on their brightness) to make a wide variety of colours (255^3 = 16,581,375 possible colours).random(256); //returns a number between 0 and 255

The function is called "fader" and it takes two arguments. Each argument is separated by a comma and has a type declared in the first line of the function definition:

void fader(int colour1, int colour2)

We can see that both of the arguments fader accepts are ints, and we're using the names "colour1" and "colour2" as dummy variables for our function definition. The "void" refers to the data type that the function returns, since our function does not return anything (it simply executes commands), we set the return type to void.

WARNING !!

Important: don't fry your Arduino and/or your USB port!!Never make a circuit that demands a current greater than 50mA in each port. It could fry your Arduino and/or the USB of your computer.Never change the wiring of the circuit while connected to USB and/or to an external source.

After painstakingly completing the basics of both Arduino and Processing .Started to figure out how to make these two communicate with each other.Arduino is a micro-controller with elaborated sensor i/o ports it lacks functionality of storing , retrieving data that is essential part of Internet Of Things.Therefore i came across the Processing IDE.

What is Processing ??

Processing is an open source programming language and development environment being accepted by visual arts and visual literacy communities since 2001.It brought technology and art together under one roof for the tinkerers.Useful when you want those other computers to "talk" with an Arduino, for instance to display or save some data collected by the Arduino.

In this tutorial you will learn:

How to send data from Arduino to Processing over the serial port

How to receive data from Arduino in Processing

How to send data from Processing to Arduino

How to receive data from Processing in Arduino

How to write a serial ‘handshake’ between Arduino and Processing to control data flow

Arduino Serial Communication

I'm skipping the part of getting started with Arduinoyou can check about that by clicking on the link.I am assuming you have installed Arduino IDE , a white sketch like this one appears when you run Arduino IDE.

This is a basic code for setting up serial communication between your computer and the arduino development board which houses an ATMEGA 328 micro-controller that communicates with our computer at a baud rate of 9600.For instance you can understand baud rate as the rate at which we are sending data to our computer.

Note : If you're using mac then the serial port would be like this /dev/tty.usbmodem (for the Uno or Mega 2560) or /dev/tty.usbserial (for older boards)

After our setup() method, we need a method called loop(), which is going to repeat over and over as long as our program is running.In this method we will make Arduino communicate with our computer by sending a string "Hello World" every 100 millisecond. The code for loop () is below.

Now to make it work just plug in your arduino board select your board type (under Tools -> Board Type) and your Serial port (under Tools -> Serial Port) and hit the ‘upload’ button to load your code onto the Arduino.We are moving to the next part where processing will detect the data being sent from arduino over serial comm port. For that you should have Processing IDE downloaded.

Processing to receive and detect what Arduino sent

The Processing IDE looks very much similar to Arduino IDE and it also has only two methods void setup () and void draw (). Processing IDE comes with a Serial library to facilitate such communication.Once we have an open sketch, our first step is to import the Serial library. Go to Sketch->Import Library->Serial, as shown below:

You should now see a line like import processing.serial.*;at the top of your sketch. Just like the image below.Underneath our import statement we need to declare some global variables. Add these two lines beneath the import statement:

In order to listen to any serial communication we have to get a Serial object (we call it myPort but you can it whatever you like), which lets us listen in on a serial port on our computer for any incoming data. We also need a variable to recieve the actual data coming in. In this case, since we’re sending a String (the sequence of characters ‘Hello, World!’) from Arduino, we want to receive a String in Processing.For oursetup()method in Processing, we’re going to find the serial port our Arduino is connected to and set up our Serial object to listen to that port.

}In order to avoid any confusions put 9600 baud rate in the last argument of our Serial object so that it matches with the baud rate of Arduino as mentioned in Serial.begin(9600) statement (Arduino sketch).

In our draw() loop, we’re going to listen in on our Serial port and when we get something, stick that something in our val variable and print it to the console (that black area at the bottom of your Processing sketch).

void draw(){ if ( myPort.available() > 0) { // If data is available, val = myPort.readStringUntil('\n'); // read it and store it in val } println(val); //print it out in the console}

If you hit the ‘run’ button (and your Arduino is plugged ), you should see a little window pop-up, and after a sec you should see `Hello, World!‘ appear in the Processing console. Over and over. Like this:

Now we have successfully achieved Arduino to Processing communication now its time for flip side to initiate the same procedure and setup a communication from Processing to Arduino.

Processing talks back to Arduino

There are no major changes in processing sketch.First import the Serial library and declare the global serial object varibale and in our setup()method we find our port and initialize Serial communication on that port with our Serial variable at 9600 baud rate. We’re also going to use the size()command, to give us a little window to click in, which will trigger our sketch to send something over the Serial port to Arduino.

In our draw() loop, we send whatever we want over the serial port by using the write method from the Processing Serial library. For this sketch, we will send a ‘1’ whenever we click our mouse in the Processing window. We’ll also print it out on the console, just to see that we’re actually sending something. If we aren’t clicking we’ll send a ‘0’ instead.

Catching 1's at Arduino end

In order to detect the incoming 1s from Processing at communication port of Arduino we need external hardware a LED at pin 13 (Say) which will glow as it encounters "1 ".At the top of our Arduino sketch, we need two global variables - one for holding the data coming from Processing, and another to tell Arduino which pin our LED is hooked up to.

char val; // Data received from the serial portint ledPin =13; // Set the pin to digital I/O 13Next, in our setup() method, we’ll set the LED pin to an output, since we’re powering an LED, and we’ll start Serial communication at 9600 baud rate.

voidsetup() { pinMode(ledPin, OUTPUT); // Set pin as OUTPUT Serial.begin(9600); // Start serial communication at 9600 bps }Finally, in the loop() method, we’ll look at the incoming serial data. If we see a ‘1’, we set the LED to HIGH (or ON), and if we don’t (e.g. we see a ‘0’ instead), we turn the LED OFF. At the end of the loop, we put in a small delay to help the Arduino keep up with the serial stream.

voidloop() { if (Serial.available()) { // If data is available to read val = Serial.read(); // read it and store it in val } if (val =='1') { // If 1 was receiveddigitalWrite(ledPin, HIGH); // turn the LED on } else{ digitalWrite(ledPin, LOW); // otherwise turn it off } delay(10); // Wait 10 milliseconds for next reading}

This is how the code would look in Arduino sketch.Now run the code in Processing sketch and click anywhere in the canvas the LED on 13th pin will glow when you will click.Now we can communicate via processing with Arduino.

Handshaking

So far we were doing it in half duplex mode one sending and another one listening then repeating vice versa.But we want both Processing and Arduino to communicate simultaneously this is called Serial Handshaking i.e full duplex mode.In this part, we’re going to combine our two previous examples in such a way that Processing can both receive ‘Hello, world!’ from Arduino and send a 1 back to Arduino to toggle an LED. Of course, this also means that Arduino has to be able to send ‘Hello, world!’ while listening for a 1 from Processing. Let's get started.Again like the previous code we need a variable to hold data coming from serial port and a variable for state of led.In our code we only listen for 1’s, and each time we hear a ‘1’ we toggle the LED on or off. To do this we added a boolean (true or false) variable for the HIGH or LOW state of our LED. This means we don’t have to constantly send a 1 or 0 from Processing, which frees up our serial port , quite a bit.There is introduction of this new function establishContact();

In the loop() function we have made changes in code for LED as now it will toggle according to '!' that means every time it encounters a '!' the ledState will set the boolean to the opposite of what it was before (so LOW becomes HIGH or vice-versa). We will send "Hello,World ! "only when we see a 1 coming in.T lherefore its now placed in else loop.

void loop(){ if (Serial.available() > 0) { // If data is available to read, val = Serial.read(); // read it and store it in val

if(val == '1') //if we get a 1 { ledState = !ledState; //flip the ledState digitalWrite(ledPin, ledState); } delay(100); } else { Serial.println("Hello, world!"); //send back a hello world delay(50); }}Now we get to that establishContact() function we had put in our setup() method before. This function just sends out a string (the same one we’ll need to look for in Processing) to see if it hears anything back - indicating that Processing is ready to receive data.

Now that we are done with Arduino part , it's time for amendments in Processing sketch.The code is pretty much similar but minor changes and introduction of serialEvent() method which basically gets called every time we see a specific character in our serial buffer which acts as a delimiter as it tells processing - We're done with this chunk of data which in our case is Hello,World !In the beginning of our code there is introduction of a boolean firstContact to mark us when we made our very much first contact with Arduino.The code snippets are below:

Our setup() function is the same as it was for our serial write program, except we added the myPort.bufferUntil('\n'); line. This let’s us store the incoming data into a buffer, until we see a specific character we’re looking for. In this case, it’s a carriage return (\n) because we sent a Serial.printlnfrom Arduino. The ‘ln’ at the end means the String is terminated with a carriage return, so we know that’ll be the last thing we see.

If you’ve got your Arduino code finished and loaded onto your board, try running this sketch. You should see ‘Hello, world!’ coming in on the console, and when you click in the Processing window, you should see the LED on pin 13 turn on and off. Success!

Consolidated Processing code:import processing.serial.*; // Import the Serial librarySerial myPort; //The Serial port objectString val;// Since we are doing a serial handshaking so we need to know// if we heard from microcontroller or not ?boolean firstContact = false;