<![CDATA[Sahiba Aggarwal - Blog]]>Wed, 30 Dec 2015 06:00:40 -0800EditMySite<![CDATA[Moodles Part 1- Flashing RGB to music]]>Sun, 24 May 2015 21:57:18 GMThttp://sahibaaggarwal.weebly.com/blog/making-rgb-flash-to-musicI 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.

Flashing the LED to music can be done by following ways:-


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 blog
import ddf.minim.*;  //import minim library
import 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 well
import org.firmata.*; 

  • Creating Objects :Now moving onto next part that is creating objects to access the methods within those libraries.
Minim minim;  //Minim object
AudioPlayer song; // AudioPLayer library in minim using song as object
BeatDetect beat;    //BeatDetect is a class part of minim analysis
BeatListener bl;    //class BeatListner implements AudioListner from minim library
Arduino arduino; // arduino object to access the led pins for controlling them

  • BeatDetect class :The BeatDetect class allows you to analyse an audio stream for beats (rhythmic onsets).

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.
  */

import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import cc.arduino.*;
import org.firmata.*;

Minim minim;
AudioPlayer song;
BeatDetect beat;
BeatListener bl;
Arduino arduino;

int ledPin =  9;    // LED connected to digital pin 12
int ledPin2 =  10;    // LED connected to digital pin 1
int ledPin3 =  11;    // LED connected to digital pin 0

float kickSize, snareSize, hatSize;

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();
}
The above image is made using the below code:


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

BeatListner Code

The details about this code can be found with Audio Player class of minim library

class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioPlayer source;
  
  BeatListener(BeatDetect beat, AudioPlayer source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }
  
  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }
  
  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}
]]>
<![CDATA[Working with Processing IDE and Minim library]]>Thu, 21 May 2015 12:19:05 GMThttp://sahibaaggarwal.weebly.com/blog/working-with-processing-ide-and-minim-libraryPicture
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. 

Some of the features of Minim:

  • AudioPlayer: Mono and Stereo playback of WAV, AIFF, AU, SND, and MP3 files.
  • AudioMetaData: An object filled with metadata about a file, such as ID3 tags.
  • AudioRecorder: Mono and Stereo audio recording either buffered or direct to disk.
  • AudioInput: Mono and Stereo input monitoring.
  • AudioOutput: Mono and Stereo sound synthesis.
  • FFT: perform a Fourier Transform on audio data to generate a frequency spectrum.
  • BeatDetect: a class for doing beat detection.
  • real-time synthesis framework based around unit generators, which we call UGens.


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 outputs
like 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 :-

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer song;
AudioInput input;

void setup()
{
 size(512,200,P3D);
 minim = new Minim(this);
 // this loads Infinity - Wicked Game Carolina Wallace Mashup Cover.mp3 from the data folder
 song =minim.loadFile("Infinity_Wicked_Game_Carolina_Wallace_Mashup_Cover.mp3");
 song.play();
}

void draw()
{
 background(0);
  stroke(255);
  
  // draw the waveforms
  // 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

  for(int i = 0; i < song.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, song.bufferSize(), 0, width );  //
map(value, start1, stop1, start2, stop2)
    float x2 = map( i+1, 0, song.bufferSize(), 0, width );
    line( x1, 50 + song.left.get(i)*50, x2, 50 + song.left.get(i+1)*50 );
    line( x1, 150 + song.right.get(i)*50, x2, 150 + song.right.get(i+1)*50 );
  }
}
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.
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer song;
FFT fft;
void setup()
{
 size(512,200);
 minim = new Minim(this);
 song = minim.loadFile("Moves Like Jagger.mp3",512);
 song.play();
 
 fft = new FFT(song.bufferSize(),song.sampleRate());
}

void draw()
{
 background(0);
 fft.forward(song.mix);
 stroke(255,0,0,128);
 for(int i=0;i<fft.specSize();i++){
  line(i,height,i,height-fft.getBand(i)*4);
 }
 
 stroke(255);
 for(int i = 0; i < song.left.size() - 1; i++)
  {
    line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
    line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
  }
}
Resource :
http://code.compartmental.net/tools/minim/quickstart/
]]>
<![CDATA[Making RGB LED diffuse to several colours´╗┐]]>Mon, 18 May 2015 17:06:51 GMThttp://sahibaaggarwal.weebly.com/blog/making-rgb-led-diffuse-to-several-coloursJust 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

