File tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed
libraries/EEPROM/examples Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * EEPROM Clear
3
+ *
4
+ * Sets all of the bytes of the EEPROM to 0.
5
+ * This example code is in the public domain.
6
+
7
+ */
8
+
9
+ #include < EEPROM.h>
10
+
11
+ void setup ()
12
+ {
13
+ EEPROM.begin (512 );
14
+ // write a 0 to all 512 bytes of the EEPROM
15
+ for (int i = 0 ; i < 512 ; i++)
16
+ EEPROM.write (i, 0 );
17
+
18
+ // turn the LED on when we're done
19
+ digitalWrite (13 , HIGH);
20
+ EEPROM.end ();
21
+ }
22
+
23
+ void loop ()
24
+ {
25
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * EEPROM Read
3
+ *
4
+ * Reads the value of each byte of the EEPROM and prints it
5
+ * to the computer.
6
+ * This example code is in the public domain.
7
+ */
8
+
9
+ #include < EEPROM.h>
10
+
11
+ // start reading from the first byte (address 0) of the EEPROM
12
+ int address = 0 ;
13
+ byte value;
14
+
15
+ void setup ()
16
+ {
17
+ // initialize serial and wait for port to open:
18
+ Serial.begin (9600 );
19
+ while (!Serial) {
20
+ ; // wait for serial port to connect. Needed for Leonardo only
21
+ }
22
+ EEPROM.begin (512 );
23
+ }
24
+
25
+ void loop ()
26
+ {
27
+ // read a byte from the current address of the EEPROM
28
+ value = EEPROM.read (address);
29
+
30
+ Serial.print (address);
31
+ Serial.print (" \t " );
32
+ Serial.print (value, DEC);
33
+ Serial.println ();
34
+
35
+ // advance to the next address of the EEPROM
36
+ address = address + 1 ;
37
+
38
+ // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
39
+ // on address 512, wrap around to address 0
40
+ if (address == 512 )
41
+ address = 0 ;
42
+
43
+ delay (500 );
44
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * EEPROM Write
3
+ *
4
+ * Stores values read from analog input 0 into the EEPROM.
5
+ * These values will stay in the EEPROM when the board is
6
+ * turned off and may be retrieved later by another sketch.
7
+ */
8
+
9
+ #include < EEPROM.h>
10
+
11
+ // the current address in the EEPROM (i.e. which byte
12
+ // we're going to write to next)
13
+ int addr = 0 ;
14
+
15
+ void setup ()
16
+ {
17
+ EEPROM.begin (512 );
18
+ }
19
+
20
+ void loop ()
21
+ {
22
+ // need to divide by 4 because analog inputs range from
23
+ // 0 to 1023 and each byte of the EEPROM can only hold a
24
+ // value from 0 to 255.
25
+ int val = analogRead (0 ) / 4 ;
26
+
27
+ // write the value to the appropriate byte of the EEPROM.
28
+ // these values will remain there when the board is
29
+ // turned off.
30
+ EEPROM.write (addr, val);
31
+
32
+ // advance to the next address. there are 512 bytes in
33
+ // the EEPROM, so go back to 0 when we hit 512.
34
+ // save all changes to the flash.
35
+ addr = addr + 1 ;
36
+ if (addr == 512 )
37
+ {
38
+ addr = 0 ;
39
+ EEPROM.commit ();
40
+ }
41
+
42
+ delay (100 );
43
+ }
You can’t perform that action at this time.
0 commit comments