Arduino to arduino serial communication with sensor and LCD

Hello there! I want to use two arduino, each one is connected to a ultrasonic sensor and to a LCD screen (I2C). I want to show the read of sensor 1 in LCD 2 and show the read of sensor 2 in LCD 1 using serial communication. Both LCD show the right value the first time, but after that it starts showing wrong values and I don't know why. This is my script:

#include <Adafruit_LiquidCrystal.h>
#define trigPin 9
#define echoPin 8
int incomingByte = 0;
Adafruit_LiquidCrystal lcd_1(0);
#define trigPin 9
#define echoPin 8
void setup(){
  lcd_1.begin(16, 2);
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  

}
void loop(){
  long sure, mesafe; // start of the sensor config
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  sure=pulseIn(echoPin, HIGH); // end od sensor config
  mesafe=(sure/2)/29.1; //value of sensor in cm
  Serial.print(mesafe, DEC); // printing value in serial
  delay(1000);
  if (Serial.available() > 0) { //checking for info
    incomingByte = Serial.read(); // saving value in variable
    Serial.print(incomingByte, DEC);
    digitalWrite(12, HIGH);//turning on a led if it's getting data
  }
  lcd_1.print(incomingByte, DEC);
  lcd_1.print(" cm");
  delay(1000);

  lcd_1.clear();
  digitalWrite(12, LOW);
  delay(1000);
  }

Welcome! Sorry, I would like to help but I cannot read frizzy. Posting a real schematic with links to technical information on each of the hardware devices would help us help you. Even labeled hand drawn schematic is better. If you cannot draw you can download KiCad and use that. Many parts have the same name but are different. I am curious as to what displays you are using, apparently they have built in pull up resistors as I see none. I do not have a clue what the prototype board is there for, it is not being used other then splicing wires.

You would do well to review Serial Input Basics to see how to best frame and read serial messages.

@juancarlossegovia
You printing mesafe variable to serial as string, but reading it as single byte. It will not work.
Please follow an advice in post #3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.