Interfacing with Processing is simple as Processing has a
Serial Module.
Processing to Arduino I
The Arduino board will receive data from the computer.
In this case, the Arduino boards turns on an LED when it receives the character 'H',
and turns off the LED when it receives the character 'L'. Put a LED on pin 13.
Pots and Processing
Wire an LED and a potentiometer. Complete and upload the Arduino code, then open Processing and run the program. Make any modifications in the Processing code that suits you.
Arduino
// variables for input pin and control LED
int analogInput =__;
int LEDpin =__;
// variable to store the value
int value = 0;
int value2 = 0;
int old=0;
// a threshold to decide when the LED turns on
void setup(){
// declaration of pin modes
pinMode(LEDpin, OUTPUT);
// begin sending over serial port
Serial.begin(9600);
}
void loop(){
// read the value on analog input
value = analogRead(analogInput);
delay(10);
value2 = analogRead(analogInput);
if(value!=old && (value==value2)){
// print out value over the serial port
Serial.print(value/4);
// and a signal that serves as seperator between two values
printByte(10);
// wait for a bit to not overload the port
delay(10);
old=value;
}
}
void blink(){
digitalWrite(LEDpin, HIGH);
delay(500);
digitalWrite(LEDpin, LOW);
}
Processing
// importing the processing serial class
import processing.serial.*;
// variables for serial connection, portname and baudrate have to be set
Serial port;
// definition of window size and framerate
int xWidth = 267;
int yHeight = 400;
int fr = 24;
int value = 0;
String buf="";
int value1=0;
// variables to draw graphics
int xpos;
void setup(){
port = new Serial(this, Serial.list()[0], 9600);
// set size and framerate
size(xWidth, yHeight);
frameRate(fr);
}
void drawPotState(){
noStroke();
fill(0, 255-xpos, xpos);
// draw rectangfle at xpos
rect(xpos, 0, width/32, height);
fill(255-xpos, 0, 0);
// draw rectangfle at xpos
rect(0, 0, xpos, height);
}
void serialEvent(int serial){
// if serial event is not a line break
if(serial!=10) {
// add event to buffer
buf += char(serial);
} else {
// if serial is line break set value1 to buff and clear it
value1 = int(buf);
buf="";
}
// convert value1 to xpos
if (value1>0 && value1<256){
xpos=value1;
}
}
// draw listens to serial port, draw
void draw(){
background(0);
// listen to serial port and trigger serial event
while (port.available() > 0) {
serialEvent(port.read());
}
drawPotState();
}
Sending Multiple values
Send multiple values from the Arduino board to the computer.
In this case, the readings from three potentiometers are used to set the red,
green, and blue components of the background color of a Processing sketch.
-
Wire up a board with 3 potentiometers. One will control the red level, another
the green level and the last, the blue level.
- In Arduino set up your program
- Set up your red, green and blue variables
- In setup() start serial communication
- In loop() use
Serial.print("R");
Serial.println(analogRead(redPin)); for each color
- Call delay and pass it 100
- Open Processing and create a new sketch
- Copy, paste and modify:
//import the serial package
_________________
//create a variable of type String named buff and set to an empty string
___________
//create 3 variables of type int to hold the rval, gval and bval values
_______________
//create a variable of type int named NEWLINE and set it to 10
_________________
//create a variable of type Serial named port. Do not set it
________
void setup(){
//set the size of the sketch
_______________
// Print a list in case COM1 doesn't work out
println("Available serial ports:");
println(Serial.list());
//port = new Serial(this, "COM1", 9600);
// Uses the first available port
port = new Serial(this, Serial.list()[0], 9600);
}
void draw(){
while (port.available() > 0) {
serialEvent(port.read());
}
//set background to the red, green and blue values
________________
}
void serialEvent(int serial) {
// If the variable "serial" is not equal to the value for
// a new line, add the value to the variable "buff". If the
// value "serial" is equal to the value for a new line,
// save the value of the buffer into the variable "val".
//test if serial does not equal NEWLINE
__________________
//if condition is met set buff to itself plus the char passed in through serial
//use char(serial)
____________
} else {
// The first character tells us which color this value is for
char c = buff.charAt(0);
// Remove it from the string
buff = buff.substring(1);
// Discard the carriage return at the end of the buffer
buff = buff.substring(0, buff.length()-1);
// Parse the String into an integer
if (c == 'R')
rval = Integer.parseInt(buff);
else if (c == 'G')
//continue for green and blue
// Clear the "buff"
__________
}
}
Modify the code so that the user changes the color of a rectangle instead of the background.