Go Down

Topic: Saving a variable with EEPROM (Read 510 times) previous topic - next topic

woujer

Hello i came across a little problem i'm making a thermostat and the variable i use for the temperature i set has to be saved. So when the power turns of and it gets back up the set variable is the same as when he got turned off. Right now it just goes back to 0°C evrytime. I have been searching for a solution for this problem but most of the solutions i came across are just not working or they are just way to complicated. I hope someone can help me would be mutch help! I'm a still a student so explanation with the solution would be appriciated.

Kind regards Woujer

TolpuddleSartre

Use the EEPROM.
If you haven't changed the current set point for , say, thirty seconds, save the value to EEPROM.
Read EEPROM in setup() to recover the set point.
Be careful that you do not write to EEPROM too frequently or you will wear it out.

sterretje

eeprom.put and eeprom.get can be used so you don't have to worry about the size of the variable.

eeprom.put also has the advantage that it first checks if the value has changed and hence does not put extra strain on the eeprom cells.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

woujer

eeprom.put and eeprom.get can be used so you don't have to worry about the size of the variable.

eeprom.put also has the advantage that it first checks if the value has changed and hence does not put extra strain on the eeprom cells.
Hmm can't figure out how i can fix it i tried with put and get but still not a result it saves only the start variable that is 0°C.

TolpuddleSartre

How to fix what?
You haven't posted any code.

(You could ask a moderator to move this to an appropriate section of the forum)

Budvar10

It is very simple task:
1. read the value from EEPROM into the variable at start (e.g. in setup()),
2. at any change of its value, save the variable into EEPROM.

Post your code if you are not able to solve the problem.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

woujer

It is very simple task:
1. read the value from EEPROM into the variable at start (e.g. in setup()),
2. at any change of its value, save the variable into EEPROM.

Post your code if you are not able to solve the problem.
Thanks solved it! :) But now the only problem is he won't save the value after the 0.
Example when it sets to 21.4°C It saves 21.0°C you know anyway to fix this?

sterretje

#7
Mar 29, 2018, 03:53 pm Last Edit: Mar 29, 2018, 03:54 pm by sterretje
Thanks solved it! :) But now the only problem is he won't save the value after the 0.
Example when it sets to 21.4°C It saves 21.0°C you know anyway to fix this?
Without seeing your code? My crystal ball is on Easter holiday at this moment :)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Budvar10

Hard to say without seeing the code. :o
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

woujer

Please don't mind all the text next to it my school asks for me to put information next to each line i write. Sorry if it looks like shit  :smiley-confuse:



