Hardware Reference
In-Depth Information
Where possible, try to use byte-size values, but as you can see, it is possible
to store just about anything in EEPROM.
Example Program
In the previous chapter, you created a program that would greet the user, ask
for his name and age, and write some data to a serial port. However, when the
Arduino was unplugged, it forgot everything; the next time it was powered
on, it would ask for the same information. We'll build on that same program
but now store the responses in EEPROM. The Arduino should i rst check its
EEPROM memory. If no information is found, it will ask the user some questions
and then store that information into nonvolatile memory. If the information is
found, it will tell the user what information it has and then delete the contents
of its memory. It is now clear that an Arduino knows its ABCs, so I removed
that portion of code from the example. The program is shown in Listing 6-1.
Listing 6-1: Example program (code fi lename: Chapter6.ino )
1 #include <EEPROM.h>
2
3 #define EEPROM_DATAPOS 0
4 #define EEPROM_AGEPOS 1
5 #define EEPROM_NAMEPOS 2
6 #define EEPROM_CONTROL 42
7
8 char myName[] = {"Arduino"};
9 char userName[64];
10 char userAge[32];
11 unsigned char age;
12 int i;
13 byte myValue = 0;
14
15 void setup()
16 {
17 // Configure the serial port:
18 Serial.begin(9600);
19
20 // Does the EEPROM have any information?
21 myValue = EEPROM.read(EEPROM_DATAPOS);
22
23 if (myValue == 42)
24 {
25 // Get the user's name
26 for (i = 0; i < sizeof(userName); i++)
27 {
28 userName[i] = EEPROM.read(EEPROM_NAMEPOS + i);
29 }
30
 
Search WWH ::




Custom Search