| 1 |
/*** |
| 2 |
eeprom_get example. |
| 3 |
|
| 4 |
This shows how to use the EEPROM.get() method. |
| 5 |
|
| 6 |
To pre-set the EEPROM data, run the example sketch eeprom_put. |
| 7 |
This sketch will run without it, however, the values shown |
| 8 |
will be shown from what ever is already on the EEPROM. |
| 9 |
|
| 10 |
This may cause the serial object to print out a large string |
| 11 |
of garbage if there is no null character inside one of the strings |
| 12 |
loaded. |
| 13 |
|
| 14 |
Written by Christopher Andrews 2015 |
| 15 |
Released under MIT licence. |
| 16 |
***/ |
| 17 |
|
| 18 |
#include <EEPROM.h> |
| 19 |
|
| 20 |
void setup() { |
| 21 |
|
| 22 |
float f = 0.00f; //Variable to store data read from EEPROM. |
| 23 |
int eeAddress = 0; //EEPROM address to start reading from |
| 24 |
|
| 25 |
Serial.begin(9600); |
| 26 |
while (!Serial) { |
| 27 |
; // wait for serial port to connect. Needed for native USB port only |
| 28 |
} |
| 29 |
Serial.print("Read float from EEPROM: "); |
| 30 |
|
| 31 |
//Get the float data from the EEPROM at position 'eeAddress' |
| 32 |
EEPROM.get(eeAddress, f); |
| 33 |
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. |
| 34 |
|
| 35 |
/*** |
| 36 |
As get also returns a reference to 'f', you can use it inline. |
| 37 |
E.g: Serial.print( EEPROM.get( eeAddress, f ) ); |
| 38 |
***/ |
| 39 |
|
| 40 |
/*** |
| 41 |
Get can be used with custom structures too. |
| 42 |
I have separated this into an extra function. |
| 43 |
***/ |
| 44 |
|
| 45 |
secondTest(); //Run the next test. |
| 46 |
} |
| 47 |
|
| 48 |
struct MyObject { |
| 49 |
float field1; |
| 50 |
byte field2; |
| 51 |
char name[10]; |
| 52 |
}; |
| 53 |
|
| 54 |
void secondTest() { |
| 55 |
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. |
| 56 |
|
| 57 |
MyObject customVar; //Variable to store custom object read from EEPROM. |
| 58 |
EEPROM.get(eeAddress, customVar); |
| 59 |
|
| 60 |
Serial.println("Read custom object from EEPROM: "); |
| 61 |
Serial.println(customVar.field1); |
| 62 |
Serial.println(customVar.field2); |
| 63 |
Serial.println(customVar.name); |
| 64 |
} |
| 65 |
|
| 66 |
void loop() { |
| 67 |
/* Empty loop */ |
| 68 |
} |