# question about storing in array!!

Hello to all..! I want to ask if there is a way to store values from a sensor to an array and compare each value with previous.. Specifically i want to check when the last 10 values are similar, not the same.! For example 160.03, 159.30, 161.15, 159.8, 158.70, 161.9. . . . etc.
If anyone has an idea for how can i solve this problem please help me because i'm stack and i can not continue my project.!

Well, I can't see your code, so specific help is difficult, but I'd suggest the "abs" macro/function is useful, to see if the absolute value of the difference of adjacent readings is more/less than some predefined threshold.

First off, create an array of "float" data-type that has 10 entries in it.

Second, maintain a count of the number of entries that have had a value loaded into them.

The value you want to compare will be that number minus 1 ("one"), unless the number is zero, then you want to compare it with entry 9 (since entry 10 doesn't exist, since array positions are counted from zero not one).

Finally, can we see some code that you've written in an attempt to solve this problem?

-David

PS: How would abs() help here? They want to compare values in an array. What does that have to do with the absolute (magnitude) value?

How would abs() help here? They want to compare values in an array. What does that have to do with the absolute (magnitude) value?

You can either use a circular buffer to hold the last few values, or a rolling average. The average would be easier to implement but less precise.

You need to add setup() and global vars but this should get you started.

``````void loop()
{
// put your sensor readings in an array used as circular buffer.
if (idx == 10) idx = 0;

// calculate the average of 10 readings
avg = 0;
for (int i=0; i<10; i++) avg += readings[i];
avg /= 10;

// compare the average with individual readings
//
similar = true;
for (int i=0; i<10; i++)
{
if (abs(readings[i]-avg) > (avg*0.05 ))  // 5% can be modified..
{
similar = false;
break;
}
}
Serial.println(similar);
}
``````

Thank you all for your help.. The basic problem is that i do not have threshold values because i use a magnetic sensor for take measurement and the values are not constant. Concerning average, is something that i can not use because my sensor has a fault by its environment which, at some point, can give me an unexplained value.. example.. 160.41,158,50,159.04, (5,10), 160.08, 161,30 . . .

I think to compare each reading with previous one and if they are similar, increase a counter..So when the counter=9 it means that the last 10 values of array are similar..! Is that true..?

my sensor has a fault by its environment which, at some point, can give me an unexplained value.. example.. 160.41,158,50,159.04, (5,10), 160.08, 161,30

If your sensor is slipping-in values in parentheses like that, it should be fairly simple to parse them out.

i do not have threshold

if they are similar

So how do you define similar?

here is an example of what values i received from the sensor… The grad value is the value that i want to store it in the array.! first without magnetic field over the sensor the values are similar like 73.66, 70.61, 77.60, 84.42 as you can see… After i place magnetic field over the sensor the values changed and stabilized to 2.24, 2.19, 3.36, 2.24, 3.29… and when i remove the magnetic field the values comes near to -167.19, -168.66, -170.78 etc…

w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2066 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2066 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2074 w3=2067 b=1101 x=0.00 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2067 b=1101 x=0.01 y=0.02
x^2 + y ^2 = 0.02
w2=2075 w3=2069 b=1101 x=0.01 y=0.03
x^2 + y ^2 = 0.03
w2=2074 w3=2068 b=1101 x=0.00 y=0.03
x^2 + y ^2 = 0.03
w2=0 w3=0 b=1101 x=-9.32 y=-7.90
grad= -139.72 This is an unexplained value
x^2 + y ^2 = 12.21
w2=2075 w3=2068 b=1101 x=0.01 y=0.03
x^2 + y ^2 = 0.03
w2=2075 w3=2068 b=1101 x=0.01 y=0.03
x^2 + y ^2 = 0.03
w2=2091 w3=2038 b=1101 x=0.08 y=-0.09
x^2 + y ^2 = 0.12
w2=2132 w3=2082 b=1101 x=0.26 y=0.08
x^2 + y ^2 = 0.27
w2=2117 w3=2063 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2118 w3=2063 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2117 w3=2064 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2117 w3=2063 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2118 w3=2064 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2118 w3=2064 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2118 w3=2064 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2118 w3=2063 b=1101 x=0.20 y=0.01
x^2 + y ^2 = 0.20
w2=2070 w3=2060 b=1101 x=-0.02 y=-0.00
x^2 + y ^2 = 0.02
w2=2074 w3=2060 b=1101 x=0.00 y=-0.00
x^2 + y ^2 = 0.00
w2=2066 w3=2059 b=1101 x=-0.03 y=-0.01
x^2 + y ^2 = 0.03
w2=2066 w3=2059 b=1101 x=-0.03 y=-0.01
x^2 + y ^2 = 0.03
w2=2065 w3=2059 b=1101 x=-0.04 y=-0.01
x^2 + y ^2 = 0.04
w2=2063 w3=2059 b=1101 x=-0.05 y=-0.01
x^2 + y ^2 = 0.05
w2=2065 w3=2059 b=1101 x=-0.04 y=-0.01
x^2 + y ^2 = 0.04
w2=2065 w3=2059 b=1101 x=-0.04 y=-0.01
x^2 + y ^2 = 0.04

Ok, but you know by inspection that values like 73.66, 70.61, 77.60, 84.42 are similar, and values like 2.24, 2.19, 3.36, 2.24, 3.29… are similar to each other but not to the first batch, and that -167.19, -168.66, -170.78 are also similar to each other yet different from the other two sets.

You need to be able to define the similarity (or difference) so that you can do some kind of comparison so that in a string of values 73.66, 70.61, 77.60, 84.42, 2.24, 2.19, 3.36, 2.24, 3.29… you can see the change where the highlight is.

Like for instance in the set 73.66, 70.61, 77.60, 84.42 the values are within (just guessing) 0.9 to 1.1 of each other, as are the values in the set 2.24, 2.19, 3.36, 2.24, 3.29… But when you get a change from say 80 down to 2, the ratio is up in the 40s…

And of course in the set -167.19, -168.66, -170.78 the -ve sign makes them easy to spot and a change from say 3 to -170 is seen by a -ve ratio.

EDIT… this problem is not so much about the technicalities of storing the numbers, but knowing what it is you’re looking for in the numbers in the first place. Once more it highlights the need to define the problem properly (eg in a flowchart) before leaping into code.

From what you've said so far I would say your best bet would be to calculate a rolling average which is updated with each new sample, and then record a baseline value for the average. After each sample update you recalculate the average and compare the result to the baseline. If the difference exceeds a set percentage you indicate the state as 'rising' or 'falling', otherwise you indicate it as 'steady'.

Your transitions would be detected by debouncing the status and looking for changes in state - you're looking for a rising state followed by a steady state which has persisted for X seconds (the result has stepped up) or a falling state followed by a steady state which has persisted for X seconds (the result has stepped down).

This approach doesn't need an array - it just needs a rolling average, a variable holding the baseline for the rolling average, a variable holding the current state and a variable holding the time of the previous state change.