Is this code returning the median value from the sensor or not?

I have found some code for returning the median value of a ultrasonic sensor, which I’ve managed to upload to my Arduino One, but I’m wondering if it’s really showing me the median value in the serial console, since the only thing saying serial.Print is just the distance it measures.

#include <newping.h>
#include <runningmedian.h>

//Define the pins for the ultrasonic sensor's echo and trigger pins 
#define DISTANCE_ECHO 4
#define DISTANCE_TRIGGER 7
#define MAX_ULTRASONIC_DISTANCE 200

NewPing sonar(DISTANCE_TRIGGER, DISTANCE_ECHO, MAX_ULTRASONIC_DISTANCE);

void setup()
{
    //Initialize serial communication to send the distance value  to the serial monitor
    Serial.begin(9600);
}

void loop()
{
  int distance_cm;
  distance_cm = getDistanceFromUltrasonicSensor();
  
  Serial.print(distance_cm);
  Serial.print("cm");
  Serial.println();
}

int getDistanceFromUltrasonicSensor(){
  //Create an instance of the RunningMedian class with the number of samples to use
  RunningMedian samples = RunningMedian(7);
  
  //Get the 7 samples
  for(int i= 0; i<5; i++){
      delay(50);
      int pingVal = sonar.ping();
      
      int distance = pingVal / US_ROUNDTRIP_CM;
      if(distance == 0){
        distance = 3000;
      }
     samples.add(distance);
   }   
   //Return the median value
   return samples.getMedian();  
}
</runningmedian.h></newping.h>

I need the serial.Print value for a Max MSP project, so if it isn’t, could someone please help me fix it :slight_smile: ? Thanks!

This is not HTML.

Remove this:

</runningmedian.h></newping.h>

Does it even compile like that?

The code creates an instance of a RunningMedian object named samples using the runningmedian library and it appears to create the median value using that and returns it from the getDistanceFromUltrasonicSensor() function to the distance_cm variable.

So, depending on what the library does, it is probably printing the median value. Do you have the library ?

DrAzzy:
This is not HTML.

Remove this:

</runningmedian.h></newping.h>

Does it even compile like that?

Nope, that was an error, I have removed that from my code :slight_smile: don’t know why it was included in the first place.

UKHeliBob:
The code creates an instance of a RunningMedian object named samples using the runningmedian library and it appears to create the median value using that and returns it from the getDistanceFromUltrasonicSensor() function to the distance_cm variable.

So, depending on what the library does, it is probably printing the median value. Do you have the library ?

Yup, it’s this one: http://playground.arduino.cc/Main/RunningMedian

If you didn't type that, my guess would be that you used an text editor that thought you were writing XML/HTML, and tried to finish the tags for you....

The comments suggest getting 7 samples, but the loop restricts it to 5. Why is that? Also, why not feed it 7 "bogus" values that you create and know the median for and see what the function gives back to you?