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