so, i want to make so that whenever a value is added/subtracted, it saves the value in EEPROM, so when i turn the arduino off and on again, it shows the same value.
the add button is fine, i added the value and turned off the arduino and it still shows the same. but when i subtract it (more than 255) and turned it off and on, it add 1 to the value (it probably doesnt reach 0? this happends after i turned it off and on).
i've tried to change the numbers a little bit...
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(18, 19, 20, 21, 22, 23);
int value = EEPROM.read(0);
int a = EEPROM.read(4); // the variable to count the loops "value" has made
int add_button = 27;
int minus_button = 26;
void setup() {
lcd.begin(16,2);
pinMode(add_button, INPUT);
pinMode(minus_button, INPUT);
}
void loop() {
if(digitalRead(add_button) == HIGH)
{
value = value + 125; // add them by 250 (the delay is too short)
EEPROM.write(0, value); // save the value
lcd.clear();
delay(50);
}
if(digitalRead(minus_button) == HIGH)
{
value = value - 125; // subtract them by 250
EEPROM.write(0, value); // save the value
lcd.clear();
delay(50);
}
if(value >= 255) // this is when the add button is pressed
{
value = value - 255; // loop value to 0
a++; // add the loop count
EEPROM.write(4, a); // save a
}
// the problem opening
if(value <= 0) // this is when the subtract button is pressed and it subtracts more than 255
{
value = value + 255; // loop to 255
a--; // subtract the loop count
EEPROM.write(4, a); // save a
}
// the problem closing
if(a >= 255)
{
a = a - 255; // a is 255 when starting the program (EEPROM default) and that affects the display algorithm below
}
lcd.setCursor(0,0);
lcd.print(value + (a * 255) - 5); // -5 because it displays 255 when starting the program (EEPROM default), i hope you understand what im doing here
}
looking at the problem, changing "if(value <= -1)" wont change anything
"if(value <= 1)" broke the display
"value = value + 256" with "if(value <= -1)" wont work, "value = value + 254" wont work either.
how do i solve this problem?