# Better way to read Maxbotix Sensor

Hi,
I am using a Maxbotix EZ-1 sensor that when triggered sends and email.
I take 5 readings and then get average for my final answer. Most of the time this works great.
Sometimes a get a false positive and the sensor is triggered when it shouldn’t.
The code below works fine, but I want to make it better. Variable distsensor is declared as int earlier and is global.
How would I re-write the code to remove the highest and lowest readings for a better final result?

``````void checkSensors(){
Serial.println("Checking Sensors");
for (i=0; i<5; i++)
{
delay(5);
}
distsensor/=5;
if (distsensor <=70  )
{
sensor1=3;
sendEmail();
}
}
``````

Thanks.

Use an array to hold the 5 values. Loop through the array to find the highest value. Record it's position. Loop through the array to find the smallest value. Record it's position. Set the highest and lowest positions to 0, then add all the values and divide by 3, to get the average.

Thanks PaulS. I'll give the array suggestion a try.

No need to store all the samples. Track the highest and lowest and subtract them before averaging:

int lowest, highest, total;
lowest = total;
highest = total;
for (i=0; i<(SAMPLES-1); i++)
{
int value;
delay(5);
if (value < lowest) lowest=value;
if (value > highest) highest=value;
total+= value;
}
total -= lowest;
total -= highest;
total /= (SAMPLES-2); // Average the remaining samples

I have cobbled together the following completely untested code to test both suggestions.
Using an Insertion Sort to test PaulS’s array method I sort the array lowest to highest value, then change array nymbers 0 & 4 to zero and divide by 3 for an average.
I added some print statements to johnwasser’s method.
I wonder which method is faster or is it that both methods are so fast it won’t matter?

``````     //Testing methods for losing lowest and highest values from sensor reading
//http://www.algolist.net/Algorithms/Sorting/Insertion_sort

int i;
int j;
int tmp;
int arr[5];
int length=5;
int a[] = {2, 4, 8, 3, 6};
int results=0;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Unsorted array ");
Serial.println(a);

insertionSort(a,5);

Serial.print("Sorted array ");
Serial.println(a);
chopArray();
Serial.print("Final average =  ");
Serial.println(results);
delay(5000);
trackingSort();
delay(5000);
}

void insertionSort(int arr[], int length) {
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
void chopArray(){
a[0]=0;
a[4]=0;
for (i = 1; i < 4; i++)
results= results+a[i];
results/=3;
}
void trackingSort(){
int lowest, highest, total;
lowest = total;
highest = total;
for (i=0; i<(SAMPLES-1); i++)
{
int value;
delay(5);
Serial.print(value);
Serial.print(" ");
if (value < lowest) lowest=value;
if (value > highest) highest=value;
total+= value;
}
Serial.println(" ");
Serial.print("Lowest ");
Serial.print(lowest);
Serial.print("Highest ");
Serial.println(highest);
total -= lowest;
total -= highest;
total /= (SAMPLES-2);  // Average the remaining samples
Serial.println("Total =  ");
Serial.print(total);
Serial.println(" ");
}
``````

Ok, I got the code working.
In the first part I use PaulS’s idea with a made up array. Later I will hook it up to the sensor.
Thanks for the help.

`````` //Testing methods for losing lowest and highest values from sensor reading
//http://www.algolist.net/Algorithms/Sorting/Insertion_sort

int i;
int j;
int tmp;
int arr[5];
int length=5;
int a[] = {2, 4, 8, 3, 6};
int results=0;
int SAMPLES;
void setup() {
Serial.begin(9600);
Serial.println("Getting a better sensor average");
Serial.println("Sorting an array and removing highest and lowest numbers using an Insertion Sort");
Serial.println("Unsorted array ");
for (i=0; i<5; i++)
{
Serial.print(a[i]);
Serial.print(" ");
}
Serial.println("");
insertionSort(a,5);

Serial.println("Sorted array ");
for (i=0; i<5; i++)
{
Serial.print(a[i]);
Serial.print(" ");

}
Serial.println(" ");
chopArray();
Serial.print("Final average (total / 3) =  ");
Serial.println(results);
Serial.println(" ");

trackingSort();

}

void loop() {

}

void insertionSort(int arr[], int length) {
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}

void chopArray(){
a[0]=0;
a[4]=0;
Serial.println("Chopping array");
for (i = 0; i < 5; i++){
Serial.print(a[i]);
Serial.print("  ");
results= results+a[i];
}
Serial.print("Total = ");
Serial.println(results);
results=results/3;
Serial.println("");

}

void trackingSort(){
Serial.println("------------------------");
Serial.println("");
Serial.println("Tracking lowest & highest without array and removing them");
Serial.print("Sensor reading = ");
int lowest, highest, total;
Serial.print(total);
Serial.print(" ");
lowest = total;
highest = total;
for (i=0; i<4; i++)
{
int value;
delay(5);
Serial.print(value);
Serial.print(" ");
if (value < lowest) lowest=value;
if (value > highest) highest=value;
total= total+ value;

}
Serial.println("");
Serial.print("Lowest = ");
Serial.println(lowest);
Serial.print("Highest = ");
Serial.println(highest);
Serial.print("Total  =  ");
Serial.println(total);
total -= lowest;
total -= highest;

Serial.print("Total - high & low values =  ");
Serial.println(total);
Serial.println(" ");
Serial.print("Final Average =  ");
total /= (5-2);  // Average the remaining samples
Serial.print(total);
}
``````