I’m attempting to create a ball that is moved through the x-axis reading data from an ultrasonic sensor connected to an Arduino, since the ultrasonic sensor sometimes does not gets the wished value for a second(given to its accuracy or any x reasons) the ball following the data from the sensor displays that data(it jumps out of nowhere to the coordinate received from the sensor), so even if I’m moving my hand 30cm away from the sensor but it gets the wrong distance lets say for 40 cm or so, the ball jumps out of the range were im moving my hand, in order to prevent this from happening im planning to get the present x value and the previous one, compare them, and if the difference its greater that for example : 50 cm, do not update the position of the ball, (this is the best i can come out with my limited processing and coding knowledge), in order to write those conditions i need to get the previous x value and the present one, i attempted arrays, and now this but nothing has worked yet, if anybody has any idea how to save an previous value from the code i just wrote or anything similar, i would be extremely thankful, if not i still thank you for taking your time for reading my question.
Saludos!.
import processing.serial.*;
Serial myPort; //SETTING PORT
String myString = null; //SETTING INCOMING ARDUINO VALUE DATA STRING
int nl = 10;
float myVal, xpos, ypos, difference; // VARIABLES FOR EXECUTION OF PROGRAM
float past_x, x; // ATTEMPTING TO SAVE THE PRESENT AND PAST "X" VALUES
void setup() {
size(500, 500);
xpos = width/2;
ypos = height/2;
myPort = new Serial(this, "COM3", 9600);
}
void draw() {
background(0, 0, 0);
while (myPort.available() > 0) { // BASIC ARDUINO CONNECTION
myString = myPort.readStringUntil(nl); // READ THE ARDUINO DATA UNTIL NL
if (myString != null && myString != "NaN") { // DATA FILTERING CONDTIONS
myVal = float(myString); // COPY THE DATA OF THE SENSOR INTO myString
x = myVal; // TRYING TO SET UP x as myVal
past_x = x; // TRYING TO SAVE THE PRESENT X VALUE INTO past_x
println("myVal = "+myVal);
println("x = "+x);
println("past_x = "+past_x);
}
}
fill(255);
xpos= map(myVal, 20, 190, 10, width);
ellipse(xpos, ypos, 80, 80);
myPort.clear();
}
// THIS IS NOT WORKING....