#include <EEPROM.h>
const int ledPin = 13; // Pin number for the LED
const int buttonOnPin = 2; // Pin number for the button to turn the LED on
const int buttonOffPin = 3; // Pin number for the button to turn the LED off
const int eepromAddress = 0; // Address in EEPROM to store the LED state
int ledState = LOW; // Initial LED state
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonOnPin, INPUT_PULLUP);
pinMode(buttonOffPin, INPUT_PULLUP);
Serial.begin(9600);
// Read the LED state from EEPROM
int storedState = EEPROM.read(eepromAddress);
if (storedState == HIGH) {
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
}
void loop() {
// Read the state of the "On" button
int buttonOnState = digitalRead(buttonOnPin);
// Read the state of the "Off" button
int buttonOffState = digitalRead(buttonOffPin);
// Check if the "On" button is pressed and the LED is off
if (buttonOnState == LOW && ledState == LOW) {
ledState = HIGH; // Turn on the LED
digitalWrite(ledPin, ledState);
// Store the updated LED state in EEPROM
EEPROM.write(eepromAddress, ledState);
Serial.println("LED is ON");
}
// Check if the "Off" button is pressed and the LED is on
if (buttonOffState == LOW && ledState == HIGH) {
ledState = LOW; // Turn off the LED
digitalWrite(ledPin, ledState);
// Store the updated LED state in EEPROM
EEPROM.write(eepromAddress, ledState);
Serial.println("LED is OFF");
}
}