Can arduino write variables ?

Hello fello arduinos,

My question is like this. Lets say the value of
int number = 5;

Then i execute a calculation and the value is changed. Lets say

number = 20;

So next time I switch on the arduino I want the "int number" to be 20

Is it possible guys ?

Thanks in advance :slight_smile:

When you switch off the power, all variables are lost and your sketch starts from the beginning.
You need to store the value in the EEPROM or on an external SD card or a battery-buffered RTC chip with RAM

olf2012:
When you switch off the power, all variables are lost and your sketch starts from the beginning.
You need to store the value in the EEPROM or on an external SD card or a battery-buffered RTC chip with RAM

How to store that value in EEPROM ??? Any examples ?

Use the eeprom write anything library.
In setup(), read the value back from eeprom.

CrossRoads:
Use the eeprom write anything library.
In setup(), read the value back from eeprom.

Thanks and sorry for this noob question. Just now took a look at examples

olf2012:
When you switch off the power, all variables are lost and your sketch starts from the beginning.
You need to store the value in the EEPROM or on an external SD card or a battery-buffered RTC chip with RAM

CrossRoads:
Use the eeprom write anything library.
In setup(), read the value back from eeprom.

Pls guide me. I used the following example

#include <EEPROM.h>

int re1 = 6;
char inr = 0;

void setup() {
pinMode(re1, OUTPUT);
Serial.begin(9600);
}

void loop() {

int re1 = 6;

if (Serial.available() > 0)
{

inr = Serial.read();
int state = inr - '0';
if (inr == '1')
{
int ans = EEPROM.read(re1);
Serial.println(state);
digitalWrite(re1, !ans);
EEPROM.write(re1, !ans);
delay(100);
}
else
{
Serial.println("error");
}

If i switch off and On again I dont get HIGH from pin 6 :o

Can someone help pls ?