#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize I2C LCD (Address 0x27 is common)
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C Address 0x27 for 16x2 LCD
// Pin definitions
int POT1 = A3;
int POT2 = A2;
int BUTTON1 = 2;
int LED = 4;
int BUTTON2 = 3;
int BUZZER = 5;
volatile bool ledTriggered = false;
volatile bool buzzerTriggered = false;
unsigned long ledStartTime = 0;
unsigned long buzzerStartTime = 0;
bool displayUpdated = false; // Flag for preventing repeated LCD updates
void ISR_Button1() {
ledTriggered = true;
ledStartTime = millis();
displayUpdated = false; // Allow LCD update
}
void ISR_Button2() {
buzzerTriggered = true;
buzzerStartTime = millis();
displayUpdated = false; // Allow LCD update
}
void setup() {
// Initialize I2C LCD
lcd.init();
lcd.backlight(); // Turn on backlight
// Set pin modes
pinMode(POT1, INPUT);
pinMode(POT2, INPUT);
pinMode(LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
// Ensure outputs are OFF initially
digitalWrite(LED, LOW);
digitalWrite(BUZZER, LOW);
// Attach interrupts
attachInterrupt(digitalPinToInterrupt(BUTTON1), ISR_Button1, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON2), ISR_Button2, FALLING);
}
void loop() {
// Read potentiometer values
int pot1_value = analogRead(POT1);
int pot2_value = analogRead(POT2);
// Convert to voltage
float pot1_voltage = (pot1_value / 1023.0) * 2.5;
float pot2_voltage = (pot2_value / 1023.0) * 5.0;
// Handle LED (8 seconds) logic
if (ledTriggered) {
digitalWrite(LED, HIGH);
if (!displayUpdated) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LED ISR1 ON");
lcd.setCursor(0, 1);
lcd.print("8 seconds");
displayUpdated = true; // Prevent multiple updates
}
if (millis() - ledStartTime >= 8000) { // After 8 seconds
digitalWrite(LED, LOW);
ledTriggered = false;
lcd.clear(); // Clear LCD after LED off
}
}
// Handle buzzer (6 seconds) logic
if (buzzerTriggered) {
digitalWrite(BUZZER, HIGH);
if (!displayUpdated) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BZR ISR2 ON");
lcd.setCursor(0, 1);
lcd.print("6 seconds");
displayUpdated = true; // Prevent multiple updates
}
if (millis() - buzzerStartTime >= 6000) { // After 6 seconds
digitalWrite(BUZZER, LOW);
buzzerTriggered = false;
lcd.clear(); // Clear LCD after buzzer off
}
}
// If no LED or buzzer is active, display potentiometer values
if (!ledTriggered && !buzzerTriggered) {
lcd.setCursor(0, 0);
lcd.print("POT1: ");
lcd.print(pot1_voltage, 1);
lcd.print("V ");
lcd.print(pot1_value);
lcd.setCursor(0, 1);
lcd.print("POT2: ");
lcd.print(pot2_voltage, 1);
lcd.print("V ");
lcd.print(pot2_value);
}
delay(500); // Refresh the display every 500ms
}