-1

I'm using EEPROM on Arduino to store some large constant array. I noticed that both EEPROM.read(address) and EEPROM[address] works for my reading. But there are few documentations on the EEPROM[address] method. I also experienced occasional memory crash with that method.

EEPROM.read(address) has not been fully tested for long run. It does take more storage space when compiling. Is it safer for its behavior behind the scene?

2 Answers2

0

EEPROM[adress] will give you a reference to the eeprom cell while EEPROM.read(adress) will give you an unsigned char value from that cell.

In both cases you should ensure that your adress is valid.

make sure adress is >= 0 and < EEPROM.length().

Piglet
  • 27,501
  • 3
  • 20
  • 43
0

EEPROM.read(adress) ->Read the EEPROM (address starting form 0)and send its value as unsigned char.

EEPROM[adress] -> reference eeprom cell with address

To reduce the size of the you can use avr/eeprom library , which has various function and macros for the eeprom usage. This is a reliable library and well tested. avr/eeprom.h

Sample Code

#include <EEPROM.h>
#include <avr/eeprom.h>

void Eepromclr();

void setup() {

  Serial.begin(9600);

  eeprom_write_byte((void*)0,12);
  int x = eeprom_read_byte((void*)0);\
  Serial.println(x);

  Eepromclr();

  eeprom_update_byte((void*)0,6);
  int y = eeprom_read_byte((void*)0);
  Serial.println(y);

}
void loop() {

}

void Eepromclr() {
  for (int i = 0 ; i < EEPROM.length() ; i++) {
  EEPROM.write(i, 0);
}
Serial.println("Eeprom is cleared");
}
Nithin Varghese
  • 893
  • 1
  • 6
  • 28