Combine 2 programs (Sensor + display on I2C LCD)

Hello everyone !!

I have 1 Ultrasonic Range Finder SRF10 (SRF10 Ultra sonic range finder) and a I2C LCD ( I2C_TWI_LCD1602_Module__SKU__DFR0063_-DFRobot ).
They're connect with a arduino card (confirm by my teacher)

I have found 2 programs (one for the sensor, one for the LCD. They independently work, but I don't know how to connect them, to display the altitude on the LDC screen.
Here my programs :

Sensor :

#include <Wire.h>

void setup()
{
   Wire.begin();                // join i2c bus (address optional for master)
   Serial.begin(9600);          // start serial communication at 9600bps
}

int reading = 0;

void loop()
{
   // step 1: instruct sensor to read echoes
   Wire.beginTransmission(112); // transmit to device #112 (0x70)
                                // the address specified in the datasheet is 224 (0xE0)
                                // but i2c adressing uses the high 7 bits so it's 112
   Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)  
   Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50) 
                                // use 0x51 for centimeters
                                // use 0x52 for ping microseconds
   Wire.endTransmission();      // stop transmitting

   // step 2: wait for readings to happen
   delay(70);                   // datasheet suggests at least 65 milliseconds

   // step 3: instruct sensor to return a particular echo reading
   Wire.beginTransmission(112); // transmit to device #112
   Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
   Wire.endTransmission();      // stop transmitting

   // step 4: request reading from sensor
   Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

   // step 5: receive reading from sensor
   if(2 <= Wire.available())    // if two bytes were received
   {
     reading = Wire.read();  // receive high byte (overwrites previous reading)
     reading = reading << 8;    // shift high byte to be high 8 bits
     reading |= Wire.read(); // receive low byte as lower 8 bits
     Serial.println(reading);   // print the reading
   }

   delay(250);                  // wait a bit since people have to read the output :)

LCD

#include <Wire.h>
#include <BMP085.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
 
// Define LCD Pins
#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
 
// Initialize LiquadCrystal with pin setup
LiquidCrystal_I2C lcd(0x20,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
 
// Initialize BMP085 

 
void setup() {    
 
 
  lcd.begin(16,2);
 
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
 
}
 
void loop() {
 

 
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello");
  lcd.setCursor(0,1);
  lcd.print("Mister");
  
  delay(100);
}

Can you help me please ??

Merge the two setup () functions, then take all the code in loop() from the sonar sketch and make it a function returning a range.

Thank you for your fast answer !
How can I "make it a function returning a range" ?

int rangeInCm()
{
   Wire.beginTransmission(112);
   Wire.write(0);
   Wire.write(0x51);      // command sensor to measure in "inches" (0x50) 
                                // use 0x51 for centimeters
                                // use 0x52 for ping microseconds
   Wire.endTransmission();

   delay(70);

   Wire.beginTransmission(112);
   Wire.write(byte(0x02));
   Wire.endTransmission();

   Wire.requestFrom(112, 2);

   if(Wire.available() >= 2)
   {
     int reading = Wire.read();
     reading = reading << 8;
     reading |= Wire.read();
     return reading;   
   }
   else
     return -1;  // something went wrong
}

((uncompiled, untested)
Of course, that delay will prevent you doing other things, but you may not have other stuff to do.

Okay thank you. Now, the LDC is empty, the altitude is not display ...
Here the final program :

LCD :

#include <Wire.h>
#include <BMP085.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
 
// Define LCD Pins
#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
 
// Initialize LiquadCrystal with pin setup
LiquidCrystal_I2C lcd(0x20,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
 
// Initialize BMP085 

 
void setup() {    
 
 
  lcd.begin(16,2);
 
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
    Wire.begin();                // join i2c bus (address optional for master)
   Serial.begin(9600);          // start serial communication at 9600bps
}
 
void loop() {
 
  lcd.clear();
  lcd.setCursor(0,0);
  rangeInCm();
  delay(100);
}

Sensor :

#include <Wire.h>


int reading = 0;


 int rangeInCm()
{
   Wire.beginTransmission(112);
   Wire.write(0);
   Wire.write(0x51);      // command sensor to measure in "inches" (0x50) 
                                // use 0x51 for centimeters
                                // use 0x52 for ping microseconds
   Wire.endTransmission();

   delay(70);

   Wire.beginTransmission(112);
   Wire.write(byte(0x02));
   Wire.endTransmission();

   Wire.requestFrom(112, 2);

   if(Wire.available() >= 2)
   {
     int reading = Wire.read();
     reading = reading << 8;
     reading |= Wire.read();
     return reading;   
   }
   else
     return -1;  // something went wrong
}

Can you see an error ?

Yes, you're not displaying the value returned be the ranging function.

Hmm ok what can I do to resolve that ? (Sorry I'm beginner ...)

rangeInCm returns an int, so try doing something with that.