0

I've searched online and saw this link " https://www.best-microcontroller-projects.com/arduino-eeprom.html#:~:text=Arduino%20EEPROM%20get%20vs%20read,bytes%20starting%20from%20an%20address. " but I'm still not understanding how EEPROM.read(address) and EEPROM.get(address) is any different.

I created this code, to see if the EEPROM.get() will read bytes starting from the first address to the last address.

#include <EEPROM.h>

int address = 0;
int eeAddress = 0;
byte value;
float f = 0.00f;

void setup() {
    Serial.begin(9600);
  while (!Serial) {
         ;                    // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  //--------------------EEPROM.get()-----
    Serial.println("---Using EEPROM.get()----");
  EEPROM.get(eeAddress, f);
      Serial.print(".get() address: ");
  Serial.println(eeAddress);
  Serial.print(".get() value: ");
  Serial.println(f, 3);

  //-------------------EEPROM.read()------
    Serial.println("---Using EEPROM.read()----");

  value = EEPROM.read(address);
    Serial.print(".read() address: ");
  Serial.println(address);
  Serial.print(".read() value: ");
  Serial.println(value, DEC);
  
}

Im only getting "-0.000" as a result for EEPROM.get() and "3" for EEPROM.read(). Im not understanding the difference between them.

Xvx
  • 67
  • 1
  • 11

2 Answers2

0

All the information is in the language reference:

get()

Read any data type or object from the EEPROM.

read()

Reads a byte from the EEPROM.

And if you need more information:

read() operates on a single byte. It reads a single byte from an address.

get() reads multiple bytes starting from an address. The number of bytes read is the size of the type.

As explained here.

Nino
  • 702
  • 2
  • 6
  • 12
  • Ok, I set all the bytes to 0, I wrote a new code in which the 260 is written into an int address **int addr = 0;** and is stored in an int variable **int ra;**, when I printed out the value using **EEPROM.get(addr, ra)** I got 4. When I used **va = EEPROM.read(addr)** I also got 4. Code: `void loop() { int addr = 0; int ra; EEPROM.write(addr, 260); EEPROM.get(addr, ra); Serial.print("address: "); Serial.print(addr); Serial.print(", value: "); Serial.println(ra); }` I understand that 1 address has 1 byte/8 bits, but with the EEPROM.get() wasn't I suppose to get 260? – Xvx Apr 21 '21 at 20:56
  • I'm not understanding what you're saying, because the results I got is confusing me. – Xvx Apr 21 '21 at 21:11
0

OK... after figuring this out myself... Referring to my comment on Nino's response, the problem was that I had used EEPROM.write() beforehand. when I used either EEPROM.get() or EEPROM.read() it didn't matter because I only wrote a single byte to the EEPROM. Going back to what Nino was saying or the link used for the question, " https://www.best-microcontroller-projects.com/arduino-eeprom.html#:~:text=EEPROM%20Write%20Standard%20type%20or%20Structure&text=You%20can%20use%20this%20function,that%20use%20different%20type%20sizes). ", EEPROM.get() reads multiple bytes starting from an address(more than 1 byte), so if I wanted to store 259 (2 bytes) into the EEPROM I will have to use EEPROM.put() followed by EEPROM.get() and not EEPROM.write() followed by either EEPROM.get() or EEPROM.read().

NOW.... explaining my answer for the topic. The reason why I got " -0.000 ", is because it was a result of what I had before. Using EEPROM.put() will use update semantics. Meaning if you give the address an integer of 56 and the variable you're storing 56 into, by using int x; EEPROM.get(addr, x); or int x = EEPROM.read(addr); is an integer, you'll will get the integer value. However if the variable is another type, such as float x ; EEPROM.get(addr, x); you will not get the integer 56, but the value of what was stored in that address with that declaration (float) is what you'll get. Therefore the reason why I got " -0.000 " and " 3 " is because of a previous value for float and different previous value for the integer. For " 3 " it could've been 259 stored with EEPROM.write() or simply 3 stored with either EEPROM.write() or EEPROM.put().

Xvx
  • 67
  • 1
  • 11