How to write/read my Setpoint to Eeprom

Hello,

With a Up&Down Button i be able to change my Setpoint. Now I want that my Uno remember the last Setpoint.

I want to store my Setpoint to my Eeprom. When I restart my Uno I want to see my last Setpoint

Is there someone who can help me?

const int downButtonPin = 7;                 
const int upButtonPin = 6;                   
int downButtonState = LOW;                   
int downReading;                             
int upButtonState = LOW;                     
int upReading;                               
int Setpoint;

void setup()
{
  Serial.begin( 9600); 
  pinMode(downButtonPin, INPUT);// assign pins to the buttons
  pinMode(upButtonPin, INPUT);// assign pins to the buttons
  Serial.print("Setpoint:" );

}

void loop()
{
          // Check states of pushbuttons, if pressed change setpoint up or down
          upButtonState = digitalRead(upButtonPin);
          if (upButtonState == 0)
            Setpoint++;
          downButtonState = digitalRead(downButtonPin);
          if (downButtonState == 0)
            Setpoint--;
          Serial.println(Setpoint);
          delay(100);
}

Check the documentation for EEPROM.get() and EEPROM.put().

yes!! this will work, thank you

For now I be able to "put' the setpoint to the Eeprom like this:

#include <EEPROM.h>
const int downButtonPin = 7;
const int upButtonPin = 6;
int downButtonState = LOW;
int downReading;
int upButtonState = LOW;
int upReading;
int Setpoint;

struct MyObject {
  float field1; // Setpoint Eeprom
};


void setup()
{
  Serial.begin( 9600);
  pinMode(downButtonPin, INPUT);// assign pins to the buttons
  pinMode(upButtonPin, INPUT);// assign pins to the buttons
  Serial.print("Setpoint:" );

}

void loop()
{
  float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0;   //Location we want the data to be put.

  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, Setpoint);

  //Data to store.
  MyObject customVar = {
    Setpoint,
  };

  eeAddress += sizeof(float); //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, customVar);


  // Check states of pushbuttons, if pressed change setpoint up or down
  upButtonState = digitalRead(upButtonPin);
  if (upButtonState == 0)
    Setpoint++;
  downButtonState = digitalRead(downButtonPin);
  if (downButtonState == 0)
    Setpoint--;
  Serial.println(Setpoint);
  delay(100);
}

This works well and I can "get" the setpoint out of the Eeprom with this:

#include <EEPROM.h>

void setup() {

  float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0; //EEPROM address to start reading from

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Read float from EEPROM: ");

  //Get the float data from the EEPROM at position 'eeAddress'
  EEPROM.get(eeAddress, f);
  Serial.println(f, 3);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

  /***
    As get also returns a reference to 'f', you can use it inline.
    E.g: Serial.print( EEPROM.get( eeAddress, f ) );
  ***/

  /***
    Get can be used with custom structures too.
    I have separated this into an extra function.
  ***/

  secondTest(); //Run the next test.
}

struct MyObject {
  float field1;

};

void secondTest() {
  int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.

  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get(eeAddress, customVar);

  Serial.println("Read custom object from EEPROM: ");
  Serial.println(customVar.field1);
}

void loop() {
  /* Empty loop */
}

But now I want to get the setpoint in my sketch, how can I do that?

You do not quite seem to understand how it works.

  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, Setpoint);
  ...
  ...
  eeAddress += sizeof(float); //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, customVar);

The sizeof(float) returns 4 bytes; your setPoint however is 2 bytes. so to write something after the setpoint, you need to increment eeAdress by sizeof(int). However, if you only have one variable, you don't need that part of the example.

Although not relevant for your code, you have to be careful; you can only write reliably 100,000 times to an eeprom location. EEPROM.put() takes care of that for you; EEPROM.write does not. Below code demonstrates how to only write the setpoint to eeprom when it changes.

Add the following to variables to the top of your code

#include <EEPROM.h>
...
...
int Setpoint;
// new variables here
int lastSetpoint;         // variable to remember the last setpoint that you entered
int setpointAddress = 0;  // address in eeprom where you want to store the setpoint

Your loop will look like below

void loop()
{
  // Check states of pushbuttons, if pressed change setpoint up or down
  upButtonState = digitalRead(upButtonPin);
  if (upButtonState == 0)
  {
    Setpoint++;
  }
  downButtonState = digitalRead(downButtonPin);
  if (downButtonState == 0)
  {
    Setpoint--;
  }
  Serial.println(Setpoint);

  // if there is a change in setpoint from the last time
  if (Setpoint != lastSetpoint)
  {
    // save to eeprom
    EEPROM.put(setpointAddress, Setpoint);
    // remember
    lastSetpoint = Setpoint;
    // info
    Serial.println("New setpoint saved to eeprom");
  }
  delay(100);
}

And in setup(), you can read the saved value from eeprom when the application starts

void setup()
{
  Serial.begin( 9600);
  pinMode(downButtonPin, INPUT);// assign pins to the buttons
  pinMode(upButtonPin, INPUT);// assign pins to the buttons

  // get the saved setpoint
  EEPROM.get(setpointAddress, Setpoint);
  // for detection of a change, set lastSetpoint to Setpoint
  lastSetpoint = Setpoint;

  Serial.print("Initial setpoint from eeprom:" );
  Serial.println(Setpoint);
}

PS
Not tested