More Processing+Arduino

Read Data from the Serial Port
The first visualization example shows how to read the serial data in from the Arduino board and how to convert that data into the values that fit to the screen dimensions:

import processing.serial.*;
Serial port; // Create object from Serial class
float val; // Data received from the serial port
void setup() {
size(440, 220);
// IMPORTANT NOTE:
// The first serial port retrieved by Serial.list()
// should be your Arduino. If not, uncomment the next
// line by deleting the // before it. Run the sketch
// again to see a list of serial ports. Then, change
// the 0 in between [ and ] to the number of the port
// that your Arduino is connected to.
//println(Serial.list());
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 9600);
}
void draw() {
if (port.available() > 0) { // If data is available,
val = port.read(); // read it and store it in val
val = map(val, 0, 255, 0, height); // Convert the value
}
rect(40, val-10, 360, 20);
}
The Serial library is imported on the first line and the serial port is opened in setup(). It may or may not be easy to get your Processing sketch to talk with the Arduino board; it depends on your hardware setup. There is often more than one device that the Processing sketch might try to communicate with. If the code doesn’t work the first time, read the comment in setup() carefully and follow the instructions.
Within draw(), the value is brought into the program with the read() method of the Serial object. The program reads the data from the serial port only when a new byte is available. The available() method checks to see if a new byte is ready and returns the number of bytes available.
This program is written so that a single new byte will be read each time through draw(). The map() function converts the incoming value from its initial range from 0 to 255 to a range from 0 to the height of the screen; in this program, it’s from 0 to 220.

Visualize serial data
Visualizing the Data Stream
Now that the data is coming through, we’ll visualize it in a more interesting format. The values coming in directly from a sensor are often erratic, and it’s useful to smooth them out by averaging them.

import processing.serial.*;
Serial port; // Create object from Serial class
float val; // Data received from the serial port
int x;
float easing = 0.05;
float easedVal;
void setup() {
size(440, 440);
frameRate(30);
smooth();
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 9600);
background(0);
}
void draw() {
if ( port.available() > 0) { // If data is available,
val = port.read(); // read it and store it in val
val = map(val, 0, 255, 0, height); // Convert the values
}

targetVal = val;
easedVal += (targetVal – easedVal) * easing;
stroke(0);
line(x, 0, x, height); // Black line
stroke(255);
line(x+1, 0, x+1, height); // White line
line(x, 220, x, val); // Raw value
line(x, 440, x, easedVal + 220); // Averaged value
x++;
if (x > width) {
x = 0;
}
}
This sketch uses the easing technique. Each new byte from the Arduino board is set as the target value, the difference between the current value and the target value is calculated, and the current value is moved closer to the target. Adjust the easing variable to affect the amount of smoothing applied to the incoming values.
Another Way to Look at the Data
This example is inspired by radar display screens. The values are read in the same way from the Arduino board, but they are visualized in a circular pattern using the sin() and cos() functions.

import processing.serial.*;
Serial port; // Create object from Serial class
float val; // Data received from the serial port
float angle;
float radius;
void setup() {
size(440, 440);
frameRate(30);
strokeWeight(2);
smooth();
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 9600);
background(0);
}

void draw()
if ( port.available() > 0) { // If data is available,
val = port.read(); // read it and store it in val
// Convert the values to set the radius
radius = map(val, 0, 255, 0, height * 0.45);
}
int middleX = width/2;
int middleY = height/2;
float x = middleX + cos(angle) * height/2;
float y = middleY + sin(angle) * height/2;
stroke(0);
line(middleX, middleY, x, y);
x = middleX + cos(angle) * radius;
y = middleY + sin(angle) * radius;
stroke(255);
line(middleX, middleY, x, y);
angle += 0.01;
}

