HELP PLEASE

I have 2 HCSR04 Sensor and 2 displays , i want that each display show me 1 value from 1 sensor.

But the display's show me only the same value from one senor ? Where is the problem ? Here is my sketch:

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

LiquidCrystal_I2C lcd1(0x27,16,2);
LiquidCrystal_I2C lcd2(0x26,16,2);

//#define TRIG_PIN 5
//#define ECHO_PIN 11

const int TRIG_PIN1 = 5;
const int ECHO_PIN1 = 11;
const int TRIG_PIN2 = 6;
const int ECHO_PIN2 = 12;

long duration1;
float distance1;

long duration2;
float distance2;

void setup() {
Wire.begin();
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);

lcd1.init();
lcd1.begin(16,2);
lcd1.clear();
lcd1.backlight();

lcd2.init();
lcd2.begin(16,2);
lcd2.clear();
lcd2.backlight();

Serial.begin(9600);
}

void loop(){

digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);

duration1 = pulseIn(ECHO_PIN1,HIGH);

distance1 = (duration1*0.0343)/2;

if( distance1 >= 300 || distance1 <=2){
//
// lcd1.setCursor(0,1);
// lcd1.print("Out of Range");
}
else{

lcd1.setCursor(0,0);
lcd1.print("Distanz: ");
lcd1.setCursor(10,1);
lcd1.print(distance1);
//lcd.setCursor(13,1);
//lcd.print("cm");
}

digitalWrite(TRIG_PIN2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN2, LOW);

duration2 = pulseIn(ECHO_PIN2,HIGH);

distance2 = (duration2*0.0343)/2;

if( distance2 >= 300 || distance2 <=2){
//
// lcd2.setCursor(0,1);
// lcd2.print("Out of Range");
}
else{

lcd2.setCursor(0,0);
lcd2.print("Distanz: ");
lcd2.setCursor(10,1);
lcd2.print(distance1);
//lcd.setCursor(13,1);
//lcd.print("cm");
}
}

You're only printing distance1

Please remember to use code tags when posting code

Also try here

I change that but it's not working :frowning:

auditech:
I change that but it's not working :frowning:

I can't see your changes

how can i print distance2 ? its just changing the number , from 1 to 2 ? Also i have the exact same displays i am not sure if they have different hex adresses

auditech:
how can i print distance2 ?

In the same way you print distance1

Did you jumper the address pads on one of the I2C backpack to set the address on one backpack to 0x26?

Code tags and the IDE autoformat tool will make the code easier to read.

Nope , how can i do that ? :astonished:

Look here.. Found with a Google search.

i have done that but int is still not working i have just a blue screen at one of the display's , the another one is working

Can you post a photo of the LCD showing the change and the latest version of your code?

Do you see the 2 displays when you run the I2C scanner? What addresses are returned?

// I2C scanner by Nick Gammon.  Thanks Nick.

#include <Wire.h>

void setup() {
  Serial.begin (115200); //*****  make sure serial monitor baud matches *****

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}