Code to use LCD Display and Ultrasonic Sensor (3 pin sensor)

All the tutorials I found to make this work were based on 4 pin sensors, for example the "Ultrasonic Ranging Module HC-SR04" which are the most commonly used, it is connected like this:

Sensor Pin 1 (Gnd) –> Arduino Ground
Sensor Pin 2 (Echo) –> Arduino Pin 11
Sensor Pin 3 (Trig) –> Arduino Pin 12
Sensor Pin 4 (Vcc) –> Arduino +5V

But mine has 3 Pins, SIG, VCC and GND

I tried following two of arduino's tutorials, Serial Input- From the "Liquid Crystal" Library, and the "Ping Ultrasonic Range Finder" from "Examples" http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

Both codes individually work great with the lcd display and the sensor.

So I tried putting them together, so that the lcd display would read the input from the sensor in the serial monitor and display it. But even if it doesnt show an "error" message, my code wont work.

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Liquid crystal

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  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();
 
  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.
  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;

// when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
}
}*
```

What am I doing wrong?

Why on Earth does a function designed to convert echo times do anything with the serial line and the LCD?

(When in fact, it returns first)

So, in loop() you have code that displays the stuff you want on the serial monitor.
Where do you think the LCD display code should go?
(Hint: not after a return statement in another function)

Alright, so complete code.

No error message appears and the info in the monitor appears swiftly. It just doesn't display in the LCD screen.

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Liquid crystal

// this constant won't change. pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  // variables for duration of the ping, 
  //  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();
  
  delay(100);

// when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
  }

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.
  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
  // half of the distance travelled.
  return microseconds / 29 / 2;

  
}

I try to figure out what most of this code means but maybe im letting some key piece behind.
Am I forgetting to look at something important??

(Im thinking something at the end of the code, the return functions, again, very little idea)

if (Serial.available())

Maybe I'm not understanding your requirements, but what are you sending over the serial interface?

It's a distance sensor, in the serial monitor I see the distance of the closest object to the sensor.

I want to send that distances and make them appear in the display.

No, i'm asking what are you sending to the Arduino that you need to check for incoming serial data?
If all you need to do is write the value of "cm" or "inches" to the LCD, then I don't see why you're checking to see if the Arduino has received anything.

I think maybe im doing it all wrong.
All I want, is to display the distance in the lcd screen.

lcd.print (cm); ?