Please consider the following demo and see if you can adapt it to your project. The demo will send random integers from 255 to 1024 from Arduino followed by a line feed. The Processing code will put the incoming data into a string and break it at the line feed. The resulting string is then reconverted to an integer and printed to the console.
Processing Code:
import processing.serial.*;
Serial myPort;
String inStr;
void setup() {
surface.setVisible(false);
printArray(Serial.list());
// Select port for your system
myPort= new Serial(this, Serial.list()[4], 9600);
}
void draw() {
}
void serialEvent( Serial myPort) {
//put the incoming data into a String -
//the '\n' is delimiter indicating the end of a complete packet
inStr = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (inStr != null) {
//trim whitespace and formatting characters (like carriage return)
inStr = trim(inStr);
int val = Integer.valueOf(inStr);
println("value = ", val);
}
}
Arduino Code:
long randNumber;
void setup() {
Serial.begin(9600);
}
void loop() {
// print a random number from 255 to 1024
randNumber = random(255, 1024);
Serial.println(randNumber);
delay(50);
}