We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys, I want to control a ball's acceleration in a proccesing animation, accordingly to a analogue input on my Arduino. The balls acceleration and behaviour is written with PVectors (see code below) but when I activate my arduino sketch, the ball just disappears from the window. I have made several simple tests were I for an example changed the size of an ellipse by mapping the serialRead with success, so the communication between Arduino and Processing is not the problem.
//the code:
import processing.serial.*;
Serial myPort;
Mover mover;
float inByte;
void setup(){
size(800,600);
background(240);
smooth();
mover = new Mover();
myPort = new Serial(this,Serial.list()[2], 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(240);
mover.update();
mover.checkEdges();
mover.display();
fill(10);
}
void serialEvent (Serial myPort){
String inString = myPort.readStringUntil('\n');
if(inString != null){
inString = trim(inString);
inByte = float(inString);
inByte = map(inByte,0,1023,0,5);
}
}
//newtab
class Mover{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover(){
location = new PVector(width/2,height/2);
velocity = new PVector(0,0);
topspeed = 10;
}
void update(){
acceleration = PVector.random2D();
acceleration.mult(inByte);
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display(){
noStroke();
fill(10);
ellipse(location.x, location.y, 50, 50);
}
void checkEdges(){
if(location.x > width){
location.x = 0;
}
else if (location.x < 0 ){
location.x = width;
}
if(location.y > height){
location.y = 0;
}
else if(location.x < 0){
location.y = height;
}
}
}
Answers
You'll get a better response if you format your code. Here's how:
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
ahh, thx for the tip bilmor
Dunno whether it helps or not, but I've got some tips anyways:
Since you had already activated bufferUntil(), just a simple readString() within serialEvent() is enough:
https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html
https://Processing.org/reference/libraries/serial/Serial_readString_.html
Also it seems like inByte is used internally inside Mover class mostly.
So you should consider declaring it a member of it.
In short, something like this: O:-)
Thx for the advice GoToLoop. As a test I managed to control the size of the mover with inByte but when i add more movers to the animation they all disappear. And when I try to multiple inByte with the acc, the mover disappears again.
Hey i think that the problem is that processing Serial lags when arduino sends too much data. since the values dont change over a certain period of time, all the data gets buffered every time that the the arduino prints data over the serial port. and if the values are the same every time, that wll be buffered and readed afterward by processing. Add a delay of 25 millisec to your arduino sketch. and check if it works. It worked for me. The lag was so bad that i could unplug my arduino, and processing was still "receiving data from the comport".
I tried to add delay to my Arduino sketch but i didn't work :( i also tried with delay(100), but my mover still disappears. My Arduino sketch looks like this: