Could you maybe check my EEProm routines?

Tis is my first try with the EEProm on Arduino.

My goal::

  • save my Calibration data in address 0
  • save my sensor readings from 1 to end of the EEprom

The same way I want to read them later.

I did an Enable to only write to EEProm when it's on in my complete code.

Also I did 2 lines to comment out (whichever is needed)
One should start over the recording from the beginning if the End of the EEProm has been reached
The other should stop recording when the end has been reached.

I just want to make sure my routines don't miss some dataspace or write/read over the EEProm length.

Also is it ok to write an int to the byte-space in the EEProm or will it overwrite data in the next cell then and I need to use byte as variable after I mapped it?

Thx very much!

#include <EEPROM.h>


// ---------------- EEProm Aufzeichnung Sensordaten von Sensor1 aktivieren (HIGH = AN) -----------------------

                                      bool EE_Enable = LOW;

// -------- EEProm Widergabe auf SerialPort aktivieren (HIGH = AN) und Geschwindigkeit einstellen ------------

                                      bool EE_Read = LOW;

                                      const int EE_ReadDelay = 100;

//---------------------------  ACHTUNG NUR EINS ZUR ZEIT ODER BEIDE AUS!  ------------------------------------


int EEWriteAdress;                              // EEProm Adresscounter (Startwert wird in setup() gesetzt)
const int EECalib = 0;                          // EEProm Adresse für Kalibrationsdaten
const int EEData = 1;                           // EEProm Startadresse für Messdaten (Falls Adresse geändert wird muss auch EEWriteAdress angepasst werden)


void setup() {
  EEWriteAdress = EEData;                       // Startadresse EEprom setzen
}


void loop() {
// Here they are used somewhere
}


// -------------- EEProm Funktionen -----------------------------------------

void EE_WriteCalib(int CalibWert) {
  EEPROM.update(EECalib, map(CalibWert, 0, 1023, 0, 255));            // Mappe Kalibration von 1024 auf 256 und schreibe in Speicher für Kalibration
}

void EE_WriteValue(int MessWert) {                                    // Schreibt gemessenen Wert ins EEProm
  EEPROM.update(EEWriteAdress, map(MessWert, 0, 1023, 0, 255));       // Mappe Messwert von 1024 auf 256 und schreibe in Speicher
  EEWriteAdress++;                                                    // Adresse hochzählen
  if (EEWriteAdress == EEPROM.length()) {
    // EEWriteAdress = EEData;                                           // Adresse zurücksetzen und von vorne beginnen mit Aufzeichnen oder
    EE_Enable = LOW;                                                  // Aufzeichnung stoppen
  }
}

void EE_SerialPrint() {
  int CalibRead = EEPROM.read(EECalib);                               // Lese Kalibration in Variable
  for (int i = EEData; i < EEPROM.length(); i++) {                    // Gebe Daten aus von Startwert bis Ende EEProm
    Serial.print(CalibRead);                                          // Kalibrationswert ausgeben
    Serial.print(",");                                                // Trennung damit 2. Graph mit Messdaten erstellt wird
    Serial.print(EEPROM.read(i));                                     // Anzeige gelesener Messwert
    delay(EE_ReadDelay);                                              // Verzögerung vor Ausgabe nächster Wert
  }
}

Also is it ok to write an int to the byte-space in the EEProm

If you want to write an int, then you split it up into two bytes and store them at address 0 and 1.

need to use byte as variable after I mapped it?

Yes pull two bytes out of the eeprom and then join them together as an int.

Also is it ok to write an int to the byte-space in the EEProm or will it overwrite data in the next cell then and I need to use byte as variable after I mapped it?

You can ask it to write the int to the single address, but it will only write the low byte there. So it won't overwrite anything, but it also won't write the correct value. It will only write the low 8 bits. So anything over 255 gets truncated and won't be the number you expect.

If you use the EEPROM.put() method, then it will allow you to write multi-byte variables to EEPROM, but you have to keep up with how many bytes you wrote so you know what the next address will be.

Delta_G:
You can ask it to write the int to the single address, but it will only write the low byte there. So it won't overwrite anything, but it also won't write the correct value. It will only write the low 8 bits. So anything over 255 gets truncated and won't be the number you expect.

If you use the EEPROM.put() method, then it will allow you to write multi-byte variables to EEPROM, but you have to keep up with how many bytes you wrote so you know what the next address will be.

Yes that is only what I need I just need up to 255 I just wanted tp be sure the rest is not written into the next byte. Thanks very much.

Can maybe someone look over my write and read routine if I did the adressing correctly?

Yes that is only what I need I just need up to 255

Then why are they type int and not byte? Why waste a byte of memory on each one?

because i read int from the adc before and need to map it so if it only writes 0-255 in the eeprom I don't need to convert it before right?
That's what I want to know.

Or will he write 2 byte to eeprom? one containing the lower byte and one empty higher one?

because i read int from the adc before and need to map it so if it only writes 0-255 in the eeprom I don't need to convert it before right?

Do you want/need to save the 0 to 1023 value or are you happy to divide it by 4 and save the 0 to 255 value ? You can save either range of values to EEPROM using either 2 bytes (0 to 1023) or 1 byte (0 to 255)

Which would you prefer to do ?

I want 0-255 this is enough look up my sketch I mapped it to 0-255

That makes it easy. All you need to do is to EEPROM.write() the single byte of data to an EEPROM address, increment the EEPROM address variable and make sure that it does not exceed EEPROM.length()

Gorkde:
Or will he write 2 byte to eeprom? one containing the lower byte and one empty higher one?

EEPROM.put() would do that. But write() will just lop off the top 8 bits and write only the lower 8 bits.