Is it possible to save a variable's value even when my arduino uno is turned off?
void preset();
int x;
int fhours;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.begin();
lcd.backlight();
if (x == 1){
lcd.clear();
lcd.print("You have a current preset");
lcd.setCursor(0, 1);
lcd.print(fhours);
delay(2000);
}
else{
lcd.clear();
lcd.print("You dont have a current preset");
delay(2000);
preset();
}
}
void loop()
{
}
void preset(){
fhours = 10;
x = 1;
lcd.clear();
lcd.print("You created a preset");
delay(2000);
}
The variable that I wanted to save is the variable int x. So the next time I turned off my arduino uno and turned it back on again the value of x will be 1 but whenever I do that it resets back. How can I save a variable's value in arduino uno? Thanks!
The Arduino has eeprom memory that can store data when the board is powered down. You need to save the data before power down (duh) and check to see if there is stored data on power up. Search on Arduino eeprom for more information.
void preset();
int x;
int fhours;
#include <avr/eeprom.h>// add avr eeprom library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin();
lcd.backlight();
x = eeprom_read_word(0);// read the value at address 0 into x
if (x == 1) {
lcd.clear();
lcd.print("You have a current preset");
lcd.setCursor(0, 1);
lcd.print(fhours);
delay(2000);
}
else {
lcd.clear();
lcd.print("You dont have a current preset");
delay(2000);
preset();
}
}
void loop();
void preset() {
fhours = 10;
x = 1;
if (eeprom_is_ready) eeprom_update_word(0, x);// write the value of x into address 0
lcd.clear();
lcd.print("You created a preset");
delay(2000);
}