| 1 |
/*** |
| 2 |
eeprom_put example. |
| 3 |
|
| 4 |
This shows how to use the EEPROM.put() method. |
| 5 |
Also, this sketch will pre-set the EEPROM data for the |
| 6 |
example sketch eeprom_get. |
| 7 |
|
| 8 |
Note, unlike the single byte version EEPROM.write(), |
| 9 |
the put method will use update semantics. As in a byte |
| 10 |
will only be written to the EEPROM if the data is actually |
| 11 |
different. |
| 12 |
|
| 13 |
Written by Christopher Andrews 2015 |
| 14 |
Released under MIT licence. |
| 15 |
***/ |
| 16 |
|
| 17 |
#include <EEPROM.h> |
| 18 |
|
| 19 |
struct MyObject { |
| 20 |
float field1; |
| 21 |
byte field2; |
| 22 |
char name[10]; |
| 23 |
}; |
| 24 |
|
| 25 |
void setup() { |
| 26 |
|
| 27 |
Serial.begin(9600); |
| 28 |
while (!Serial) { |
| 29 |
; // wait for serial port to connect. Needed for native USB port only |
| 30 |
} |
| 31 |
|
| 32 |
float f = 123.456f; //Variable to store in EEPROM. |
| 33 |
int eeAddress = 0; //Location we want the data to be put. |
| 34 |
|
| 35 |
|
| 36 |
//One simple call, with the address first and the object second. |
| 37 |
EEPROM.put(eeAddress, f); |
| 38 |
|
| 39 |
Serial.println("Written float data type!"); |
| 40 |
|
| 41 |
/** Put is designed for use with custom structures also. **/ |
| 42 |
|
| 43 |
//Data to store. |
| 44 |
MyObject customVar = { |
| 45 |
3.14f, |
| 46 |
65, |
| 47 |
"Working!" |
| 48 |
}; |
| 49 |
|
| 50 |
eeAddress += sizeof(float); //Move address to the next byte after float 'f'. |
| 51 |
|
| 52 |
EEPROM.put(eeAddress, customVar); |
| 53 |
Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); |
| 54 |
} |
| 55 |
|
| 56 |
void loop() { |
| 57 |
/* Empty loop */ |
| 58 |
} |