#include <LiquidCrystal.h>
// Define LCD and component pins
static const uint8_t LCD_RS = 7U;
static const uint8_t LCD_E = 8U;
static const uint8_t LCD_D4 = 9U;
static const uint8_t LCD_D5 = 10U;
static const uint8_t LCD_D6 = 11U;
static const uint8_t LCD_D7 = 12U;
static const uint8_t LED_PIN = 2U;
static const uint8_t BUZZER_PIN = 3U;
static const uint8_t BUTTON_PIN = 4U;
static LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup(void) {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
lcd.begin(16U, 2U); // Initialize LCD (16 columns, 2 rows)
lcd.setCursor(0U, 0U);
lcd.print("LED: OFF BZR: OFF");
lcd.setCursor(0U, 1U);
lcd.print("BTN: NOT Pressed");
}
void loop(void) {
uint8_t buttonState = digitalRead(BUTTON_PIN);
uint8_t ledState = digitalRead(LED_PIN);
uint8_t buzzerState = digitalRead(BUZZER_PIN);
// Toggle LED and Buzzer when button is pressed
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
// Update LCD display
lcd.setCursor(5U, 0U);
if (ledState == HIGH) {
lcd.print(" ON ");
} else {
lcd.print("OFF");
}
lcd.setCursor(13U, 0U);
if (buzzerState == HIGH) {
lcd.print(" ON ");
} else {
lcd.print("OFF");
}
lcd.setCursor(5U, 1U);
if (buttonState == LOW) {
lcd.print("Pressed ");
} else {
lcd.print("NOT Pressed");
}
delay(200U); // Small delay for debounce
}