@Robin2, yes, my suggestion regarding throwing all the data away if the min and max values are more than 10% apart after comparing 10 values (which are each within 10% of each reading), is a problem. It is especially a problem if it so happened that part of those values would be within 10% tolerance of a group of data which is read proceeding when that data was thrown away. You might get half of the magic values in one group and half of the magic values in another group and end up throwing them away and not finding the magic moment.
A better solution would be:
Start the program by gathering 10 values into an array of 10 elements(before doing anything else).
Then, start finding the min value in the array and the max value in the array. Compare the min and the max to see if they are 10% or less apart. If they aren’t 10% or less apart, throw out the oldest value and add in the newest value. Compare again.
Rinse and repeat. This way, you only have to compare the lowest and highest value each time (since all the other values will be within that range) and you never throw the baby out with the bathwater.
Since I am only a beginner and don’t know C very well (I am much better at simple languages like PHP), the following code could have errors I am not aware of or floating point issues, but consider it and make minor adjustments as necessary.
#include <stdio.h>
int a[10];
int i;
int max;
int min;
int start_incrementer;
int value_read;
void setup()
{
pinMode(2, INPUT); // or whichever pin
a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
i = 0;
max = 0;
min = 1023;
start_incrementer = 0;
value_read = 0;
}
void loop()
{
// analogRead a pin or wherever you want the data to come from
value_read = analogRead(2);
// make sure at least 10 values are gathered before starting to compare the array elements
// this makes sure you avoid reading the initial array of 0s so you don't find an artificial match at the start of the program
if(start_incrementer < 10)
{
// shift oldest to the end of array and newest at the start of the array
a[9] = a[8];
a[8] = a[7];
a[7] = a[6];
a[6] = a[5];
a[5] = a[4];
a[4] = a[3];
a[3] = a[2];
a[2] = a[1];
a[1] = a[0];
a[0] = value_read;
// add this loop to the counter
start_incrementer++;
}
else
{
// before adding more to the array, see if we have found a match
// find max value in the array
// initialize max variable with the minimum value possible
// in this case, 0 for an analogRead
max = 0;
for(i=0; i<10; i++)
{
if(a[i] > max)
{
max = a[i];
}
}
// find min value in the array
// initialize min variable with the max value possible
// in this case, 1023 for an analogRead
min = 1023;
for(i=0; i<10; i++)
{
if(a[i] < min)
{
min = a[i];
}
}
// are max and min within 10% of eachother?
if(max/min < 1.10)
{
// do something
// we found an instance where all values are within 10% of eachother
}
// add to the array and keep looking for more matches
// shift oldest to the end of array and newest at the start of the array
// the 11th oldest value naturally falls off the end
a[9] = a[8];
a[8] = a[7];
a[7] = a[6];
a[6] = a[5];
a[5] = a[4];
a[4] = a[3];
a[3] = a[2];
a[2] = a[1];
a[1] = a[0];
a[0] = value_read;
}
}
I think that will do what you are after except that I don’t know your bigger vision and don’t know how you are receiving the data. If you are receiving the data at certain intervals or at an otherwise slow rate, you may need to sync the sampling of the code so that the same sample is not taken 10 times within the same 1000ths of a second and considered 10% in tolerance with itself.