We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So here is code in Arduino
int sensorPin =A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int data=analogRead(sensorPin);
Serial.println(data);
Serial.write(10);
delay(1);
}
Processing Code
import processing.serial.*;
Serial myPort;
float data;
void setup(){
size(800, 800);
smooth();
String portName=Serial.list()[4];
myPort = new Serial(this, portName, 9600);
}
void draw() {
if (myPort.available() > 0) {
data = myPort.read();
rect(width / 2, height / 2, data, data);
println(data);
}
}
Answers
https://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_5
Thank you so much, It works very well. Just may I ask why my code doesn't work? When I run my code,the data in Arduino varies based on the sensor. But in Processing the data I got is complete different,I can not see any clue how it changes.
And more over can I use the data to make some graphs in this way.
Sorry for keeping bothering you,thank you so much.
Important: Never mutate the sketch's canvas outside the "Animation Thread"! [-X
You're invoking rect() inside serialEvent() which is run outside the "Animation Thread"! :-SS
Aha,,yes,, the rect should be invoked inside draw. It's all solved now!!!!so cool, Thank you so much. Learned a lot. Have a good day.