How to mange EEPROM storage

Hello
Sorry for the basic question but I was unable to figure it out myself.
I am trying to figure out how to use EEPROM in case of power outage.
I manged to successfully use EEPROM.get and EEPROM.put (using docs

int temp;
int s1max1;
int s1min1;

void setup() {
  Particle.function("s1",s1 ); 
  Particle.variable("s1max1",s1max1); 
  Particle.variable("s1min1",s1min1); 
  pinMode(D0, OUTPUT);
  pinMode(D0, OUTPUT);
  EEPROM.get(0, s1max1);
  EEPROM.get(5, s1min1);
  Serial.begin(9600);
}

void loop() {  
  //temp=airSensor.getTemperature();
  size_t length = EEPROM.length();
  Serial.print("The size of byte is "); Serial.print((length)); 
  Serial.println(".");
  delay(2000);

  if(temp <= s1max1){
   digitalWrite(D0, HIGH); 
  }

  if(temp <= s1min1){
    digitalWrite(D0, LOW); 
    delay(2000);
  }
}

int s1 (String value) {
  s1min1 = value.substring(0,2).toFloat();
  s1max1= value.substring(2,4).toFloat();
  EEPROM.put(0, s1max1);
  EEPROM.put(5, s1min1);
  
  return 0; 
}

Although it works OK I did not understand how addresses work and how much storage each variable takes.
In this code attached I used address 0 for the first variable and address 5 for the second and printing the size of the eeprom to see if it changes.

My question is : How much of storage each INT with 1 digit (for example 6), with 2 digit (for example 26)/ 3 digit etc…
I tried reading this but I did not understand it.

That depends on the datatypes you use, but sizeof(yourVariable) will give you the byte count your variable occupies in memory.
For int (better to use int32_t for clarity) it'd be four bytes.

An int32_t always occupies 32 bits, no matter which number it holds (providing within its range -2,147,483,648 .. +2,147,483,647).
If you know your respective data will never exceed a certain range you can always opt for shorter datatypes (e.g. int8_t for -128..+127 - which will require one byte in memory).

2 Likes

Thank you ScruffR
I think I understand now.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.