Picture
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)

//RGB LED - test

//pin connections

int red = 9;
int green = 10;
int blue = 11;
void setup()

{
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}
void loop()

{
  for (int pinNum=9;pinNum<12;pinNum++)

         {
                digitalWrite(pinNum, HIGH);
                delay(500);
                digitalWrite(pinNum, LOW);
                delay(500);

        }
}

Schematics

Picture
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
//RGB LED - random colours

//pin connections

int red = 9;
int green = 10;
int blue = 11;
void setup(){
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}
void loop(){
 //pick a random colour

 analogWrite(red,random(256));
 analogWrite(blue,random(256));
 analogWrite(green,random(256));
 delay(1000); //wait one second 
}

---------------------------------------------------------------------------------------------------------------
The following sketch fades LED from red to green to blue to red to green and so on...

//RGB LED - random colours with diffusing colours

//pin connections

int red = 9;
int green = 10;
int blue = 11;
int brightness=0;
void setup(){
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}
void loop(){
for(brightness=0;brightness<256;brightness++){
    analogWrite(red,255-brightness);
    analogWrite(green,brightness);
    delay(10);
  }
 for(brightness=0;brightness<256;brightness++){
    analogWrite(green,255-brightness);
    analogWrite(blue,brightness);
    delay(10);
  }
 for(brightness=0;brightness<256;brightness++){
    analogWrite(blue,255-brightness);
    analogWrite(red,brightness);
    delay(10);
  }
}



NOTE: The code above is quite repetitive so we will make few changes and bring all the functionality of the above repetitive code into one fader code.


//RGB LED- fading between colours
//pin connections


int red=9;
int green =10;
int blue =11;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);

}

void loop() {
  fader(red,green);
  fader(green,blue);
  fader(blue,red);
}

void fader(int colour1, int colour2){
     for(int brightness=0;brightness<256;brightness++){
       analogWrite(colour1,255-brightness);
       analogWrite(colour2,brightness);
       delay(10);
     }
}

Explaining code written above:-
Let's follow line by line for the snippet


void fader(int colour1, int colour2){
for (int brightness=0;brightness<256;brightness++){
analogWrite(color1, 255-brightness);
analogWrite(color2, brightness);
delay(10);
}

}
 

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.



fader(red, green);
from the Arduino's loop(), the Arduino evaluates the fader function with colour1 = red and colour2 = green. 
Resource
  • http://www.instructables.com/id/Choosing-The-Resistor-To-Use-With-LEDs/
  • http://www.robotroom.com/Calculators/Resistor/Resistor-Color-Code-Calculator.aspx
  • http://www.instructables.com/id/RGB-LED-Tutorial-using-an-Arduino-RGBL/?ALLSTEPS
  • http://wiring.org.co/learning/basics/rgbled.html


Wanna try more ??
http://hackaday.com/2012/09/08/hacking-a-floating-rgb-led-decorative-ball/

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.
]]>
<![CDATA[Arduino with Processing IDE]]>Tue, 05 May 2015 19:14:14 GMThttp://sahibaaggarwal.weebly.com/blog/arduino-with-processing-idePicture
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 Arduino  you 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.


void setup() 
 { 
 //initialise serial communications at a 9600 baud rate 
   Serial.begin(9600);
 }

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.


void loop() 

//send 'Hello, world!' over the serial port 
Serial.println("Hello, world!"); 
//wait 100 milliseconds 
delay(100); 
}


This is how the code should look like on sketch.
 
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:


Serial myPort;  // Create object from Serial class 
String val;  // Data received from the serial port

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 our setup() 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.


void setup()
{
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600); 

}

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.


import processing.serial.*
 Serial myPort; // Create object from Serial class 
 void setup() 
 { 
  size(200,200); //make our canvas 200 x 200 pixels  
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600); 
}


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.

