Debian Codegraph @

libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

raw
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 we're going to write to next) **/
12 int addr = 0;
13
14 void setup() {
15 /** Empty setup. **/
16 }
17
18 void loop() {
19 /***
20 Need to divide by 4 because analog inputs range from
21 0 to 1023 and each byte of the EEPROM can only hold a
22 value from 0 to 255.
23 ***/
24
25 int val = analogRead(0) / 4;
26
27 /***
28 Write the value to the appropriate byte of the EEPROM.
29 these values will remain there when the board is
30 turned off.
31 ***/
32
33 EEPROM.write(addr, val);
34
35 /***
36 Advance to the next address, when at the end restart at the beginning.
37
38 Larger AVR processors have larger EEPROM sizes, E.g:
39 - Arduino Duemilanove: 512 B EEPROM storage.
40 - Arduino Uno: 1 kB EEPROM storage.
41 - Arduino Mega: 4 kB EEPROM storage.
42
43 Rather than hard-coding the length, you should use the pre-provided length function.
44 This will make your code portable to all AVR processors.
45 ***/
46 addr = addr + 1;
47 if (addr == EEPROM.length()) {
48 addr = 0;
49 }
50
51 /***
52 As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
53 EEPROM address is also doable by a bitwise and of the length - 1.
54
55 ++addr &= EEPROM.length() - 1;
56 ***/
57
58
59 delay(100);
60 }