Home‎ > ‎

Ping LCD

/* 
     
   The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
       * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor to LCD VO pin (pin 3)
 speaker to pin 10 and ground

  
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009 
   by Tom Igoe
   modified 30 april 2011
   by Ethan Flubacher
 
   
 */


#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4 ,3, 2);

const int pingPin = 7;

int soundPin = 10;
int soundPin2 = 9;

boolean serialOutput = 0; //Serialon/off

void setup() {

  if(serialOutput == true)
  {
    Serial.begin(9600);
  }
  pinMode(soundPin, OUTPUT);
    pinMode(soundPin2, OUTPUT);
  lcd.begin(20, 4); // change if using 16 by 2 lcd
  
  // note i used a 4 by 20 lcd you can change this above 
 
}

void loop()
{
  
  float duration, inches, cm, feet, meter;


  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  feet = microsecondsToFeet(duration);
  meter = microsecondsToMeter(duration);
  
  info( inches, cm, feet, meter);
  
  if(serialOutput == true)
  Serialinfo(inches, cm, feet, meter);
  
  if(cm <= 50.0 && cm > 0.1)
  {
  tone(soundPin, cm * 100);
  tone(soundPin2, cm * 100);
  }
  else
  {
  noTone(soundPin);
    noTone(soundPin2);
  }

 
  }



float 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 / 73.746 / 2.000;
}

float microsecondsToFeet(long microseconds)
{
  return microseconds / 884.952 / 2.000;
  // just multiplied inches speed by 12
}

float 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.000 / 2.000;
}

float microsecondsToMeter(long microseconds)
{
  return microseconds / 2900.000 / 2.000;
}


void Serialinfo( float inches, float cm, float feet, float meter) {
  if(cm == 0.0 && inches == 0.0 && feet == 0.0 && meter == 0.0)
{
       //you'll have to change this if you are using a 2x16
  Serial.println(" NO INPUT");
  
}
else
{
  if( inches >= 14.0 && cm >= 369.0 )
  {
    
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(feet);
  Serial.print("ft");
                   // you'll have to delete this if you us a 2x16
  Serial.println("MAX DISTANCE REACHED");  
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print(meter);
  Serial.print("mtr(s)");
  
}
else
{
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(feet);
  Serial.print("ft ");
  Serial.print(cm);
  Serial.println("cm, ");
  Serial.print(meter);
  Serial.print("mtr(s) ");
 
}
  }
}

void info(float inches, float cm, float feet, float meter)
{
    if(cm == 0.0 && inches == 0.0 && feet == 0.0 && meter == 0.0)
{
  lcd.setCursor(5,0);      //you'll have to change this if you are using a 2x16
  lcd.print(" NO INPUT");
  noTone(soundPin);
  noTone(soundPin2);
  delay(1000);
  lcd.clear();
}
else
{
  if( inches >= 14.0 && cm >= 369.0 )
  {
    
  lcd.setCursor(0,0);
  lcd.print(inches);
  lcd.print("in, ");
  lcd.print(feet);
  lcd.print("ft");
  lcd.setCursor(0,2);                  // you'll have to delete this if you us a 2x16
  lcd.print("MAX DISTANCE REACHED");
  lcd.setCursor(0,1);    
  lcd.print(cm);
  lcd.print("cm, ");
  lcd.print(meter);
  lcd.print("mtr(s)");
  delay(100);
  lcd.clear();
}
else
{
  lcd.print(inches);
  lcd.print("in, ");
  lcd.print(feet);
  lcd.print("ft");
  lcd.setCursor(0,1);
  lcd.print(cm);
  lcd.print("cm, ");
  lcd.print(meter);
  lcd.print("mtr(s)");
  lcd.setCursor(0,0);
  delay(100);
  lcd.clear();
}
  }
 
}





Comments