I am simply trying to get values from a Arduino to Processing:
A gray scale sensor is measuring light perfectly, but the values appearing in processing are flickering between to numbers - regardless the amount of light...
The serial monitor in Arduino is working great - the values are changing with amount of light, so why dont the values appear in Processing?
Thanks.
Morten
The code in Processing:
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(400, 400);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, "COM3", 9600);
}
void draw() {
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
println(val);
}
background(204); // Clear background
fill(val); // Set fill color with the value read
rect(100, 100, 200, 200); // Draw square
In Processing, serial data handling should NOT be done in draw(). It should be done by implementing the serialEvent() method with the proper signature.
The Serial::bufferUntil() method should be called in setup() to trigger the serialEvent() method only when a complete packet arrives.
What is the Arduino actually sending? You are only reading one character at a time in the Processing sketch, and assuming that you have read a complete value that the Arduino sent. That seems unlikely.
You are not discarding any carriage returns or line feeds, either.