Skip to content

Commit c6d7839

Browse files
committed
Fixed EEPROM examples to remove any doubt for beginners.
Fixes arduino/Arduino#5488 Fixes arduino#3841
1 parent 2f67caf commit c6d7839

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

hardware/arduino/avr/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#include <EEPROM.h>
1212

1313
void setup() {
14+
1415
// initialize the LED pin as an output.
1516
pinMode(13, OUTPUT);
16-
17+
1718
/***
1819
Iterate through each byte of the EEPROM storage.
1920
@@ -27,7 +28,14 @@ void setup() {
2728
***/
2829

2930
for (int i = 0 ; i < EEPROM.length() ; i++) {
30-
EEPROM.write(i, 0);
31+
32+
/*
33+
Use the update method to prevent un-necessary wear on already cleared cells.
34+
The update method checks to see if the current cell is different before writing.
35+
You could easily replace this with EEPROM.write(i, 0); or even EEPROM.put(i, 0);
36+
*/
37+
38+
EEPROM.update(i, 0);
3139
}
3240

3341
// turn the LED on when we're done

hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
#include <EEPROM.h>
1919

20+
//EEPROM address to start reading from. This does not need to be global, but in this example, it is shared between two different functions.
21+
int eeAddress = 0;
22+
2023
void setup() {
2124

2225
float f = 0.00f; //Variable to store data read from EEPROM.
23-
int eeAddress = 0; //EEPROM address to start reading from
2426

2527
Serial.begin(9600);
2628
while (!Serial) {
@@ -52,7 +54,7 @@ struct MyObject {
5254
};
5355

5456
void secondTest() {
55-
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
57+
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
5658

5759
MyObject customVar; //Variable to store custom object read from EEPROM.
5860
EEPROM.get(eeAddress, customVar);

0 commit comments

Comments
 (0)