I've searched online and saw this link " . " 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.
2 Answers
All the information is in the language reference:
Read any data type or object from the EEPROM.
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.
2OK... 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, " ). ", 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().