# Trying to store the previous value of a variable

**URL:** <https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 7, 2021, 7:17am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606 "2021-08-07T07:17:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![G03TH3](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/g03th3/32/14033_2.png) [@G03TH3](https://discourse.processing.org/u/G03TH3)\
**Post date:** [August 7, 2021, 7:17am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/1 "2021-08-07T07:17:59Z")

</div>

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!.

```auto
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....

```

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [August 7, 2021, 8:17am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/2 "2021-08-07T08:17:17Z")

</div>

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

```auto
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 🙂

This Code is not tested

---

<div class="post-metadata">

**Author:** ![G03TH3](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/g03th3/32/14033_2.png) [@G03TH3](https://discourse.processing.org/u/G03TH3)\
**Post date:** [August 7, 2021, 8:18am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/3 "2021-08-07T08:18:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [August 7, 2021, 10:23am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/4 "2021-08-07T10:23:11Z")

</div>

@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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [August 7, 2021, 5:26pm UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/5 "2021-08-07T17:26:02Z")

</div>

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 7, 2021, 11:32pm UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/6 "2021-08-07T23:32:31Z")

</div>

Hello,

> [@G03TH3](#):
>
> if anybody has any idea how to save an previous value from the code i just wrote or anything similar,

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

This will correct it:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ee3079236db181d677b9cd2a272c2fd55e6a16c3.png)

I was sending an integer count from Arduino.

`:)`

---

<div class="post-metadata">

**Author:** ![G03TH3](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/g03th3/32/14033_2.png) [@G03TH3](https://discourse.processing.org/u/G03TH3)\
**Post date:** [August 10, 2021, 8:45am UTC](https://discourse.processing.org/t/trying-to-store-the-previous-value-of-a-variable/31606/7 "2021-08-10T08:45:22Z")

</div>

_IT WORKED!_

 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a096e99994b6d574eda78ce3bd5fed4309d4fbd7.png)  
Thanks for your help (and everyone else ; ) ) , i didn’t knew the printing order would affect the result of the variable, thanks again!.
