on Feb 23rd, 2012A conversation between Processing and Arduino

Goal: To learn how to use serial communication to send information from Processing to Arduino.

Part One: Virtual light switch
Using a virtual button in Processing, an LED can be turned on and off using Arduino. This is done by sending one of two ASCII characters to Arduino through the serial port when a box registers a mouse hover over in the Processing canvas. When an ‘L’ is sent, the LED is off; when an ‘H’ is sent, the LED is on.

Part Two: Physical Mouse Speedometer
Based on the speed of mouse, a series of LEDs acts as a VU meter. This is done by sending various 5 ASCII (‘A’-‘E’) characters corresponding to different speed levels. For an alternate project, this basic idea can actuate a Servo motor to produce a more precise speedometer.

What you will need:

Arduino, installed
Processing, installed
5 LEDs (preferably 2 green, 2 yellow, 1 red)
1 Breadboard
1 USB serial cable
Various wires to connect Arduino, LEDs, and breadboard


Part One

Processing

import processing.serial.*; //This allows us to use serial objects

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

void setup()
{
size(800, 600);
println(Serial.list()); //This shows the various serial port options
String portName = Serial.list()[1]; //The serial port should match the one the Arduino is hooked to
port = new Serial(this, portName, 9600); //Establish the connection rate
}

void draw()
{
background(255);
if (mouseOverRect() == true)
{ // If mouse is over square,
fill(150); // change color and
port.write('H'); // send an H to indicate mouse is over square
}
else
{ // If mouse is not over square,
fill(0); // change color and
port.write('L'); // send an L otherwise
}
rect(50, 50, 100, 100); // Draw a square
}

boolean mouseOverRect()
{ // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

Arduino

#define PIN13 13 //define the pin
char val; // Data received from the serial port

void setup()
{
pinMode(PIN13, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}

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

if (val == 'H')
{ // If H was received
digitalWrite(PIN13, HIGH); // turn the LED on
}
else
{
digitalWrite(PIN13, LOW); // Otherwise turn it OFF
}
}

Part Two

Processing

import processing.serial.*; //This allows us to use serial objects

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

void setup()
{
size(800, 600);
println(Serial.list()); //This shows the various serial port options
String portName = Serial.list()[1]; //The serial port should match the one the Arduino is hooked to
port = new Serial(this, portName, 9600); //Establish the connection rate
}

void draw()
{
background(255);

char speed = 'E';
if (mouseSpeed()>45)
{
speed = 'A';
}
else if (mouseSpeed()>30)
{
speed = 'B';
}
else if (mouseSpeed()>15)
{
speed = 'C';
}
else if (mouseSpeed()>5)
{
speed = 'D';
}
else
{
speed = 'E';
}
background(mouseSpeed());
println(speed);
port.write(speed);
}

int mouseSpeed()
{
return((int)(abs(mouseX-pmouseX)+abs(mouseY-pmouseY)));
}

Arduino

#define PIN13 13 //define the pin
#define PIN12 12
#define PIN11 11
#define PIN10 10
#define PIN9 9

char val; // Data received from the serial port

void setup()
{
pinMode(PIN13, OUTPUT); // Set pins as OUTPUT
pinMode(PIN12, OUTPUT);
pinMode(PIN11, OUTPUT);
pinMode(PIN10, OUTPUT);
pinMode(PIN9, OUTPUT);

Serial.begin(9600); // Start serial communication at 9600 bps
}

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

switch(val)
{
case 'A':
digitalWrite(PIN13, HIGH);
digitalWrite(PIN12, HIGH);
digitalWrite(PIN11, HIGH);
digitalWrite(PIN10, HIGH);
digitalWrite(PIN9, HIGH);
break;
case 'B':
digitalWrite(PIN13, HIGH);
digitalWrite(PIN12, HIGH);
digitalWrite(PIN11, HIGH);
digitalWrite(PIN10, HIGH);
digitalWrite(PIN9, LOW);
break;
case 'C':
digitalWrite(PIN13, HIGH);
digitalWrite(PIN12, HIGH);
digitalWrite(PIN11, HIGH);
digitalWrite(PIN10, LOW);
digitalWrite(PIN9, LOW);
break;
case 'D':
digitalWrite(PIN13, HIGH);
digitalWrite(PIN12, HIGH);
digitalWrite(PIN11, LOW);
digitalWrite(PIN10, LOW);
digitalWrite(PIN9, LOW);
break;
default:
digitalWrite(PIN13, HIGH);
digitalWrite(PIN12, LOW);
digitalWrite(PIN11, LOW);
digitalWrite(PIN10, LOW);
digitalWrite(PIN9, LOW);
break;
}
}

One Response to “A conversation between Processing and Arduino”

  1. Michaelon 02 Mar 2012 at 3:27 pm

    Really an excellent and simple solution! I’ve been scanning for days looking for an easy way to make Processing talk to Arduino. I’m going to take this and see if we can get Twitter to talk to Processing now and then to Arduino.

    Many thanks and well done!