1
+ /* **
2
+ eeprom_arrays example.
3
+ This example sketch shows how to use the EEPROM library
4
+ to read and write arrays of one or more dimensions.
5
+
6
+ For more fine control over what is written, see the example:
7
+ eeprom_memory_blocks
8
+
9
+ Written by Christopher Andrews 2015
10
+ Released under MIT licence.
11
+ ***/
12
+
13
+ #include < EEPROM.h>
14
+
15
+ void setup () {
16
+
17
+ Serial.begin ( 9600 );
18
+
19
+ while (!Serial) {
20
+ ; // wait for serial port to connect. Needed for Leonardo boards only.
21
+ }
22
+
23
+ /* **
24
+
25
+ Read and write a simple 1D array.
26
+
27
+ ***/
28
+
29
+ int address = 0 ; // Address to first EEPROM cell.
30
+
31
+ char text[] = " Testing of a c-string (char array) using put() and get()." ;
32
+
33
+ EEPROM.put ( address, text ); // Write array data.
34
+
35
+ memset ( text, 0 , sizeof (text) ); // Clear testing array.
36
+
37
+ EEPROM.get ( address, text ); // Read back array data.
38
+
39
+ Serial.print ( " char array retrieved from EEPROM: " );
40
+ Serial.println ( text );
41
+
42
+ /* **
43
+
44
+ Read and write a multi-dimensional array.
45
+
46
+ ***/
47
+
48
+ long numbers[3 ][4 ] = {
49
+ { 0xA00 , 0xA01 , 0xA02 , 0xA03 },
50
+ { 0xB00 , 0xB01 , 0xB02 , 0xB03 },
51
+ { 0xC00 , 0xC01 , 0xC02 , 0xC03 }
52
+ };
53
+
54
+ address += sizeof ( text ); // Move to end of array 'text'.
55
+
56
+ EEPROM.put ( address, numbers ); // Write array data.
57
+
58
+ memset ( numbers, 0 , sizeof (numbers) ); // Clear testing array.
59
+
60
+ EEPROM.get ( address, numbers ); // Read back array data.
61
+
62
+ Serial.println ( " \n\n Array of long values retrieved from EEPROM: " );
63
+ Serial.println ( ' {' );
64
+ for ( int i = 0 ; i < 3 ; ++i ){
65
+
66
+ Serial.print ( " {" );
67
+
68
+ for ( int j = 0 ; j < 4 ; ++j ){
69
+ Serial.print ( numbers[ i ][ j ], HEX );
70
+ Serial.print ( ' ,' );
71
+ }
72
+ Serial.println ( ' }' );
73
+ }
74
+ Serial.println ( ' }' );
75
+
76
+ /* **
77
+
78
+ Read a single dimension out of a multi-dimensional array.
79
+
80
+ ***/
81
+
82
+ long set[4 ] = {}; // Enough space for a single dimension.
83
+
84
+ int offset = address + sizeof ( numbers[0 ] ); // Offset the address by the size of one dimension in 'numbers'.
85
+
86
+ EEPROM.get ( offset, set ); // Read second dimension ( index: 1 )
87
+
88
+ Serial.print ( " \n\n Single dimension retrieved from EEPROM:\n {" );
89
+ for ( int i = 0 ; i < 4 ; ++i ){
90
+ Serial.print ( set[ i ], HEX );
91
+ Serial.print ( ' ,' );
92
+ }
93
+ Serial.print ( ' }' );
94
+ }
95
+
96
+ void loop (){
97
+ // Empty loop
98
+ }
0 commit comments