#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD Configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set I2C address to 0x27 for 16x2 LCD
// Switch Pins
const int SW1 = 14;
const int SW2 = 15;
const int SW3 = 16;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize switches as inputs with pull-up resistors
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
// Initialize LCD
Wire.begin(); // Initialize I2C
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
// Display initial message
lcd.setCursor(0, 0);
lcd.print("System Ready");
lcd.setCursor(0, 1);
lcd.print("Press a button");
Serial.println("System initialized. Ready for button input...");
}
void loop() {
// Check Switch 1
if (digitalRead(SW1) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button 1 active");
lcd.setCursor(0, 1);
lcd.print("Sensor 1 activated");
Serial.println("Button 1 pressed - Sensor 1 activated");
delay(300); // Debounce delay
while(digitalRead(SW1) == LOW); // Wait for button release
}
// Check Switch 2
if (digitalRead(SW2) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button 2 active");
lcd.setCursor(0, 1);
lcd.print("Sensor 2 activated");
Serial.println("Button 2 pressed - Sensor 2 activated");
delay(300); // Debounce delay
while(digitalRead(SW2) == LOW); // Wait for button release
}
// Check Switch 3
if (digitalRead(SW3) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button 3 active");
lcd.setCursor(0, 1);
lcd.print("Sensor 3 activated");
Serial.println("Button 3 pressed - Sensor 3 activated");
delay(300); // Debounce delay
while(digitalRead(SW3) == LOW); // Wait for button release
}
}