I have successfully established a Serial connection from Arduino to Processing using the read()-function (read() / Libraries / Processing.org). This only returns numbers between 0 and 255 though and I was wondering what the best method for receiving higher number would be?
Do I have to use readString() and convert it to an integer?
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);
}
Yes, I wasn’t very specific sorry. I simply want to send sensor values higher than 255 from Arduino to Processing – I understand now Serial.write() and read() in not the way to go. Thank you for your help!