Pages: [1]   Go Down
Author Topic: problem coding 2 ping sensors with an l2C LCD  (Read 509 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everyone
am doing a small project using 2 ping sensors & L2C LCD
so far i was able to make one sensor to work fine with LCD
here is the code for it
Code:
#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);
}

i tried to use another code that can use 2 ping sensors
i can see the result in the Serial Monitor
but couldn't show it on the LCD screen
Code:
#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

int ultraSoundSignalPins[] = {8,9}; // X, Y Ultrasound signal pins
char *pingString[] = {"X= ","Y= "}; // just something to print to indicate direction
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.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  Serial.begin(9600);
}

void loop()
{
  unsigned long ultrasoundValue;
  for(int i=0; i < 2; i++)
  {
    ultrasoundValue = ping(i);
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    Serial.print("cm, ");   
    delay(100);
  }
 
  Serial.println();
  delay(300);
 }

//Ping function
unsigned long ping(int i)
{
  unsigned long echo;

  pinMode(ultraSoundSignalPins[i], OUTPUT); // Switch signalpin to output
  digitalWrite(ultraSoundSignalPins[i], LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignalPins[i], LOW); // Holdoff
  pinMode(ultraSoundSignalPins[i], INPUT); // Switch signalpin to input
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignalPins[i], HIGH); //Listen for echo
  return (echo / 2) / 29.1; //convert time to CM
  if (echo >= 90 || echo <= 0){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(2,0); //Print to screen
    lcd.print(echo);
    lcd.setCursor(5,0);
    lcd.print(" Cm");

        }
}

can you guys help me please smiley-sad
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4398
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are returning from your ping() function before it gets a chance to display the distance on the LCD.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are returning from your ping() function before it gets a chance to display the distance on the LCD.
ok i mad the return code the last thing in the program
Code:
  echo = pulseIn(ultraSoundSignalPins[i], HIGH); //Listen for echo
  echo = (echo / 2) / 29.1; //convert time to CM 
  if (echo >= 90 || echo <= 0){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(2,0); //Print to screen
    lcd.print(echo);
    lcd.setCursor(5,0);
    lcd.print(" Cm");

        }
  return (echo / 2) / 29.1; //convert time to CM     
  delay(300);
}

but now it shows the two distance in the same line
how can i separate them to make them shows on line 1 & line 2 ??
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1421
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could check the parameter you pass to ping(), and change the line number in the setCursor() call accordingly.
Logged

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could check the parameter you pass to ping(), and change the line number in the setCursor() call accordingly.


am sorry am not that good with coding & technical words

can you explain it more or show me what you mean
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4398
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In your ping() function change the second value in lcd.setCursor() to i
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks for the help
i was able to show them both in different line now
Code:
#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

int ultraSoundSignalPins[] = {8,9}; // X, Y Ultrasound signal pins
char *pingString[] = {"X= ","Y= "}; // just something to print to indicate direction
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.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  Serial.begin(9600);
}

void loop()
{
  unsigned long ultrasoundValue;
  for(int i=0; i < 2; i++)
  {
    ultrasoundValue = ping(i);
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    Serial.print("cm, ");   
    delay(100);
  }
 
  Serial.println();
  delay(300);
 }

//Ping function
unsigned long ping(int i)
{
  unsigned long echo;

  pinMode(ultraSoundSignalPins[i], OUTPUT); // Switch signalpin to output
  digitalWrite(ultraSoundSignalPins[i], LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignalPins[i], LOW); // Holdoff
  pinMode(ultraSoundSignalPins[i], INPUT); // Switch signalpin to input
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignalPins[i], HIGH); //Listen for echo
  echo = (echo / 2) / 29.1; //convert time to CM 
  if (echo >= 200 || echo <= 0){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("X=");
    lcd.setCursor(0,1);
    lcd.print("Y=");
    lcd.setCursor(3,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("X=");
    lcd.setCursor(0,1);
    lcd.print("Y=");
    lcd.setCursor(3,i); //Print to screen
    lcd.print(echo);
    lcd.setCursor(6,i);
    lcd.print(" Cm");
    delay (300);
        }
  //return (echo / 2) / 29.1; //convert time to CM     
  delay(300);
}

but they still alternate between them instead of showing both at the same time
is it because the sensor was set as "i" instead of 2 different function ?
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1421
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but they still alternate between them instead of showing both at the same time
is it because the sensor was set as "i" instead of 2 different function ?
It's because you only ping one sensor within the ping() function, then you clear the lcd screen, and print the result to it. When you ping the second sensor, the lcd.clear erases the first reading.

You might want to try separating the ping from the display. So, the code would be something like:

ping first sensor. save distance
ping second sensor. save distance
display both sensors

The first two lines be done in a single function, called twice, one for each sensor. Basically, everything you do now in ping(), up to where you store the distance. Just move the declaration of echo out of the function, making it global. Again, make echo a two-element array.

display can be one function, called once, which will simply display both echo values.
Logged

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

Pages: [1]   Go Up
Jump to: