Hi guys! Do any of you know how to write a code that is able to measure distance of a target from the
MB1010 LV-MaxSonar-EZ1 sensor of a quadcopter? I have read in the datasheet that I have to wait for 20microseconds before pin 3 which is the analog voltage, can start to read and calculate the value. Do I have to write a code for the 20microseconds? Between I'm using analog voltage not pulse width. This is my current code, am I on the right path?
const int anPin = 0;
//variables needed to store values
long anVolt, inches, cm;
int sum=0;//Create sum variable so it can be averaged
int avgrange=10;//Quantity of values to average (sample size)
void setup() {
Serial.begin(9600);
}
void loop() {
pinMode(anPin, INPUT);
//MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.
//A simple fix is to average out a sample of n readings to get a more consistant reading.\
//Even with averaging I still find it to be less accurate than the pw method.\
//This loop gets 60 reads and averages them
for(int i = 0; i < avgrange ; i++) {
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
anVolt = analogRead(anPin);
sum += anVolt;
delay(10);
}
inches = sum/avgrange; cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
//reset sample total sum = 0;
delay(500);
}