In my project I need to store big 10 digit passwords that have only numbers. I dont understand eeprom in depth but I have been messing around with only 3 basic commands, write(), update(), and read().
Here is my main project code:
#include <Keypad.h>
#include <LiquidCrystal.h>
int currentRow = 1; // Start with the second row
const int LED = 2;
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 8, 7, 6, 5 };
byte colPins[COLS] = { 12, 11, 10, 9 };
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
String DEFAULT_PASSCODE = "1501";
String passcode = "";
String confirmPasscode = ""; // Variable to store the confirmed passcode
String Newpasscode = "";
enum State {
ENTER_PASSCODE,
PRESS_A_TO_START,
PASSCODE_CHANGE,
ENTER_NEW_PASSCODE,
CONFIRM_NEW_PASSCODE
};
State currentState = ENTER_PASSCODE;
bool passcodeChangeRequested = false;
int passcodeLength = 0; // Variable to keep track of the number of characters entered during passcode reset
void setup() {
pinMode(LED, OUTPUT);
lcd.begin(16, 2);
lcd.print("Enter Passcode:");
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
switch (currentState) {
case ENTER_PASSCODE:
handlePasscodeInput(key);
break;
case PRESS_A_TO_START:
if (key == 'A') {
lcd.clear();
lcd.print("Press B to Stop");
digitalWrite(LED, HIGH); // Turn on the LED
} else if (key == 'B') {
currentState = ENTER_PASSCODE;
lcd.clear();
lcd.print("Enter Passcode:");
passcode = "";
digitalWrite(LED, LOW); // Turn on the LED
} else if (key == '*') {
passcodeChangeRequested = true;
currentState = ENTER_NEW_PASSCODE;
lcd.clear();
lcd.print("Enter New Pass:");
Newpasscode = "";
}
break;
case ENTER_NEW_PASSCODE:
handleNewPasscodeInput(key); // Call handleNewPasscodeInput function
if (key == '#') {
currentState = CONFIRM_NEW_PASSCODE;
lcd.clear();
lcd.print("Confirm Pass:");
} else if (key == 'B') {
currentState = ENTER_PASSCODE;
passcode = "";
passcodeChangeRequested = false;
passcodeLength = 0; // Reset the passcode length
lcd.clear();
lcd.print("Enter Passcode:");
}
break;
case CONFIRM_NEW_PASSCODE:
handleConfirmPasscodeInput(key); // Call handleConfirmPasscodeInput function
break;
}
}
}
void handlePasscodeInput(char key) {
if (key != NO_KEY) {
if (passcode.length() < DEFAULT_PASSCODE.length()) {
passcode += key;
lcd.setCursor(passcode.length() - 1, 1);
lcd.print("*"); // Display an asterisk for each character entered
}
if (passcode.length() == DEFAULT_PASSCODE.length()) {
if (passcode.equals(DEFAULT_PASSCODE)) {
currentState = PRESS_A_TO_START;
lcd.clear();
lcd.print("Press A to start");
} else {
lcd.clear();
lcd.print("Incorrect Password");
lcd.setCursor(0, 1);
lcd.print("Please Try again");
delay(2000);
lcd.clear();
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1);
passcode = "";
}
}
}
}
void handleNewPasscodeInput(char key) {
if (key == '#') { // Change state when '=' key is pressed
currentState = CONFIRM_NEW_PASSCODE;
lcd.clear();
lcd.print("Confirm Pass:");
} else if (key == 'B') { // Reset passcode when 'B' key is pressed
currentState = ENTER_PASSCODE;
lcd.clear();
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1);
passcode = "";
} else if (key != NO_KEY) {
Newpasscode += key;
lcd.setCursor(Newpasscode.length() - 1, 1);
lcd.print("*");
}
}
void handleConfirmPasscodeInput(char key) {
if (key == '#') { // Change state when '=' key is pressed
if (Newpasscode == confirmPasscode) { // Check if the passcodes match
DEFAULT_PASSCODE = confirmPasscode; // Update the default passcode
currentState = ENTER_PASSCODE;
lcd.clear();
lcd.print("Passcode set");
delay(2000);
lcd.clear();
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1);
passcode = "";
passcodeLength = 0; // Reset the passcode length
} else {
lcd.clear();
lcd.print("Passcodes don't");
lcd.setCursor(0, 1);
lcd.print("match. Try again.");
delay(2000);
lcd.clear();
lcd.print("Enter New Pass:");
lcd.setCursor(0, 1);
Newpasscode = "";
confirmPasscode = "";
passcodeLength = 0; // Reset the passcode length
currentState = ENTER_NEW_PASSCODE; // Return to the new passcode entry state
}
} else if (key == 'B') { // Reset passcode when 'B' key is pressed
currentState = ENTER_PASSCODE;
lcd.clear();
lcd.print("Enter Passcode:");
lcd.setCursor(0, 1);
passcode = "";
passcodeLength = 0; // Reset the passcode length
} else if (key != NO_KEY) {
confirmPasscode += key;
lcd.setCursor(confirmPasscode.length() - 1, 1);
lcd.print("*"); // Display the entered digit
passcodeLength++; // Increment the passcode length
}
}
Where it says DEFAULT_PASSCODE = confirmPasscode; // Update the default passcode is exactly what I need to update into the EEPROM. DEFAULT_PASSCODE always needs to be on cell 0 just incase I forget my passcode and I cant get in and so all updated passcodes needs to be on cell 1 I say cell but its really address.
Here is the layout/my understanding code:
//Basicly Dont Touch (valueDT) is the default passcode we dont touch
//and Touch (valueT) is the passcode we update / touch
//If changepasscode is enabled than update valueT with the new password
//------------------------------------------------------------------------
//The main issue is that the passcode max character limit is 10 digits and
//the arduino can only allow values up to 255
#include <EEPROM.h>
int DT_DEFAULT_PASSCODE = 0; //address 1 DontTouch_DEFAULT_PASSCODE
int DEFAULT_PASSCODE = 1; //address 2 Updated DEFAULT_PASSCODE
int valueDT;//DontTouch default passcode
int valueT;//Touch default passcode
int changePasscode = 1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
EEPROM.write(DT_DEFAULT_PASSCODE, 245); // writing address 1 DontTouch_DEFAULT_PASSCODE
EEPROM.write(DEFAULT_PASSCODE, 123); // writing at address DEFAULT_PASSCODE, 123
valueDT = EEPROM.read(DT_DEFAULT_PASSCODE); //reading adress
valueT = EEPROM.read(DEFAULT_PASSCODE); //reading adress
if(changePasscode == 1){ //if I run change passcode
EEPROM.update(valueT, 456); //updating address to 150
changePasscode = 0;
}
delay(500);
Serial.println(valueT);//printing the new passcode
}