programming 2 ping sensor and LCD

Hi
am trying to program 2 ping sensors to show distance X & Y
and then show it on a LCD screen ( this one Amazon.com ) my screen only have 4 pins GND, VCC, SDA & SCL

i tried to write a program by myself that use 1 ping sensor & the lcd instead of 2 ping sesnsors
but for some reason it doesnt work :frowning:
can anyone help me please

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN  3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

const int pingPin = 8;

int n = 1;

LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() {
  lcd.begin(20, 4); //  <<----- My LCD was 20x4
  lcd.print(pingPin, OUTPUT);
  lcd.print(pingPin, INPUT);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  Serial.begin(9600);
 }

void loop() {
  int duration, distance;
  digitalWrite(pingPin, HIGH); //Write High for 1ms
  delayMicroseconds(1000);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(pingPin, HIGH); //Time pulse in
  distance = (duration/2) / 29.1; //Convert time to Cm
  if (distance >= 200 || distance <= 0){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(2,0); //Print to screen
    lcd.print(distance);
    lcd.setCursor(5,0);
    lcd.print(" Cm");
  }
  delay(300);
}

but for some reason it doesnt work

The code does something. You haven't described what it does.
You want it to do something. You haven't describes what you want it to do.

can anyone help me please

Not yet.

#define Rw_pin  1
#define Rs_pin  0

and

  Serial.begin(9600);

Can not be in the same code. You are using the pins for the LCD OR for the Serial port. You can NOT use them for both.

Your Display is using an I2C interface which only requires 2 data lines to work. the code your using is for connecting to a LCD without the I2C interface. The I2c on an Uno is connected to pins A4(SCA) and A5(SCL)

Here is an article where i added an lCD to a bot with a ping already on it. It is pretty simple.

Good Luck

sorry for the late replay

PaulS
thanks for the tip i will fix the error you pointed

Sarconastic
thanks for the help
i gave it a quick look & everything is explained in a simple way thanks :slight_smile:

where can i read about how to send information from ping sensor to the l2C interface ( how to show the distance on the screen)

ok i was able to make my code work somehow :sweat_smile:

here it is

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN  3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

const int pingPin = 8;

int n = 1;

LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() {
  
  lcd.begin(20, 4); //  <<----- My LCD was 20x4
  lcd.print(pingPin, OUTPUT);
  lcd.print(pingPin, INPUT);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  
 }

void loop() {
  int duration, distance, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH); //Time pulse in
  
  distance = (duration/2) / 29.1; //Convert time to Cm
  if (distance >= 90 || distance <= 0){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(2,0); //Print to screen
    lcd.print(distance);
    lcd.setCursor(5,0);
    lcd.print(" Cm");
  }
  delay(300);
}

what i want to know how to be able to connect a 2nd ping sensor & show it on the screen

  lcd.print(pingPin, OUTPUT);
  lcd.print(pingPin, INPUT);

What is this supposed to be doing?

what i want to know how to be able to connect a 2nd ping sensor & show it on the screen

Define a second pin and do just what you do with the first ping pin.

Do NOT replicate the code to read the ping sensor. Put that code in a function that takes an argument (the pin to diddle with) and returns the distance.

PaulS:
Define a second pin and do just what you do with the first ping pin.

Do NOT replicate the code to read the ping sensor. Put that code in a function that takes an argument (the pin to diddle with) and returns the distance.

can you explain it more ?
i defined a second pin
but what do you mean to put it in a function argument ?

but what do you mean to put it in a function argument ?

You've noticed that there is a digitalRead() function that takes a pin number as an argument? There are not 20 digitalReadN() functions that take no arguments.

You should create a function that takes a pin number as an argument, and returns a distance.