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