That...odd. I've got a sketch communicating to Processing, and it doesn't call an "Arduino" library at all.
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
int newval = 0;
void setup()
{
size(800, 600);
frameRate(100);
println(Serial.list()); // this line creates a list serial devices currently connected
port = new Serial(this, Serial.list()[2], 9600); // the third port in the list is where my Arduino was
}
void draw()
{
background(255); // setting the new window's background color first
if (0 < port.available())
{
val = port.read();
port.clear(); // so when I poll the port next time, I don't get old data off it
}
if (val != newval) // for this particular routine, change box color when data changes
{
switch (val - 50) // hacking; sending int from Arduino, and "1" sent = "51" received.
{
case 0: // using switch-case to set four different box colors
fill(#F62817);
println(val); // just for debugging purposes; prints to the Processing environment only
newval = val;
val = 0;
break;
case 1:
fill(#F76521);
println(val);
newval = val;
val = 0;
break;
case 2:
fill(#FDD017);
println(val);
newval = val;
val = 0;
break;
case 3:
fill(#347C2C);
println(val);
newval = val;
val = 0;
break;
}
}
rect(25, 25, 750, 550);
}
yay i got it figured.
so what i have here is my arduino plugged into my laptop through usb. on the arduino is thing that detects the proximity of an object by using infrared light and an infrared sensor. it has a power, ground, and data wire, the last through which it returns numbers relevant to the distance of an object on top of the sensor. the data wire goes in analog read pin 0. there is no code in the arduino.
this is the processing code:
import processing.serial.*;
Serial myPort;
byte[] inBuffer = new byte[5];
float float_data;
int int_data;
int ir_data;
int data;
int last_val_data = 0;
void setup(){
size (300, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw (){
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
if (inBuffer != null) {
String myString = new String(inBuffer);
String raw_data = myString ;
if (raw_data != null)
float_data = float(raw_data);
}
int_data = int(float_data);
if (int_data != 0){
ir_data = int_data;
}
data = smoother(ir_data, last_val_data, 10);
last_val_data = data;
background(data);
println(data);
}
int smoother (int last_output, int input, int factor){
return last_output + (input - last_output) / factor;
}
any one is free to improve on my code and share changes!
I'm suspect of that [0] there in the MyPort. I believe your USB keyboard/mouse is probably first on the list of identified ports, with the Arduino somewhere further down the list.
Make an ultra-simple program, like a counter or a button-press, and see if you can pick up serial data from the Arduino. Then work outward from there.