#include <EEPROM.h>
#define LED 5
#define BUTTON 6
byte laststate;
byte buttonstate;
byte ledstate = LOW;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
checkstate();
}
void loop() {
laststate = buttonstate;
buttonstate = digitalRead(BUTTON);
if(laststate == HIGH && buttonstate == LOW) { //makes the button toggle based off previous and current state of button
ledstate = !ledstate;
digitalWrite(LED, ledstate);
EEPROM.write(0,ledstate); //writes the ledstate to eeprom
}
}
void checkstate()
{
ledstate = EEPROM.read(0); //reads the first eeprom mem
if (ledstate == HIGH) //changes the ledstate based on the variable
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}