#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD I2C address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the IPR sensor pins
const int iprSensor1 = 2;
const int iprSensor2 = 4;
void setup() {
// Initialize the IPR sensor pins as input
pinMode(iprSensor1, INPUT);
pinMode(iprSensor2, INPUT);
// Initialize I2C communication for ESP32 with your specific pins
Wire.begin(26, 32); // SDA=26, SCL=32
// Initialize the LCD
lcd.init();
// Turn on the LCD backlight
lcd.backlight();
}
void loop() {
// Read the state of the IPR sensors
int sensor1State = digitalRead(iprSensor1);
int sensor2State = digitalRead(iprSensor2);
// Update the LCD only when an object is detected by either sensor
if (sensor1State == HIGH || sensor2State == HIGH) {
lcd.clear(); // Clear the display to show the new message
lcd.setCursor(0, 0); // Move cursor to the first line
lcd.print("Object Detected");
// Show which sensor(s) detected the object
lcd.setCursor(0, 1); // Move cursor to the second line
if (sensor1State == HIGH) {
lcd.print("S1: Yes ");
} else {
lcd.print("S1: No ");
}
if (sensor2State == HIGH) {
lcd.print("S2: Yes");
} else {
lcd.print("S2: No");
}
} else {
// Optional: Clear the display or show a different message when no object is detected
// lcd.clear();
}
delay(500); // Delay to reduce LCD flickering and rapid updates
}