I have a application in UNO where the user will enter a value ( using a matrix keypad ) ranging from 1000 to 9999 seeing it on a LCD screen.
I need to save this to the EEPROM after he hits ENTER on keypad..
The user will NOT enter a value. The user will press some keys. When you see that a key has been pressed, you need to decide what to do with the key press, based on what key the user pressed. If it is the ENTER key (whatever that means), you convert the stored characters to an int, and store the int in EEPROM. If the key is not ENTER, you might want to store the value in an array of chars (with a terminating NULL).
You can use atoi() to then convert the NULL terminated array of chars to an int.
If the value has those limits, you don't need a long; and int will do. I think the union makes good sense, since the EEPROM librray does byte reads and writes. Then you could do something like:
union {
byte digits[3]; // For null if needed
int val;
} myUnion;
// the setup() and loop() code...
int FetchInt(int offset)
{
int i;
for (i = 0; i < sizeof(int); i++) {
myUnion.digits[i] = EEPROM.read(offset++);
}
rerturn myUnion.val;
}
void SetInt(int value, int offset)
{
int i;
myUnion.val = value; // move value into the union
for (i = 0; i < sizeof(int); i++) {
EEPROM.write(offset++, myUnion.digits[i]); // Save it to EEPROM
}
}
This is untested, but it might serve as a starting point. The code assumes that input from the pad is converted to an int, perhaps using atoi(), and that value is then placed in the union.
I have never used the atoi() function and reason why I overlooked it. Thanks for reminding.
For instance to enter "1234" the user will press the keys 1,2,3,4 in that order.
I simply save the numeric values 1,2,3,4 into a temporary null terminated char array of size [5] and convert it into an int for verification / validation etc. Once done I directly save the char array[5] into the EEPROM.
Later when I want to EEPROM I can simply read the char array and send it to LCD for user view..
Delta_G had also proposed something similar I think.
Econjack : Is the argument to the sizeof() correct ?