The aim of the code is to create a toggle switch (ON/OFF) that does something in each toggle (ON/OFF). I want to store the value of the toggle in the EEPROM memory just in case the power goes off or whatever. Chip: ATtiny85 programmed with Arduino Uno as ISP. Here is my code so far.
My setup:
#include <EEPROM.h>
const int sleeppin= 1; //connected to an LED
const int buttonpin = 3; //button pin
int address = 0; //toggle switch memory address
boolean buttonstate; // variable for reading the pushbutton status
boolean curtainstate = LOW; //as of now I set it to low to begin with
void setup() {
pinMode(buttonpin, INPUT);
pinMode(sleeppin, OUTPUT);
}
and here is the actual code:
void loop() {
buttonstate = digitalRead(buttonpin); //read button
EEPROM.get(address, curtainstate); //read previous state
if (buttonstate == curtainstate) { //if the button and state match
digitalWrite(sleeppin, HIGH); //turn on LED
}
else { //if they dont match
digitalWrite(sleeppin, LOW); //turn LED off
dosomething(curtainstate); //Do something that depends on the curtain state
EEPROM.put(address, !curtainstate); //rewrite the with the new state
}
where the dosomething() function is defined as
int dosomething(boolean io){
if (io == LOW){
//DOES, SAY, A
}
if (io == HIGH){
//DOES, SAY, B
}
The output of the code is A exclusively all the time regardless of pressing the button or disconnecting and connecting the power.
My guess: Im using EEPROM.get() and EEPROM.put() sintax incorrectly.
Thanks in advance!