Re: Keypad for Gated Entry

MarkR,

The EEPROM is like an array of bytes. In this case the array is 512 bytes long. Of course the advantage of EEPROM is that the bytes you write to EEPROM are preserved even after the power has been turned off.

Just as each element of an array has a subscript, each byte of EEPROM has an address. The first byte's address is 0, the second byte address is 1, etc., up to the last byte, which has an address of 511.

Unlike an array, however, you need to use a function to read or write a byte from/to EEPROM. To write a byte to EEPROM, use:

EEPROM.write(addr,b);

Where 'addr' is the address (0 - 511), and 'b' is the byte you want to write.

To read a byte from EEPROM, you would say:

b = EEPROM.read(addr);

So, the first example you gave writes the EEPROM with a 0 at address 0, a 1 at address 1, etc.

The second example uses analogRead to read the voltage at a pin, writes the value to EEPROM, waits 100 milliseconds, then does it again. It also adds one to the address each time, so each successive value goes to the next position in the EEPROM. There's an interesting twist in this code: when it reaches the end of the EEPROM, it goes back to the beginning, and starts overwriting the previous 512 values. This means that the EEPROM will contain the most recent 512 voltages read with analogRead().

So you see, the analogRead() has absolutely nothing to do with reading and writing the EEPROM. In the example, analogRead is used just as a source for data to write to EEPROM. The data could just have easily come from a keypad instead of analogRead().

I hope this helps.

-Mike