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.

4
  • 1
    Yes. Post your code so we can see where you went wrong.
    – Majenko
    May 8, 2017 at 12:49
  • @Majenko I edit my question and add my code , May 8, 2017 at 13:00
  • What are WATEREQFORN1 and WATEREQFORN2? You only show a bit of your code, and have omitted what may be the most important parts - your variable definitions.
    – Majenko
    May 8, 2017 at 13:02
  • unsigned int WATEREQFORN1; unsigned int WATEREQFORN2; That is the value the user enter sir .. example 200A it will be get by case a and be assign in WATEREQFORN1as show in my code WATEREQFORN1 = val the val initially hold the serial value 200 and pass to WATERECFORN1 May 8, 2017 at 13:07

4 Answers 4

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;
  ...
2
  • Thanks I think thats the problem , I follow your advice and It Works like a charm .. :D May 8, 2017 at 16:37
  • the allocation is the problem .. May 8, 2017 at 16:38
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.

2
  • So instead of unsigned int i will use byte ? because i only want to save numbers. May 8, 2017 at 13:27
  • It works now that i change the unsigned int WATEREQFORN1; into byte WATEREQFORN1 i dont know why but it works , when i write new data to WATEREQFORN2 the WATEREQFORN1 dont change into random numbers May 8, 2017 at 13:57
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).

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.