1
+ /* **
2
+ eeprom_pointer example.
3
+
4
+ This example shows how the built-in EEPtr
5
+ object can be used to manipulate the EEPROM
6
+ using standard pointer arithmetic.
7
+
8
+ Running this sketch is not necessary, this is
9
+ simply highlighting certain programming methods.
10
+
11
+ Written by Christopher Andrews 2015
12
+ Released under MIT licence.
13
+ ***/
14
+
15
+ #include < EEPROM.h>
16
+
17
+ void setup () {
18
+
19
+ Serial.begin (9600 );
20
+
21
+ /* **
22
+ In this example, we will iterate forward over the EEPROM,
23
+ starting at the 10th cell (remember indices are zero based).
24
+ ***/
25
+
26
+ EEPtr ptr = 9 ;
27
+
28
+ // Rather than hard coding a length, we can use the provided .length() function.
29
+
30
+ while ( ptr < EEPROM.length () ){
31
+
32
+ Serial.print ( *ptr, HEX ); // Print out hex value of the EEPROM cell pointed to by 'ptr'
33
+ Serial.print ( " , " ); // Separate values with a comma.
34
+ ptr++; // Move to next cell
35
+ }
36
+
37
+ /* **
38
+ In this example, we will iterate backwards over the EEPROM,
39
+ starting at the last cell.
40
+ ***/
41
+
42
+ ptr = EEPROM.length () - 1 ;
43
+
44
+ do {
45
+
46
+ Serial.print ( *ptr, HEX );
47
+ Serial.print ( " , " );
48
+
49
+ }while ( ptr-- ); // When the pointer reaches zero the loop will end as zero is considered 'false'.
50
+
51
+
52
+ /* **
53
+ And just for clarity, the loop below is an equivalent implementation
54
+ of the C++11 ranged for loop.
55
+ ***/
56
+
57
+ for ( EEPtr ptr = EEPROM.begin () ; item != EEPROM.end () ; ++item ){
58
+ Serial.print ( *ptr, HEX );
59
+ Serial.print ( " , " );
60
+ }
61
+
62
+ /* **
63
+ The actual C++11 version:
64
+
65
+ for( auto ptr : EEPROM ){
66
+ Serial.print( *ptr, HEX );
67
+ Serial.print( ", " );
68
+ }
69
+ ***/
70
+
71
+
72
+ }
73
+
74
+ void loop (){}
0 commit comments