Ping))) With LCD

Hi, This is my code and I keep getting the error message " 'LiquidCrystal' does not name a type " Can anyone help me with this. Thanks, CoreyAlan15

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   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

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe

   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
__________________________________________________
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13;    // pin 13 will control the backlight
void setup() {
___________________________________________________
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);

  Serial.begin(9600);
}

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

  // 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);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 //__________________________This is my part of the code.
  if (Serial.available()) {
    delay(100);
    lcd.clear();
    while (Serial.available() > 0) {
    lcd.write(Serial.read());
 //_____________________________
  
  delay(1000)
}

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;
}

It looks to me that you forgot to include the LiquidCrystal library.

Go to Sketch - Import Library then click LiquidCrystal.

– OR –

Add this line to the top of your code:

#include <LiquidCrystal.h>

I just made a similar sketch myself, and ran across a odd error…

and this is the error code I get:

sketch_feb16a.cpp: In function ‘void loop()’:
sketch_feb16a:69: error: ‘lcd’ was not declared in this scope

/*stripped down code to read distance for Ping)) Ultrasonic
  and display output on both the serial console, and
  on the 1602 LCD display.

  The circuit:
 The Ping)) SIG to Digital Pin 7
 The VDD to +5 in parallel with the LCD Pin 2
 The GND to common GND bus (to pins 1 & 5 of LCD, and the Contrast
 POT.)

 * 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:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
*/






// this constant won't change.  It's the pin number
// of the sensor's output:

// include the library code:
#include <LiquidCrystal.h>

const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

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

  // 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);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println();
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of inches:
  lcd.print(inches);
  lcd.print(" In.");
    
  delay(100);
}

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;
}

So, what is wrong with this picture? Why is it trying to declare the lcd.setCursor function, as a variable?

gelfling6,

You're missing a line like this:

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

You guys should really look at my example code for a similar project I posted here:

http://arduino.cc/forum/index.php/topic,52028.0.html

My blog post has both full-feature version and nutshell version of the code:

http://liudr.wordpress.com/2011/02/12/arduino-parking-sensor/

It's also a featured project on instructables.com

http://www.instructables.com/id/Arduino-reverse-obstacle-sensor-for-cars/

500 views over the last 24 hours. :grin:

liudr:
gelfling6,

You’re missing a line like this:

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

:blush: OOPS! I knew I forgot something, But it also wasn’t working when I realized I inserted it in the wrong place too…

For those reading, the correct statement, Immediately after the #include is

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11 ,5, 4, 3, 2);

(No ideas what the 10 in your example would do.)

Once I inserted it in the right place (I had it under the void setup(), which was the wrong place.), it compiled right, and works.

Okay, since the LCD is occupying 6 wires, the ping occupying 1, leaves me few usable digital pins for the robot project I’m working on… But, so-far, It’s just test-bed stuff. I have a MEGA-2560 waiting once things are finalized.

Thanks for pointing it out… Typical newbie C programmer. :roll_eyes:

You're welcome. The 10 is RW, which you don't have to use and save one pin.

liudr: You're welcome. The 10 is RW, which you don't have to use and save one pin.

Ah, Okay.. The usual circuit I've been finding, has the r/w pin GND'ed, so that explains it.

Also noticed I forgot the lcd.begin(16,2) statement :( ... finally got everything working, and fine-tuned to display feet & inches.

Wish I could find it again, I had a nice 16 X 3 LCD I pulled from an old dead printer.. Finally finding datasheets to all kinds of devices I used to tinker with long ago.. One tough one, is a 2.4Ghz RF TX/RX module, MAR105C.. finding it in a lot of cheap wireless game controllers. Finding some source pages, but no readable documentation..