// Define the LED pin
const int ledPin = 13;
// Define the button pin (connected to external interrupt)
const int buttonPin = 2;
// Variable to store the LED state
volatile bool ledState = false;
void setup() {
// Configure the LED pin as output
pinMode(ledPin, OUTPUT);
// Configure the button pin as input with a pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Attach an interrupt to the button pin
// Trigger the interrupt on a falling edge (button press)
attachInterrupt(digitalPinToInterrupt(buttonPin), toggleLED, FALLING);
// Initialize the LED to OFF
digitalWrite(ledPin, LOW);
// Optional: Debug message
Serial.begin(9600);
Serial.println("Smart Home Light Control System Initialized");
}
void loop() {
// Main loop does nothing, as the logic is handled by the interrupt
}
// Interrupt Service Routine (ISR) to toggle the LED state
void toggleLED() {
// Debounce delay
delay(50);
// Toggle the LED state
ledState = !ledState;
// Apply the new state to the LED
digitalWrite(ledPin, ledState ? HIGH : LOW);
// Optional: Debug message
Serial.print("LED is now: ");
Serial.println(ledState ? "ON" : "OFF");
}