// Define button pins
const int BACK_KEY = 36;
const int UP_KEY = 34;
const int DOWN_KEY = 39;
const int ENTER_KEY = 35;
// Define debounce variables
unsigned long switchPressTime = 0;
const uint8_t Key_Debounce_Delay = 200; // Debounce delay for key press
// Define password and input variables
//const char correctPassword[] = "2468"; // Define your password here
char inputPassword[5] = "0"; // Variable to store the input password (4 digits + null terminator)
// Initialize button pins
void setup() {
Serial.begin(115200);
pinMode(BACK_KEY, INPUT_PULLUP);
pinMode(UP_KEY, INPUT_PULLUP);
pinMode(DOWN_KEY, INPUT_PULLUP);
pinMode(ENTER_KEY, INPUT_PULLUP);
DISP_2_ROW_MSG("Enter PIN", inputPassword);
}
// Main loop
void loop() {
Enter_Password();
}
void Enter_Password() {
DISP_2_ROW_MSG("Enter PIN", inputPassword);
while (true) {
if (Key_Press_With_Debounce(BACK_KEY)) {
// Remove the last digit of the input password
size_t len = strlen(inputPassword);
if (len > 1) {
inputPassword[len - 1] = '\0';
DISP_2_ROW_MSG("Enter PIN", inputPassword);
}
}
if (Key_Press_With_Debounce(UP_KEY)) {
// Increment the last digit of the input password
size_t len = strlen(inputPassword);
if (len > 0) {
char lastChar = inputPassword[len - 1];
lastChar = (lastChar == '9') ? '0' : lastChar + 1;
inputPassword[len - 1] = lastChar;
DISP_2_ROW_MSG("Enter PIN", inputPassword);
}
}
if (Key_Press_With_Debounce(DOWN_KEY)) {
// Decrement the last digit of the input password
size_t len = strlen(inputPassword);
if (len > 0) {
char lastChar = inputPassword[len - 1];
lastChar = (lastChar == '0') ? '9' : lastChar - 1;
inputPassword[len - 1] = lastChar;
DISP_2_ROW_MSG("Enter PIN", inputPassword);
}
}
if (Key_Press_With_Debounce(ENTER_KEY)) {
// Add a new digit to the input password if it's not full
size_t len = strlen(inputPassword);
if (len < 4) {
inputPassword[len] = '0'; // Add '0' for a new digit
inputPassword[len + 1] = '\0';
DISP_2_ROW_MSG("Enter PIN", inputPassword);
} else {
// Check if the password is correct
if (strcmp(inputPassword, "2468") == 0) {
Serial.println("Password correct!");
executeOtherFunctions();
} else {
DISP_2_ROW_MSG("Incorrect", "PIN");
delay(1000); // Use delay instead of vTaskDelay for simplicity
}
strcpy(inputPassword, "0"); // Reset input password
DISP_2_ROW_MSG("Enter PIN", inputPassword);
}
}
}
}
void executeOtherFunctions() {
// Add your functions to be executed after correct password entry
Serial.println("Executing other functions...");
}
bool Key_Press_With_Debounce(uint8_t Key_Name) {
if (!digitalRead(Key_Name) && (millis() - switchPressTime > Key_Debounce_Delay)) {
switchPressTime = millis(); // Update switchPressTime to current time
return true; // Key is pressed
}
return false; // Key is not pressed
}
void DISP_2_ROW_MSG(const char* line1, const char* line2) {
// Simulate a function to display messages
Serial.print(line1);
Serial.print(" ");
Serial.println(line2);
}