I want to save different values on address 0 and 1 on eeprom I tried to change it but when I put data on address 0 and also put data on address 1 the address 0 change its value stored into random number and vice versa .. Is it possible to change the value of respective address without affecting others ..
CODE
in the setup
int eepromSize=EEPROM.length();
Serial.print("EEPROM found, size=");
Serial.println(eepromSize);
// Get the last fade value (as an integer)
EEPROM.get(0, WATEREQFORN1);
Serial.print("Last Value N1: ");
Serial.println(WATEREQFORN1);
EEPROM.get(1, WATEREQFORN2);
Serial.print("Last Value N2: ");
Serial.println(WATEREQFORN2);
The user will input the value that will be save in eeprom i use switch because the user will input like this example .. 100A or 23B
case 'A':
Serial.print("Case A got ");
Serial.println(val);
WATEREQFORN1 = val;
EEPROM.put(0, WATEREQFORN1);
Serial.print("DATA SAVE TO EEPROM");
break;
case 'B':
Serial.print("Case B got ");
Serial.println(val);
WATEREQFORN2 = val;
EEPROM.put(1, WATEREQFORN2);
Serial.print("DATA SAVE TO EEPROM");
break;
The problem is, when I change/write value into address 0 and after that change/write also into address 1 the value on address 0 also change into different random numbers.
4 Answers 4
The variable names in all upper case is a style problem you should address. Conventionally, C and C++ programmers use all-upper-case names to designate defined constants or macros. Contravening that convention can lead to confusion if other people use or maintain the code. Instead of WATEREQFORN1
consider waterEq1
, etc.
The problem of your second value overwriting part of the first in EEPROM is an allocation problem. Presumably the WATEREQ...
variables are multiple bytes in length. EEPROM.get()
and put()
read and write data types with multiple bytes using several consecutive bytes of EEPROM. For example, if WATEREQFORN1
is a real and takes 4 bytes, it will occupy cells 0, 1, 2, 3 in EEPROM.
The following code snippet shows a way to automatically and systematically allocate EEPROM space. The sizeof
operator returns the size in bytes of its argument. Thus, an expression like WaterAddr2=WaterAddr1+sizeof waterEq1
correctly sets WaterAddr2
to be the next available cell after waterEq1
, no matter what type waterEq1
is.
enum { WaterAddr1=0, WaterAddr2=WaterAddr1+sizeof waterEq1, DelayAddr=WaterAddr2+sizeof waterEq2 };
...
case 'A':
waterEq1 = val;
EEPROM.put(WaterAddr1, waterEq1);
break;
case 'B':
waterEq2 = val;
EEPROM.put(WaterAddr2, waterEq2);
break;
case 'D':
Delay = val;
EEPROM.put(DelayAddr, Delay);
break;
...
-
Thanks I think thats the problem , I follow your advice and It Works like a charm .. :Dkaisar Great– kaisar Great2017年05月08日 16:37:56 +00:00Commented May 8, 2017 at 16:37
-
the allocation is the problem ..kaisar Great– kaisar Great2017年05月08日 16:38:30 +00:00Commented May 8, 2017 at 16:38
EEPROM.put() and EEPROM.get() write or read the object you pass them. An int
is 2 bytes so that is what they write or read. It sounds like you intended for the functions to only handle 1 byte. In that case, make the your variable's data type something that is 1 byte, such as int8_t
; otherwise allocate 2 bytes of EEPROM to each.
Update: It works because you had only allocated 1 byte in EEPROM for each variable but you store 2-byte variables. Now you're storing 1-byte variables, which matches your EEPROM allocation.
-
So instead of
unsigned int
i will usebyte
? because i only want to save numbers.kaisar Great– kaisar Great2017年05月08日 13:27:52 +00:00Commented May 8, 2017 at 13:27 -
It works now that i change the
unsigned int WATEREQFORN1;
intobyte WATEREQFORN1
i dont know why but it works , when i write new data toWATEREQFORN2
theWATEREQFORN1
dont change into random numberskaisar Great– kaisar Great2017年05月08日 13:57:59 +00:00Commented May 8, 2017 at 13:57
Probably WATEREQFORN1 is an int or word, at least more than one byte.
If you write a 2 byte value on address 0, it will occupy address 0 and 1.
When you overwrite address 1 (again with a 2 byte value), it will occupy addres 2 and 3, destroying the LSB part of the value written to byte 0.
Try writing on address 0 and 2 (or 0 and 4 when using 4 bytes).
Is it possible to change the value of respective address without affecting others ..
Yes, if the two values are 8-bit types.
Otherwise, No.
WATEREQFORN1
andWATEREQFORN2
? You only show a bit of your code, and have omitted what may be the most important parts - your variable definitions.unsigned int WATEREQFORN1; unsigned int WATEREQFORN2;
That is the value the user enter sir .. example200A
it will be get bycase a
and be assign inWATEREQFORN1
as show in my codeWATEREQFORN1 = val
theval
initially hold the serial value200
and pass toWATERECFORN1