Saving to EEPROM



  • Hi all,
    Could someone please explain how to save a state to EEPROM, I have a hot water heater that I have to "dumb up", my problem is the saving off the last state, IE when there is a power interruption and the sensor reboots, how do I read the previous state and the previous LOW and HIGH temperature settings. For example I use these statements
    #define LowTempState 0
    #define HighTempState 0
    #define HeaterONOFF 0

    So the heater will not be switched on when you install it the 1st time, but then once the sensor is up and running, you would set the state and temperature from the controller and save that, but I want to prevent the heater from being ON and heating and causing damage when you install it for the 1st time. If I can explain more.. If this is a new installation, run the
    #define LowTempState 10
    #define HighTempState 20
    #define HeaterONOFF 0
    But once it is connected and running and the temperature and state is set from the controller, save those values and when there is a power failure and the sensor reboots, use the saved values to come up with.. I hope this makes sense?

    Is there some example code somewhere that I might check please...

    Regards


  • Mod

    @esawyja the relay example saves state. See if you can use that as template.



  • Hi
    I think the relay example get the previous state from the EEPROM, I need to get the state from the controller after it was set for the 1st time from the controller?

    void before()
    {
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
    }

    An example with lots of comments that is being done will help a lot please

    Regards


  • Contest Winner

    @esawyja for getting a value from the controller one can use "request()", i.e. in the relay example like this:

    request(CHILD_ID,V_LIGHT);
    

    The answer should be handled by the receive function



  • Thanks all, I'll try to get this to work



  • Hi
    I'm struggling to get my head around this, when I do this

    #define LowTempState 0
    #define HighTempState 0
    #define HeaterONOFF 0

    Does this define the position in EEPROM, as per the documentation, so what I'm doing here is completely incorrect? What I need to do it to give it a value where to save with the #define statements?

    The documentation states
    void saveState(uint8_t pos, uint8_t value);

    pos - The position to store value in (0-255)
    value - Value to store in position

    So if I do this
    #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
    #define HighTempState 20
    #define HeaterONOFF 30
    I have to save the values to the EEPROM addresses with
    saveState(HighTempState, 40);
    saveState(LowTempState, 30);
    saveState(HeaterONOFF, 0);
    So the above with save the values 40, 30 and 0 to their positions 10, 20 and 30 in EEPROM?

    I have to read the state with
    int temHighTrigger=loadState(HighTempState);
    int temLowTrigger=loadState(LowTempState);
    int HeaterStatus=loadState(HeaterONOFF);
    So temHighTrigger will now contain 40? temLowTrigger will now contain 30 and the HeaterStatus will be 0?

    Retrieving a state (from local EEPROM).
    uint8_t loadState(uint8_t pos);

    Sorry for all the questions, my head is spinning....


  • Hero Member

    @esawyja You are correct with the your explanation. The EEPROM consists of 256 "byte" (8 bit) positions , best defined by the type uint_8_t. Be aware that an "int" (int8_t) type in the Arduino C++ compilers is 16 bits. So if you want to store a full int you need two EEPROM positions.



  • Thanks @AWI
    So for
    #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
    #define HighTempState 20
    #define HeaterONOFF 30

    I need to do something like this if it is possible?
    #define LowTempState 10, 11 //Naming the position LowTempState at position 10 in EEPROM
    #define HighTempState 20, 21
    #define HeaterONOFF 30

    Or do I just declare the trigger as 2 bytes with
    uint_8_t temHighTrigger=loadState(HighTempState); //So this will read from position 10 for 16 bits?
    uint_8_t temLowTrigger=loadState(LowTempState); //This one will start at 20 and read 21 as well?
    int HeaterStatus=loadState(HeaterONOFF); //This one will read only position 30?

    Regards


  • Hero Member

    @esawyja you are getting a little too creative here. I suggest you stick to using uint8_t (byte) types for all of your variables unless you need more range than 0..255. If you need more you need to split these types (int, long, float,... ) into bytes yourself and handle them separately.
    If you need suggestions there are a few easy ways to accomplish this by means of using 'union'. Just let us know here.



  • Ok now I'm more confused, but thanks, I will figure it out


  • Hero Member

    @esawyja sorry... Also for the first sentence? Use byte (uint8_t) for all variables you need to store..


Log in to reply
 

Suggested Topics

13
Online

11.4k
Users

11.1k
Topics

112.7k
Posts