How to insert 0 into memory

Hey friends
I use the EEPROM library to insert data into the non-volatile memory.
The value variable contains data from 1 to 20.
When I insert a number higher than 10 and then save a number smaller than 10 again, ie a number with one digit, the EEPROM memory stores the second digit of the previous number, i.e. if I entered 8 the second time instead of getting 08 I will get 80, I tried to enter 0 Before the number but as everyone knows that 0 is nothing .... and only a number higher than 1 will be kept.

How can I fix this?

EEPROM.write(0, variable);

Thank you.

Are you storing characters or binary values?

What's the type of variable?

Post your code, not an excerpt. So we can see what you actually do,

Please post your code

The variable type is int

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);

}

void loop() {
int Value = analogRead(A0); //Pin X-axis of joystick module
  if(Value < 21){
    
    EEPROM.write(0, Value);
  }
}

Please see replies #3 and #4

See Answer 6
That's all my code

An int is 2 bytes long. EEPROM.write stores 1 byte in memory.

Do you see the problem?

Use EEPROM.put().

I tried it does not help.
The length of an int can be either one bit if it is one digit or two bits if it is two numbers.
The storage is done well, the problem is that it does not add 0 if it is only one number, what it does is keep the number in the first bit and the second bit stays with the previous memory, so I have to save the memory a value of 0 for the first house and the number value for the second house.

No.
An int on an AVR is sixteen bits
Always

ah.. no that's not how it works. An Int is 2 bytes.. 16 bits. You need more schooling my friend.

Okay, so what's the solution ....

To start with try and be a bit more appreciative toward the people that are giving up their time to try and help you.

Then, if you can be bothered, have look at the documentation for the Arduino EEPROM, in particular the put() and get() methods.

Hey, hey, hey.
I really appreciate it...
Why do you think differently ???
(Maybe my English is not the best, and yes I can not pronounce myself correctly).
Overall I say I updated my code as shown below, and it still retains the previous memory.
So I wonder where I went wrong.

#include <EEPROM.h>

void setup() {
  Serial.begin(9600);

}

void loop() {
int Value = analogRead(A0); //Pin X-axis of joystick module
  if(Value < 21){
    
    EEPROM.put(0, Value);
  }
}

How did you get the value back from memory? With EEPROM.read()... or EEPROM.get() ?

READ the documentation... it is enlightening!

1 Like

After using EEPROM.write() always use EEPROM.commit() it will surely saves your values

Only on an ESP...

I used get
And I went through the documentation twice.
However the update of number one does not apply to the other
I update it at number 19 and then update again at number 4, my output is 49.

We can't see your code.