A mode filter for the maxbotix sensor

Carrying on from my last post I investigated how accurate the sonar readings were. Overall the readings are extremely stable (except when you don’t solider the pins in properly). To overcome the rare anomalies, and to make sure I will store the correct values further done the track I decided to come up with a mode filter which I couldn’t find on the arduino forums. I used the sorting function from this forum, googled around and came up with the following code. I also included a check to see if there is a mode, or if there are two modes, and if so, to grab the median value, instead of the mode.

This script is designed to take several readings from the maxbotix sonar and generate a mode/median.
Author: Jason Lessels
Date created: 2011/June/06
Lisence: GPL (=>2)
This work has been compileed using many sources mainly posts/wiki posts from;
Allen, Bruce (2009/July/23) and Gentles, Bill (2010/Nov/12)

//Set the pin to recieve the signal.
const int pwPin = 7;
//variables needed to store values
int arraysize = 9;  //quantity of values to find the median (sample size). Needs to be an odd number

//declare an array to store the samples. not necessary to zero the array values here, it just makes the code clearer
int rangevalue[] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0};
long pulse;
int modE;

void setup()

  //Open up a serial connection
  //Wait for the serial connection

//Main loop where the action takes place
void loop()
  pinMode(pwPin, INPUT);

  for(int i = 0; i < arraysize; i++)

    pulse = pulseIn(pwPin, HIGH);
    rangevalue[i] = pulse/58;

  Serial.print("Unsorted: ");
  Serial.print("Sorted: ");
  modE = mode(rangevalue,arraysize);
  Serial.print("The mode/median is: ");

//Function to print the arrays.
void printArray(int *a, int n)

  for (int i = 0; i < n; i++)
    Serial.print(a[i], DEC);
    Serial.print(' ');


//Sorting function
// sort function (Author: Bill Gentles, Nov. 12, 2010)
void isort(int *a, int n)
//  *a is an array pointer function
  for (int i = 1; i < n; ++i)   {     int j = a[i];     int k;     for (k = i - 1; (k >= 0) && (j < a[k]); k--)
      a[k + 1] = a[k];
    a[k + 1] = j;

//Mode function, returning the mode or median.
int mode(int *x,int n){
  int i = 0;
  int count = 0;
  int maxCount = 0;
  int mode = 0;
  int bimodal;
  int prevCount = 0;

    if(count==maxCount){//If the dataset has 2 or more modes.
    if(mode==0||bimodal==1){//Return the median if there is no mode.
    return mode;

By the next post I hope to have a video of the setup of both the mode filter and the data storage in action. I have this working, but no time to get it into a video.


2 responses to “A mode filter for the maxbotix sensor

  1. To make things easier, I developed a library: https://github.com/Diaoul/arduino-Maxbotix

    Let me know what you think!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s