# Storing a previous value/creating an array

Hi, I am hoping someone can help.....

So I have a loop going and in this I am calculating a value for X

void loop(){ //Calculating a value for X }

When the loop is finished I am then serial printing my calculated value for X. It keeps updating like below;

X = 173 X = 176 X = 169 X = 169 X = 200

I now want to check if X has changed from its previous two values.

e.g If X(last) == X(2ndlast) == X(3rdlast), then Serial.print(Yes);

else { Serial.print(No);

Can anyone help on how to code this? I am getting confused on storing the previous value. Do I need to create an array?

Thanks

bogey: Do I need to create an array?

You don't have to, but that would be one way to keep track of the previous values, and that is what you need to do.

You don't have to, but that would be one way to keep track of the previous values, and that is what you need to do.

Any suggestions on how to do this?

bogey:

You don't have to, but that would be one way to keep track of the previous values, and that is what you need to do.

Any suggestions on how to do this?

Assuming you have three variables: current, last and former. Right before you set the value of current, set former to last, and last to current.

Time to hit the tutorials. Arrays are fundamental.

http://arduino.cc/en/Tutorial/Array

Examples > Control Structures

Arrays

This variation on the For Loop example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It’s like a series of linked cups, all of which can hold the same maximum value.

The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino, with certain limitations (the pins have to be numbered contiguously, and the LEDs have to be turned on in sequence).

If you have to ask what a For Loop is then look on the first link, the Tutorials main page, for the For Loop example and learn from that. If some part of that is outside of your knowing then find the example on that.

I don’t want to be a pain but there’s books and sites that explain all these things clearly without someone having to write the same things perhaps not as completely or clearly here.

If it’s any consolation, everyone who knows the stuff had to go through a learning process that kind of never ends. If you think it’s hard now, it was harder before the internet brought everything right to you. Back then I found that a nice Sam’s or Que book let me learn faster than any classes.

As has been suggested, you do not need to use an array, 3 variables will do the job

``````set value1, value2 and X to zero

start of loop
set value1 equal to value2
set value2 equal to X
calculate the new value of X
if ((value1 == value2) && (value2 == X))
print X
end of if
end of loop
``````

This method would be painful if you wanted to compare more than a couple of previous values, which is where an array would come in handy, but this will do for what you have described.