#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ENTRY_SENSOR 2
#define EXIT_SENSOR 3
#define AC_PIN 4 // LED pin for AC control (simulating temperature with PWM)
int occupancyCount = 0;
int acTemperature = 0; // Temperature of the AC
// Initialize the LCD with I2C address 0x27 and 16 columns x 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(ENTRY_SENSOR, INPUT);
pinMode(EXIT_SENSOR, INPUT);
pinMode(AC_PIN, OUTPUT);
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Fan Control System");
delay(2000); // Wait for 2 seconds
lcd.clear();
Serial.begin(9600);
}
void loop() {
static bool entryState = false;
static bool exitState = false;
// Detect Entry
if (digitalRead(ENTRY_SENSOR) == HIGH && !entryState) {
entryState = true;
occupancyCount++;
Serial.println("Person Entered! Count: " + String(occupancyCount));
} else if (digitalRead(ENTRY_SENSOR) == LOW) {
entryState = false;
}
// Detect Exit
if (digitalRead(EXIT_SENSOR) == HIGH && !exitState) {
exitState = true;
if (occupancyCount > 0) {
occupancyCount--;
}
Serial.println("Person Exited! Count: " + String(occupancyCount));
} else if (digitalRead(EXIT_SENSOR) == LOW) {
exitState = false;
}
// Adjust AC Temperature Based on Occupancy Count
if (occupancyCount == 0) {
acTemperature = 0; // AC OFF (LED OFF)
} else if (occupancyCount <= 1) {
acTemperature = 24; // 1 people → AC at 24°C
} else if (occupancyCount <= 2) {
acTemperature = 22; // 2 people → AC at 22°C
} else if (occupancyCount <= 9) {
acTemperature = 20; // 3 to 6 people → AC at 20°C
} else {
acTemperature = 18; // 6+ people → AC at 18°C
}
// Display AC Temperature and Occupancy on LCD
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(occupancyCount);
lcd.setCursor(0, 1);
if (acTemperature == 0) {
lcd.print("AC: OFF");
} else {
lcd.print("AC Temp: ");
lcd.print(acTemperature);
lcd.print(" C");
}
Serial.print("AC Temperature: ");
if (acTemperature == 0) {
Serial.println("OFF");
} else {
Serial.println(acTemperature);
}
delay(500); // Small delay to update display
}