Getting to use EEPROM

I have am wondering if I can use EEPROM on my nano like you would use a database with a structure similar to this:

// EXAMPLE     0      H     120    130     140       180     360
// EXAMPLE     1      T     15     200     350       600     180
enum myData { INDEX, CODE_0, CODE_1, CODE_2, CODE_3,  CODE_4, CODE_5 };

this is as far as I have gotten and am now wondering if it is possible.
This sketch is just my starting point,
Any suggestions would be welcome.

`t#include <EEPROM.h>

// EXAMPLE     0      H     120    130     140       180     360
// EXAMPLE     1      T     15     200     350       600     180
enum myData { INDEX, CODE, CODE_1, CODE_2, CODE_3,  CODE_4, CODE_5 };


void setup() {
  pinMode(13, OUTPUT);  digitalWrite(13, LOW);

}

void loop() {

  Serial.begin(115200);
  Serial.println(" startng eeprom demo ");

  digitalWrite(13, HIGH);
  // do this
  delay(1000);
  digitalWrite(13, LOW);

}

void myClear() {

  for (int i = 0 ; i < EEPROM.length() ; i++) {
    Serial.print("Clearing "); Serial.println(i);
    EEPROM.write(i, 0);
  }
}

void myRead(int address) {
  // read a byte from the current address of the EEPROM
  byte value = EEPROM.read(address);

  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();
}
void myUpdate( int address, byte val) {
  Serial.print("updating  address"); Serial.println(val);
  EEPROM.update(address, val);
}
ype or paste code here`

Please, edit your post and paste the code where it says "type or paste cod here"

EEPROM reading is indefinite. Try to limit the EEPROM writing as it has a limit over 100,000 writes before failure (unreliable).

Yes you could (within reason as you have a limited number of writes) use the eeprom as non volatile storage. Put and get functions would let you store or access a structure

Please fix code tags

EEPROM for the Arduino Nano ESP32 is depreciated. There is a newer library called Preferences.

Is it a Nano32 or a good old plain nano?

Although the EEPROM does have a limited number of write cycles, the 'suggested' maximum of 100,000 is a very conservative value.

I once did a test on an Arduino Uno R3, where I alternately wrote b01010101 and b10101010 to an EEPROM location. I then read the location to check whether the data had been written/read correctly.

I got up to over 5.3 million write cycles before an error occured. I realise that this was only a sample of 1 device, and there will be considerable variations in life expectancy.

Good analogy.

done

1 Like

this is a plain old nano - I understand that I have a limited number of writes and memory.

I have 0-7 buttons and a stepper motor - each button will represent the number of steps from point zero, when each button is positioned i would like to use a 9th button to save the step readings for each button. Once these are set they would most likely be readonly.
ie
button 4 has positioned the stepper to 140 steps from zero - we press button 9 and the index 4 writes the number of steps.

on setup these values are read for all buttons placed in an array.

this would change the structure of my data to
index
code (H or T)
steps

What does positioned mean?

How do you associate the numbers of steps to each button ?

As you have been advised, you can dream up any use for EEPROM data storage, subject to constraints of size, speed of access and lifetime reliability.

Here I would suggest a small change

byte myRead(int address) {
  // read a byte from the current address of the EEPROM
  byte value = EEPROM.read(address);

  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();

  return value;
}

As you wrote it, there was no ability to use what got read. A temptation to make value global in scope should be resisted.

Also, myClear() is a nice idea; it might make more sense to avoid having a need to ever clear the entire EEPROM, just to reduce wear and tear.

HTH

a7

Typo - should read pressed.

the stepper motor is moved forward one step by buttons 3 and backward by button 4 when the stepper is in the required position button 9 latching button would be pressed and button 4 momentary this would write to index 4 -eeprom the stepper position (index 4 being the target position of button 4).
let me know if this is clear?

noted on both accounts - thanks

On a lot of devices, the default "erased" condition of EEPROM is all 1's (0xFF in each byte), not all 0's, so clearing the EEPROM to all 0's is wasting a write cycle. It would also be better to use update() instead of write(), otherwise a byte that already contains 0x00 will be erased to 0xFF then re-written to 0x00.

OK how would this do:

void writeDefaults() {
    Serial.print("defaults  "); 
    EEPROM.update(1, 15);
    EEPROM.update(2, 30);
    EEPROM.update(3, 45);
    EEPROM.update(4, 60);
    EEPROM.update(0, "Done);
    Serial.println(" Done ");
  }
}


this would only be performed for testing.
would this EEPROM.update(0, "Done):wink: need to be different from the other index's when performing the read?

Assuming you meant

     EEPROM.update(0, "Done");

I don't think it would compile; if it does compile it is not likely to do what you think or want.

EEPROM at this level is fairly primitive. update() takes two arguments, a memory address and a byte of data, 8 bits. Checks to see and if need be actually writes the byte to memory.

Anything more sophisticated is kinda up to you to design and organize. There are a few more powerful functions, see

https://docs.arduino.cc/learn/built-in-libraries/eeprom/

a7

correct, bad keyboard...
I will play with this and see where I go, Thank you

Tried this - but not getting the correct information on the readDefaults();

startng eeprom demo
defaults Done
text = Done
1 28271
2 25966
3 101
4 0

#include <EEPROM.h>

void setup() {

  Serial.begin(115200);
  Serial.println(" startng eeprom demo ");

}

void loop() {

 

  static int status = 0;

  if (status == 0) {
    writeDefaults();
    status++;
  }
  if (status == 1) {
    readDefaults();
    status++;
  }
  delay(1000);
 

}

void writeDefaults() {
  Serial.print("defaults  ");
  EEPROM.put(1, 15);
  EEPROM.put(2, 30);
  EEPROM.put(3, 45);
  EEPROM.put(4, 60);
  EEPROM.put(0, "Done");
  Serial.println(" Done ");
}

void readDefaults() {

  char customVar[10];
  EEPROM.get( 0, customVar );
  Serial.print(" text = "); Serial.println(customVar);
  int value;
  for (int x = 1; x < 5; x++) {
    EEPROM.get(x, value);
    Serial.print(x);
    Serial.print("\t");
    Serial.print(value, DEC);
    Serial.println();
  }


}


can you advise me where I am wrong..

The first four lines are storing integers, which are two bytes each, but you are only allowing space for one byte. The last line is storing "Done", which is a char array that takes five bytes of storage, overwriting all the integers.

the knowledge to correct this is beyond me - can you explain/suggest change needed.