void draw() 
{
  if (mousePressed == true) 
  {    //if we clicked in the window
   myPort.write('1');   //send a 1
   println("1");   
  } else 
  {                    //otherwise
  myPort.write('0');
   println("0");  //send a 0
  }   
}



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 port 
int ledPin = 13; // Set the pin to digital I/O 13


Next, 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.

void setup() 

  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.

void loop() 

  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 received 
         digitalWrite(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();

char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
boolean ledState = LOW; //to toggle our LED
void setup() 
{
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
  establishContact();  // send a byte to establish contact until receiver responds 
}


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.

void establishContact() {
  while (Serial.available() <= 0) {
  Serial.println("A");   // send a capital A
  delay(300);
  }
}


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:

import processing.serial.*; //import the Serial library
 Serial myPort;  //the Serial port object
 String val;
// since we're doing serial handshaking, 
// we need to check if we've heard from the microcontroller

boolean firstContact = false;



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.println from 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.


void setup() {
  size(200, 200); //make our canvas 200 x 200 pixels big
  //  initialize your serial port and set the baud rate to 9600

  myPort = new Serial(this, Serial.list()[4], 9600);
  myPort.bufferUntil('\n'); 
}
Because we’re continuously sending data, our 
serialEvent() method now acts as our newdraw() loop, so we can leave it empty:
void draw() {
  //we can leave the draw method empty, 
  //because all our programming happens in the serialEvent (see below)

}


serialEvent(). Each time we see a carriage return this method gets called. We need to do a few things each time to keep things running smoothly.

void serialEvent( Serial myPort) {
//put the incoming data into a String - 
//the '\n' is our end delimiter indicating the end of a complete packet

val = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (val != null) {
  //trim whitespace and formatting characters (like carriage return)
  val = trim(val);
  println(val);

  //look for our 'A' string to start the handshake
  //if it's there, clear the buffer, and send a request for data

  if (firstContact == false) {
    if (val.equals("A")) {
      myPort.clear();
      firstContact = true;
      myPort.write("A");
      println("contact");
    }
  }
  else { //if we've already established contact, keep getting and parsing data
    println(val);

    if (mousePressed == true) 
    {                           //if we clicked in the window
      myPort.write('1');        //send a 1
      println("1");
    }

    // when you've parsed the data you have, ask for more:
    myPort.write("A");
    }
  }
}



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 library
Serial myPort; //The Serial port object
String val;
// Since we are doing a serial handshaking so we need to know
// if we heard from microcontroller or not ?

boolean firstContact = false;

void setup() {
  size(200, 200); //make our canvas 200 x 200 pixels big
  //  initialize your serial port and set the baud rate to 9600

  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.bufferUntil('\n'); 
}
void draw() {
  //we can leave the draw method empty, 
  //because all our programming happens in the serialEvent (see below)
}

void serialEvent( Serial myPort) {
//put the incoming data into a String - 
//the '\n' is our end delimiter indicating the end of a complete packet

val = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (val != null) {
  //trim whitespace and formatting characters (like carriage return)
  val = trim(val);
  println(val);

  //look for our 'A' string to start the handshake
  //if it's there, clear the buffer, and send a request for data

  if (firstContact == false) {
    if (val.equals("A")) {
      myPort.clear();
      firstContact = true;
      myPort.write("A");
      println("contact");
    }
  }
  else { //if we've already established contact, keep getting and parsing data
    println(val);

    if (mousePressed == true) 
    {                           //if we clicked in the window
      myPort.write('1');        //send a 1
      println("1");
    }

    // when you've parsed the data you have, ask for more:
    myPort.write("A");
    }
  }
}

Now, If you want to send over a number of sensor values, it’s a good idea to count how many bytes you’re expecting so you know how to properly parse out the sensor data.

Resource :

  • For Arduino : http://www.arduino.cc/en/Guide/HomePage
  • About Arduino UNO : http://www.arduino.cc/en/Main/ArduinoBoardUno
  • http://learning.codasign.com/index.php?title=Beginning_Processing_and_Arduino
  • http://playground.arduino.cc/Interfacing/Processing
]]>