/*
 distanceBox.ino
 Alexander Hiam
 8/2012
 
 This program is in the public domain.
  
 Uses a Parallax PING ultrasonic distance sensor and a 16x2 LCD with
 SparkFun's SerLCD serial LCD backpack (part # 258) to measure and 
 display distances in inches and centimeters.

 Circuit: 
   -Connect PING))) 5V and GND pin to the Arduino's 5V and GND pins
   -Connect PING))) SIG pin to digital pin 3 on thr Arduino
   -Connect SerLCD 5V and GND pins to the Arduino's 5V and GND pins
   -Connect SerLCD RX pin to digital pin 2 on the Arduino
  
 
 SparkFun LCD backpack code slightly modified from:
   http://www.arduino.cc/playground/Learning/SparkFunSerLCD
   
 PING))) code slightly modified from 'Ping))) Sensor' example sketch:
   http://www.arduino.cc/en/Tutorial/Ping
 From 'Ping))) Sensor':
  "
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.
  "
 */

#include <SoftwareSerial.h>
// The SoftwareSerial library enables the use of Serial communications
// on pins other than 0 and 1

// Define pin constatnts:
#define PING 3
#define txPin 2

// Create a SoftwareSerial instance for the SerLCD:
SoftwareSerial LCD = SoftwareSerial(0, txPin);
// since the LCD does not send data back to the Arduino, we should only define 
// the txPin
const int LCDdelay=10;  // conservative, 2 actually works


long getDistance(int pingPin) {
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


void lcdPosition(int row, int col) {
  // Sets the position of the cursor to given row,column
  LCD.write(0xFE);                 //command flag
  LCD.write((col + row*64 + 128)); //position (see SerLCD datasheet)
  delay(LCDdelay);
}

void clearLCD(){
  LCD.write(0xFE);   //command flag
  LCD.write(0x01);   //clear command.
  delay(LCDdelay);
}

void lcdBrightness(int percent) { 
  // Sets the LCD backlight to the given percent brightness
  // Make sure it's within [0,100]:
  percent = constrain(percent, 0, 100);
  // Map it to the range that the SerLCD expects (see datasheet):
  percent = map(percent, 0, 100, 128, 157);
  LCD.write(0x7C);    // backlight command flag 
  LCD.write(percent); // set level
  delay(LCDdelay);
}

void setup() {
  // Initialize LCD software serial:
  pinMode(txPin, OUTPUT);
  LCD.begin(9600);
  
  // Set the backlight brightness and make sure it's clear:
  lcdBrightness(25);
  clearLCD();
  
  // Only need to write the static text one time here:
  lcdPosition(0,0);
  LCD.print("Distance:");
  lcdPosition(0,14);
  LCD.print("in");
  lcdPosition(1,14);
  LCD.print("cm");
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, centimeters;

  // Take a distance measurement and convert units:
  duration = getDistance(PING);
  inches = microsecondsToInches(duration);
  centimeters = microsecondsToCentimeters(duration);

  // If the last measurement was two digits and this time it's one,
  // the third digit of the previous value will remain, so we need 
  // to first clear every space that could have digits:
  lcdPosition(0,10);
  LCD.print("   ");
  
  // Then we can write the value:
  lcdPosition(0,10);
  LCD.print(inches);
  
  // Same thing here:
  lcdPosition(1,10);
  LCD.print("   ");
  lcdPosition(1,10);
  LCD.print(centimeters);

  // If we had no delay here the numbers would be changing on the 
  // LCD very quickly and would be hard to read:
  delay(300);
}

syntax highlighted by Code2HTML, v. 0.93pm6