Trying to store the previous value of a variable

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....
1 Like

Hey I thnik you could Do this:
Instead of setting the variable you could Do this:

Float x;
Void setup (){
Size(600, 600);
X = 0;
}
Void draw(){
Background (0);
ChangeX(mouseX);
Fill(255);
Ellipse(x, height/2, 30, 30);
}
Void changeX(int i){
  //x = i;
X += i;
X /= 2;
}

This should make the movement smoother :slight_smile:

This Code is not tested

1 Like

Flolo thank you so much for such an rapid response!, ill check it ASAP!, thanks for your contribution.

1 Like

@Flolo, your code is strangely formatted, in that the first letter on each line is in uppercase. This, which is your code reformatted, worked correctly, when tested on my MacBook Air:

float x;
void setup (){
  size(600, 600);
  x = 0;
}

void draw(){
 background (0);
  changeX(mouseX);
  fill(255);
  ellipse(x, height/2, 30, 30);
}
void changeX(int i){
  // x = i;
  x += i;
  x /= 2;
}
2 Likes
int c = 1;
int move=0; 
void setup() { 
  size(600, 600);
} 
void draw() { 
  background(2); 
  move= move + c; 
   fill(255);
   ellipse(move, width/2, 45,45); 
  if (move>= width || move <= 0 ) { 
    c= c * -1;
  }
}
1 Like

Hello,

Take a close look at where you were printing values and it will come to you.

This will correct it:

I was sending an integer count from Arduino.

:)

2 Likes

IT WORKED!


Thanks for your help (and everyone else ; ) ) , i didn’t knew the printing order would affect the result of the variable, thanks again!.

1 Like