2 Purpose: To write data to EEPROM memory.
3 Dr. Purdum, Aug. 27, 2012
6 #define DEBUG 1 // We want to see debug print statements
7 // Comment out these lines to avoid seeing print statements
8 const int MAXPEOPLE = 10;
9 struct servicePeople { // Structure definition for servicePeople
16 union servicePeopleUnion { // A union definition for myUnion
18 struct servicePeople testServicePeople;
20 servicePeople myPeople[MAXPEOPLE] = { // company data for testing
21 {0x3737, "This is a dummy","admin",5555555}, // ID of dummy needs to be non-zero!
22 {101,"Kack Lawn Service","Clowder",2345678},
23 {222,"Jane's Plants","Noah",4202513},
24 {333,"Terrys Pool Service","Billings",4301832}
26 // function declarations:
27 void DataDump(struct servicePeople temp);
30 void ReadOneRecord(int index);
31 void WriteFirstRecord();
33 int loopCounter = 0; // Number of passes to make through loop
34 int initFlag = 0; // Has the EEPROM been initialized?
35 struct servicePeople temp; // A temporary structure
43 eepromMax = FindEepromTop(); // How much EEPROM?
45 Serial.print("EepromMax = ");
46 Serial.println(eepromMax);
48 initFlag = ReadIntFlag(); // Initialized?
50 Serial.print(" flag = ");
51 Serial.println(initFlag);
53 for (i = 0; i < MAXPEOPLE; i++) {
58 Purpose: Find out how much EEPROM this board has.
70 Purpose: This function reads the first two bytes of EEPROM and
71 returns the integer found there.
75 int 0 if no records in EEPROM, 1 if there are
79 int *ptr = &myUnion.testID;
80 *ptr = EEPROM.read(0);
81 return myUnion.testID;
84 Purpose: This function writes one record from the myPeople[] array
87 int index The element of the myPeople[] array to write
91 void WriteOneRecord(int index)
95 int offset = index * sizeof(servicePeople);
97 b = (byte *) &myPeople[index]; // Going to write this record
98 for (i = 0; i < sizeof(servicePeople); i++)
99 EEPROM.write(i + offset, *b++);
103 static int eepromIndex = 1; // Assume there are records
105 if (initFlag > 0) { // There are records to read
106 ReadOneRecord(eepromIndex++);
107 if (myUnion.testServicePeople.ID != 0) { // Read some real data
108 DataDump(myUnion.testServicePeople);
111 eepromIndex++; // Make sure loop can end with no records
114 Serial.println("==========");
116 if (eepromIndex == MAXPEOPLE) {
122 Purpose: This function reads one servicePerson record from
125 int index The element of the myPerson[] array to read
131 void ReadOneRecord(int index)
137 offset = index * sizeof(servicePeople); // must offset from 0 in EEPROM
138 bPtr = (byte *) &myUnion.testServicePeople; // where to place the data read
139 for (i = 0; i < sizeof(temp); i++) { // Loop through the bytes...
140 *bPtr = EEPROM.read(offset + i);
145 Purpose: Sends the data stored in parameter to the serial monitor
147 struct servicePeople temp // The data to be displayed
151 void DataDump(struct servicePeople temp)
155 Serial.print("ID = ");
156 Serial.print(temp.ID);
157 Serial.print(" Name = ");
158 Serial.println(temp.Name);
159 Serial.print(" PW = ");
160 Serial.print(temp.PW);
161 Serial.print(" Phone = ");
162 Serial.println(temp.Phone);