#include <LiquidCrystal_I2C.h>
// Assuming you have defined irSensorPin and objectCount somewhere in your code.
const int irSensorPin = 2; // Replace with the actual pin number
int objectCount = 0;
// Assuming you have created an instance of the LiquidCrystal_I2C class named lcd.
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) if needed, and adjust columns and rows.
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialize the LCD
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Object Count:");
lcd.setCursor(0, 1);
pinMode(irSensorPin, INPUT);
}
void loop() {
if (digitalRead(irSensorPin)) {
// Object detected, wait until the sensor goes low
while (digitalRead(irSensorPin)) {
delay(10);
}
// Increment object count and update LCD display
objectCount++;
lcd.setCursor(0, 1);
lcd.print(objectCount);
// Print message to Serial Monitor
Serial.println("Object detected");
}
// Add a delay to prevent rapid detection and count increment
delay(100);
}