4

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.

SDsolar
  • 1,175
  • 3
  • 11
  • 35
kaisar Great
  • 111
  • 2
  • 5
  • 11

4 Answers4

1

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.

JRobert
  • 15,407
  • 3
  • 24
  • 51
1

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;
  ...
James Waldby - jwpat7
  • 8,920
  • 3
  • 21
  • 33
0

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).

Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58
0

Is it possible to change the value of respective address without affecting others ..

Yes, if the two values are 8-bit types.

Otherwise, No.

dannyf
  • 2,813
  • 11
  • 13