PaulRB:
It would be much more efficient to store your passwords as long int instead of string (char *). Only half the space would be required.For less than 1000 paswords, you could use 1 byte of eeprom to indicate that a password had been used/deleted. For example if an entered password matched the stored password at index 123, then you could write a 1 to eeprom address 123. When the next entered password was matched, for example with the password stored at index 234, you would read the eeprom address 234 to see if a 1 had been previously written there, before accepting the password.
Ok. But I am a bit confused. What I did in the previous sketch means it has stored 520 passwords in the flash memory which is 32 Kb but not in the EEPROM, is that right?
Below I am giving a working sketch that I used and wrote 36 passwords in the flash memory. It is working fine but is it possible that I could delete the correct password in run time? I saw on the forum that it was not possible but now it is due to a third party bootloader.
I think it is possible to read and write in EEPROM in run time, isn't it? And you are suggesting that if the number of passwords is less than 1000, I should go for EEPROM and not the flash memory, did I understand you correctly?
#include <avr/pgmspace.h>
#include <Keypad.h>
const char string_0[] PROGMEM = "1234567"; // "String 0" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "1232345";
const char string_2[] PROGMEM = "6384625";
const char string_3[] PROGMEM = "1129864";
const char string_4[] PROGMEM = "2312345";
const char string_5[] PROGMEM = "7429573";
const char string_6[] PROGMEM = "1234167";
const char string_7[] PROGMEM = "1232145";
const char string_8[] PROGMEM = "6384125";
const char string_9[] PROGMEM = "1129164";
const char string_10[] PROGMEM = "2312145";
const char string_11[] PROGMEM = "7429173";
const char string_12[] PROGMEM = "1234267";
const char string_13[] PROGMEM = "1232245";
const char string_14[] PROGMEM = "6384225";
const char string_15[] PROGMEM = "1129264";
const char string_16[] PROGMEM = "2312245";
const char string_17[] PROGMEM = "7429273";
const char string_18[] PROGMEM = "1234667";
const char string_19[] PROGMEM = "1232645";
const char string_20[] PROGMEM = "6384625";
const char string_21[] PROGMEM = "1129664";
const char string_22[] PROGMEM = "2312645";
const char string_23[] PROGMEM = "7429673";
const char string_24[] PROGMEM = "1234067";
const char string_25[] PROGMEM = "1232045";
const char string_26[] PROGMEM = "6384025";
const char string_27[] PROGMEM = "1129064";
const char string_28[] PROGMEM = "2312045";
const char string_29[] PROGMEM = "7429073";
const char string_30[] PROGMEM = "1234867";
const char string_31[] PROGMEM = "1232845";
const char string_32[] PROGMEM = "6384825";
const char string_33[] PROGMEM = "1129764";
const char string_34[] PROGMEM = "2312845";
const char string_35[] PROGMEM = "7429873";
// Then set up a table to refer to your strings.
const char *const availablePassword[36] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9, string_10, string_11, string_12, string_13, string_14, string_15, string_16, string_17, string_18, string_19, string_20, string_21, string_22, string_23, string_24, string_25, string_26, string_27, string_28, string_29, string_30, string_31, string_32, string_33, string_34, string_35};
char buffer[8]; // make sure this is large enough for the largest string it must hold
#define Password_Length 8
char enteredPassword [Password_Length];
byte data_count = 0;
bool passwordAccepted = false;
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {3, 4, 5, 6};
byte colPins[COLS] = {7, 8, 9};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for native USB
}
void loop() {
/* Using the string table in program memory requires the use of special functions to retrieve the data.
The strcpy_P function copies a string from program space to a string in RAM ("buffer").
Make sure your receiving string in RAM is large enough to hold whatever
you are retrieving from program space. */
char customKey = customKeypad.getKey();
if (customKey) {
enteredPassword[data_count] = customKey;
Serial.print(enteredPassword[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {
for (int i = 0; i < 36; i++) {
strcpy_P(buffer, (char *)pgm_read_word(&(availablePassword[i])));
if (!strcmp(enteredPassword, buffer)) {
passwordAccepted = true;
break;
}
passwordAccepted = false;
}
if (passwordAccepted) {
Serial.println("Password accepted.");
}
else {
Serial.println("Wrong Password...");
}
clearData();
}
}
void clearData() {
while (data_count != 0) {
enteredPassword[data_count--] = 0;
}
return;
}