I think you meant TTL. Second, that honorary mention seems...odd. Anyway, here's the whole thing:
/*
HRXL MaxSonar WR TTL serial communication example sketch
Ultrasonic rangefinding
Original bits by Logan Park, Ph.D., in honor of his astonishingly beautiful wife.
Pretty much everything else is...
based on: http://www.maxbotix.com/documents/HRXL-MaxSonar-WR_Datasheet.pdf
and on the Arduino Mega 2560 ADK: http://arduino.cc/en/Main/ArduinoBoardADK
and on this handy ASCII chart: http://www.csgnetwork.com/asciiset.html
and a bunch of posts on the Arduino forums.
1. Connect pin 5 (tty/serial) of the ranging unit to pin 19 (Serial1 RX) on the Mega 2560.
2. Connect pin 6 of the ranging unit to any 5V supply on the Mega 2560.
3. Connect pin 7 of the ranging unit to any GND on the Mega 2560.
*/
int current_range_reading = -99;
void setup(){
Serial.begin(115200); // IMPORTANT: Set your serial monitor to this.
Serial1.begin(9600); // This is the baud rate needed for MaxBotics TTL sonar.
}
void loop(){
if (Serial1.available() > 5) // Then at least one complete range, 6 characters long, is stored in the RX buffer.
{
int inByte = Serial1.read(); // Examine the first stored character and decide what to do.
if(inByte == 'R')
{
int thousands = (Serial1.read() - '0') * 1000; // Take and convert each range digit to human-readable integer format.
int hundreds = (Serial1.read() - '0') * 100;
int tens = (Serial1.read() - '0') * 10;
int units = (Serial1.read() - '0') * 1;
int cr = Serial1.read(); // Don't do anything with this, just clear it out of the buffer with the rest.
// Assemble the digits into the range integer.
current_range_reading = thousands + hundreds + tens + units;
if(current_range_reading <= 300) //This is the minimum reading for the HRXL MaxSonar WR 7380, not the actual distance
Serial.println("too close!");
else if(current_range_reading >= 5000) //This is the max reading for the HRXL MaxSonar WR 7380, not the actual distance
Serial.println("too far!");
else
{
Serial.print("Range (mm): ");
Serial.println(current_range_reading);
}
}
else if( inByte == "\r" ) // Carriage Return character, oops!
{
//Serial.println();
}
else if( inByte == -1 ) // Just in case! This shouldn't happen if Serial1.available() returns true.
{
Serial.println("RX buffer empty, wth?");
return;
}
}
}