The angle variable is updated continuously to move the line drawing the current value around the circle, and the val variable scales the length of the moving line to set its distance from the center of the screen. After one time around the circle, the values begin to write on top of the previous data.
We’re excited about the potential of using Processing and Arduino together to bridge the world of software and electronics. Unlike the examples printed here, the communication can be bidirectional. Elements on screen can also affect what’s happening on the Arduino board. This means you can use a Processing program as an interface between your
computer and motors, speakers, lights, cameras, sensors, and almost anything else that can be controlled with an electrical signal. Again, more information about Arduino can be found at http://www.arduino.cc.

Physical pixel
// Physical pixel arduino.cc

// mouseover serial

// Demonstrates how to send data to the Arduino I/O board, in order to
// turn ON a light if the mouse is over a square and turn it off
// if the mouse is not.

import processing.serial.*;

float boxX;
float boxY;
int boxSize = 20;
boolean mouseOverBox = false;

Serial port;

void setup() {
size(200, 200);
boxX = width/2.0;
boxY = height/2.0;
rectMode(RADIUS);

// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());

// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);

}

void draw()
{
background(0);

// Test if the cursor is over the box
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
mouseOverBox = true;
// draw a line around the box and change its color:
stroke(255);
fill(153);
// send an ‘H’ to indicate mouse is over square:
port.write(‘H’);
}
else {
// return the box to it’s inactive state:
stroke(153);
fill(153);
// send an ‘L’ to turn the LED off:
port.write(‘L’);
mouseOverBox = false;
}

// Draw the box
rect(boxX, boxY, boxSize, boxSize);
}
Sensor control LED
/*
* ap_ReadAnalog
*
* Reads an analog input from the input pin and sends the value followed by a line break over the serial port.
*
* This file is part of the Arduino meets Processing Project:
* For more information visit http://www.arduino.cc.
*
*
*/

// variables for input pin and control LED
int analogInput = 3;
int LEDpin = 13;

// variable to store the value
int value = 0;

// a threshold to decide when the LED turns on
int threshold = 512;

void setup(){

// declaration of pin modes
pinMode(analogInput, INPUT);
pinMode(LEDpin, OUTPUT);

// begin sending over serial port
beginSerial(9600);
}

void loop(){
// read the value on analog input
value = analogRead(analogInput);

// if value greater than threshold turn on LED
if (value < threshold) digitalWrite(LEDpin, HIGH); else digitalWrite(LEDpin, LOW);

// print out value over the serial port
printInteger(value);

// and a signal that serves as seperator between two values
printByte(10);

// wait for a bit to not overload the port
delay(10);
}
http://webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/ldr.html
Mouse position control LED intensity

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
size(512, 512); // It is defined the window to move our mouse
arduino = new Arduino(this, Arduino.list()[0], 57600);
}
void draw() {
//Change window colour depending on the mouse X position
background(constrain(mouseX / 2, 0, 255));
// The value of the mouse X position is recorded and sent to analogic output 13
arduino.analogWrite(13, constrain(mouseX / 2, 0, 255));
// The value of the mouse Y position is recorded and sent to analogic output 9
arduino.analogWrite(9, constrain(255 – mouseY / 2, 0, 255));
}

MOUSE and LED Method 2
Mouse control switch on/off of a LED

/*
* it switch the led on when the mouse button and it is switch off when it is not pressed*/
import processing.serial.*; //Import the necessary libraries
import cc.arduino.*;
Arduino arduino; // Create the objet Arduino
int ledPin = 13; // Design PIN number for LED
void setup() { //Configure port and signals to work with
size(200, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600); // Configure port as of 0 index, could be 1, 2,…
arduino.pinMode(ledPin, Arduino.OUTPUT); // Configure PIN13 as output
arduino.digitalWrite(ledPin, Arduino.HIGH); //Swith on the LED very shortly and only once, it is not necessary
}
void draw() { //Draw the interaction window
if (mousePressed == true) { //Ask if the mouse button is pressed
arduino.digitalWrite(13,Arduino.HIGH); // If you press the mouse button switch the LED on
} else {
arduino.digitalWrite(13,Arduino.LOW); // If you don’t press the mouse button switch the LED off
}
}