I'm testing a MaxBotix MB7067 ultrasonic sensor and using the MaxBotix library and a test program from Github. Here's the program:
/*
Maxbotix advanced test took out Serial and Analog See original
Instructions:
Filters:
* NONE (default): No filtering
* MEDIAN: Take the median of a sample
* HIGHEST_MODE: Take the mode of a sample. In case more than
one mode, the highest one is returned
* LOWEST_MODE: Take the mode of a sample. In case more than
one mode, the lowest one is returned
* BEST: Take the mode of a sample. In case more than one
mode is found, the median is returned
* SIMPLE: Continue reading until sample_size consecutive readings
are issued
*/
#include "Maxbotix.h"
Maxbotix rangePW(8, Maxbotix::PW, Maxbotix::XL, Maxbotix::BEST);//use pulse input pin 8
//Maxbotix rangeSensorAD(A0, Maxbotix::AN, Maxbotix::LV, Maxbotix::BEST, 9);//Use analog pin A0
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long start;
Serial.println("Reading...");
start = millis();
Serial.print(" BEST: ");
Serial.print(rangePW.getRange());//same as Mode...best of sample is largest value
//rangePW.toInches;
Serial.print("cm - ");
Serial.print(millis() - start);
Serial.println("ms - ");
//printArray(rangePW.getSample(), rangePW.getSampleSize());//print all array values
Serial.print(" - Highest Mode: ");
Serial.println(rangePW.getSampleMode(true));//highest value of range
Serial.print(" - Lowest Mode: ");
Serial.println(rangePW.getSampleMode(false));//lowest value of range
Serial.print(" - Median: ");
Serial.println(rangePW.getSampleMedian());//middle value of range-NOT average
delay(3000);//delay for testing only
}
//prints out all values in array. Use for testing
void printArray(float* array, uint8_t array_size) {
Serial.print("[");
for (int i = 0; i < array_size; i++) {
Serial.print(array[i]);
if (i != array_size - 1) {
Serial.print(", ");
}
}
Serial.print("]");
}
With a hard reflector 61 cm (2 feet) away, the program reports 58 cm (see attachment) which is close enough for the girls I go out with. But the value it reports for #of mS is not correct because this sensor has a scale output of 58uS/cm so that, at 61 cm, it should read 3.5 mS, NOT the 420mS it is reporting. I verified this with a scope showing exactly 3.44 mS pulsewidth. So why is the millis function reporting this erroneous value?