Reading serial data from maxSonar ultrasonic rangefinder

Hi, this is great info!

Would someone happen to share their wiring for the XL, for this code? I am more used to non-serial setups, and have had issues getting a signal back at all ... thanks!

J

Goldthing:
Hi all, just started with arduino and thought I would share this. There seems to be plenty on using the analog or pulse width outputs from the MaxSonar products but nothing about their serial use. This simple function reads the incoming serial data, converts it to an integer and returns the result.

A few things to note first.

  • The serial output on the MAxSonar is always enabled unless told otherwise. Attempting to hook it up directly to the serial port will cause communication problems between arduino and computer.

  • Serial data is inverted from the MaxSonar, to remove the need for extra circuitry I have used the very handy function in SoftwareSerial to flip the signal

  • This should work for all LV-EZ* and XL* modules, however the range returned will either be inches or centimeters depending on the type.

/* test function to Parse data from MaxSonar serial data

and return integer */

#include <SoftwareSerial.h>

#define txPin 3                                            //define pins used for software serial for sonar
#define rxPin 2

SoftwareSerial sonarSerial(rxPin, txPin, true);            //define serial port for recieving data, output from maxSonar is inverted requiring true to be set.

boolean stringComplete = false;

void setup()
{
  Serial.begin(9600);                                      //start serial port for display
  sonarSerial.begin(9600);                                //start serial port for maxSonar
  delay(500);                                              //wait for everything to initialize

}

void loop()
{
  int range = EZread();
  if(stringComplete)
  {
    stringComplete = false;                                //reset sringComplete ready for next reading

Serial.print("Range ");
    Serial.println(range);
    //delay(500);                                          //delay for debugging
  }
}

int EZread()
{
  int result;
  char inData[4];                                          //char array to read data into
  int index = 0;

sonarSerial.flush();                                    // Clear cache ready for next reading

while (stringComplete == false) {
    //Serial.print("reading ");    //debug line

if (sonarSerial.available())
    {
      char rByte = sonarSerial.read();                    //read serial input for "R" to mark start of data
      if(rByte == 'R')
      {
        //Serial.println("rByte set");
        while (index < 3)                                  //read next three character for range from sensor
        {
          if (sonarSerial.available())
          {
            inData[index] = sonarSerial.read();
            //Serial.println(inData[index]);              //Debug line

index++;                                      // Increment where to write next
          } 
        }
        inData[index] = 0x00;                              //add a padding byte at end for atoi() function
      }

rByte = 0;                                          //reset the rByte ready for next reading

index = 0;                                          // Reset index ready for next reading
      stringComplete = true;                              // Set completion of read to true
      result = atoi(inData);                              // Changes string data into an integer for use
    }
  }

return result;
}





Hope this helps someone out there. Thanks to everyone who posts their code, I would never have got anywhere without you.