Without seeing your code? My crystal ball is on Easter holiday at this moment :)
Hard to say without seeing the code. :o
    Code: [Select]
    #include <LiquidCrystal_I2C.h>  /////////////////////////////////////////////////////////////////////
    #include <OneWire.h>            ///////////////////////LIBARYS TOEVOEGEN/////////////////////////////
    #include <DallasTemperature.h>  /////////////////////////////////////////////////////////////////////
    #include <EEPROM.h>             /////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #define ONE_WIRE_BUS 7                                                 //We zetten de data pin op pin 7 van de arduino.                                                                                       
    OneWire oneWire(ONE_WIRE_BUS);                                         //We geven de naam onewire aan de One wire bus.
    LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);         //De liquidcrystal configuratie voor de LCD.
    DallasTemperature sensors(&oneWire);                                   //Dallas libary zorgt ervoor dat de sensor inlees baar is via One Wire.
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    const int drukLaagStatus = 9;                                          //Pin waar de drukknop voor de temperatuur dalen op zal werken.
    const int drukHoogStatus = 10;                                         //Pin waar de drukknop voor temperatuur stuhgeb op zal werken.
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int ledKoud =6;                                                        //Led afkoelen word gezet op pin 6.
    int ledWarm =8;                                                        //Led opwarmen word gezet op pin 8.
    int drukLaag = 0;                                                      //Waarde van de drukknop omlaag ingesteld op logische 0.
    int drukHoog = 0;                                                      //Waarde van drukknop omhoog ingesteld op een logishe 0.
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    float tempSet ;                                                        //De Set temperatuur geen waarde omdat afhankelijk van de input.
    float tempC = 0;                                                       // Temperatuur die gemeten zal worden.
    float a = 0.1;                                                         //Variabel van temperatuur per druk op de knop dat het verplaatst.
    float hys = 0.5;                                                       //Variabel hoeveel verschil er moet zijn voor dat hij opwarmt of afkoelt.
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    uint8_t EEPROMaddress = 130;
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void setup()
    {
      sensors.begin();                                                     //De One wire sensor opstarten.
      lcd.begin(16,2);                                                     //LCD opstarten en toewijzen dat het een 16/2 LCD is.
      lcd.backlight();                                                     //Aanzetten de backlight van de LCD.
      Serial.begin(9600);                                                  //Data snelheid word geset in bits per seconde. standaard waarde 9600 gebruiken we.
      pinMode(6, OUTPUT);                                                  //Pin 6 word ingesteld als een OUTPUT.
      pinMode(8, OUTPUT);                                                  //Pin 8 word ingesteld als een OUTPUT.
      pinMode(10, INPUT);                                                  //Pin 10 word ingesteld als een INPUT (OPTIONEEL).
      pinMode(9, INPUT);                                                   //Pin 9 word ingesteld als een INPUT (OPTIONEEL).
      tempSet = EEPROM.read(EEPROMaddress);                                //Lees de opgeslagen data op EEPROMaddress.
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void loop()
    {
      temperatuur_lcd();                                                   //void temperatuur_lcd word opgeroepen in de loop.
      instellen();                                                         //void instellen word opgeroepen in de loop.
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void temperatuur_lcd()

      sensors.requestTemperatures();                                       // We vragen de temperatuur aan via het chekken van de sensor op de data lijn.
      tempC = sensors.getTempCByIndex(0);                                  //variabel tempC krijge als waarde de temperatuur die word gemeten we zetten (0) dit is zodat hij begint te chekken bij de eerste data input.
      Serial.println(tempC);   
      lcd.setCursor(0,0);                                                  //Zet de cursor om te beginnen op de LCD op de bovenste lijn in het begin
      lcd.print("Temp: ");                                                 //print Temp: in het begin van de LCD
      lcd.print(tempC,1);                                                  //print de gemeten waarden op de sensor
      lcd.print(" C");                                                     //Print C voor Celcius
      Serial.println(tempSet);
      lcd.setCursor(0,1);                                                  //Zet de cursor van de LCD op de 2de lijn
      lcd.print("Set : ");                                                 //print Set: op de 2de lijn van de LCD
      lcd.print(tempSet,1);                                                //print de set waarde op de LCD
      lcd.print(" C");                                                     //Print C voor celcius
      EEPROM.write(EEPROMaddress,tempSet);                                 //We slaan de tempSet gegevens op in de eeprom.
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void instellen(){
    ////// TEMPSET INSTELLEN ALS HUIDIGE GEMETEN WAARDE //////
     if (drukHoog == HIGH && drukLaag == HIGH
     {
        (tempSet = tempC);
     }
     else
     {
     //Doe niks
     }
    ////// DRUK KNOP GRADEN OMHOOG/////////
      drukHoog = digitalRead(drukHoogStatus);
      if (drukHoog == HIGH)
      {
        (tempSet = (tempSet)+(a));
      }
      else
      {
      //Doe niks 
      }
    ///// DRUK KNOP GRADEN OMlAAG ///////
      drukLaag = digitalRead(drukLaagStatus);
      if (drukLaag == HIGH )
      {
        (tempSet = (tempSet)-(a)); 
      }
      else
      {
      //Doe niks
      }
    ///// TEMP TE LAAG LED WARM AAN /////
      if (tempSet + hys >= tempC)
      {
        digitalWrite(ledWarm,0);
      }
      else
      {
        digitalWrite(ledWarm,1);
      }
    //// TEMP TE HOOG LED KOUD AAN /////// 
      if (tempSet - hys <= tempC)
      {
        digitalWrite(ledKoud,0);
      }
      else
      {
        digitalWrite(ledKoud,1);
      }
    }


    TolpuddleSartre

    Code: [Select]
    tempSet = EEPROM.read(EEPROMaddress); What datatype does EEPROM.read return?
    What datatype is tempSet?

    sterretje

    Looking like shit is one thing; not compiling is another one. You're missing a )

    If you use <control>T in the IDE, you can immediately see that something is wrong. No { (or }) should be at the beginning of a line with the exception of the { and } that embed the function body.

    OK, to the problem. The point was made by TolpuddleSartre in reply #10; for that exact reason I advised to use eeprom.put and eeprom.get in reply #2.
    If you understand an example, use it.
    If you don't understand an example, don't use it.

    Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

    Go Up