#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define sensor pins
const int entrySensor = 2; // Entry sensor connected to digital pin 2
const int exitSensor = 3; // Exit sensor connected to digital pin 3
// Variables to hold sensor states
int entryState = 0;
int exitState = 0;
// Inventory counter
int inventoryCount = 0;
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Print a starting message
lcd.setCursor(0, 0);
lcd.print("Inventory Counter");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000); // Wait for 2 seconds
// Set sensor pins as input
pinMode(entrySensor, INPUT);
pinMode(exitSensor, INPUT);
// Clear LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Count:");
lcd.setCursor(0, 1);
lcd.print(inventoryCount);
}
void loop() {
// Read the state of the sensors
entryState = digitalRead(entrySensor);
exitState = digitalRead(exitSensor);
// If entry sensor detects object
if (entryState == LOW) { // LOW because IR sensor gives LOW when object detected
inventoryCount++;
updateDisplay();
delay(500); // Simple debounce and avoid multiple counts
}
// If exit sensor detects object
if (exitState == LOW) {
if (inventoryCount > 0) {
inventoryCount--;
}
updateDisplay();
delay(500);
}
}
// Function to update the LCD display
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Count:");
lcd.setCursor(0, 1);
lcd.print(inventoryCount);
}
Entry Sensor
Exit Sensor