Untitled

mail@pastecode.io avatar
unknown
actionscript
a year ago
1.7 kB
3
Indexable
Never
#include <LiquidCrystal_I2C.h>

// Set the pin for the IR sensors
int irSensor1 = 2;
int irSensor2 = 3;

// Initialize variables
int count = 0;  // Counter for number of visitors
int lastSensorState1 = 0; // Initialize state of IR sensor 1
int lastSensorState2 = 0; // Initialize state of IR sensor 2

// Set up LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize LCD display
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Visitor Counter");
  lcd.setCursor(0, 1);
  lcd.print("Count: ");

  // Set pin modes
  pinMode(irSensor1, INPUT);
  pinMode(irSensor2, INPUT);

  // Initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // Read the state of the IR sensors
  int sensorState1 = digitalRead(irSensor1);
  int sensorState2 = digitalRead(irSensor2);

  // Check if sensor 1 is triggered
  if (sensorState1 != lastSensorState1 && sensorState1 == HIGH) {
    count++; // Increase the count by 1
    lcd.setCursor(7, 1);
    lcd.print(count); // Display the count on the LCD
    Serial.println("Visitor entered"); // Print message to serial monitor
  }

  // Check if sensor 2 is triggered
  if (sensorState2 != lastSensorState2 && sensorState2 == HIGH) {
    count--; // Decrease the count by 1
    lcd.setCursor(7, 1);
    lcd.print(count); // Display the count on the LCD
    Serial.println("Visitor exited"); // Print message to serial monitor
  }

  // Update the last state of the IR sensors
  lastSensorState1 = sensorState1;
  lastSensorState2 = sensorState2;

  // Delay for 100 milliseconds to avoid false readings
  delay(100);
}