Can't get a password lock to work with 4x4 keypad

#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x26, 16, 3);

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] = {34, 36, 38, 40};
byte colPins[COLS] = {26, 28, 30, 32};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

char inputSequence[4] = "    ";
int inputNumber = 0;

void setup() {

  lcd.init();
  lcd.clear();
  lcd.backlight();
  Serial.begin(115200);
  lcd.print("Welcome");
}


void loop() {
  char key = customKeypad.getKey();
  if (key) {
    Serial.print(key);
    inputSequence[inputNumber] = customKeypad.getKey();
    inputNumber++;
    lcd.setCursor(inputNumber, 1);
    lcd.print(key);
  }
}

i have started on what is supposed to be an objective device for airsoft/paintball that requires you to enter a series of codes to disarm said device. i cannot for the life of me figure out how i am going to make the password portion. this device requires you to enter 4, 4 digit alphanumeric codes with numbers that will be displayed on screen in a specific sequence. you must then enter the codes in said sequence from left to right. what i just stated does not matter as i can most likely complete the rest of them with an example for storing and confirming whether just 1 code is indeed correct or incorrect.

Welcome

char inputSequence[4] = "    ";

This string is 5 characters not 4, you should have a warning or an error about it

See this nice example, it may help : electronic-safe - Wokwi Arduino simulator

works just fine with 4. the code i showed is just the code that will display the code onto the lcd. i will go check out what you sent me.

what you sent me uses EEPROM. to my understanding there is only a finite amount of writing you can do with EEPROM. to make the boards last as long as possible i want to avoid that

The EEPROM would only be written when you wish to change the key code. I doubt you propose to do that several thousand times, let alone approach the durability of the EEPROM.

So there is no point discussing snippets, is there? :grimacing:

1 Like

several hours and many fried brain cells later i finally made something thats fully functional. ill post it in a sec.

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x26, 16, 3);

const int ROW_NUM = 4;
const int COLUMN_NUM = 4;

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte pin_rows[ROW_NUM] = {34, 36, 38, 40};
byte pin_column[COLUMN_NUM] = {26, 28, 30, 32};

Keypad customKeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String rand1 = "4478";
const String rand2 = "6940";
const String rand3 = "3312";
const String rand4 = "1675";

String ipass;
String ipass2;
String ipass3;
String ipass4;

int code_pos = 0;

char inputSequence[4] = "    ";
int inputNumber = -1;
int inputNumber2 = 0;
int inputNumber3 = 0;
int inputNumber4 = 0;

void setup() {
  Serial.begin(9600);

  lcd.init();
  lcd.clear();
  lcd.backlight();

  ipass.reserve(32);
  ipass2.reserve(32);
  ipass3.reserve(32);
  ipass4.reserve(32);

  lcd.setCursor(0, 0);
  lcd.print(rand1);
  lcd.setCursor(5, 0);
  lcd.print(rand2);
  lcd.setCursor(10, 0);
  lcd.print(rand3);
  lcd.setCursor(15, 0);
  lcd.print(rand4);

  lcd.setCursor(4, 0);
  lcd.print('|');
  lcd.setCursor(4, 1);
  lcd.print('|');
  lcd.setCursor(4, 2);
  lcd.print('|');
  lcd.setCursor(4, 3);
  lcd.print('|');

  lcd.setCursor(9, 0);
  lcd.print('|');
  lcd.setCursor(9, 1);
  lcd.print('|');
  lcd.setCursor(9, 2);
  lcd.print('|');
  lcd.setCursor(9, 3);
  lcd.print('|');

  lcd.setCursor(14, 0);
  lcd.print('|');
  lcd.setCursor(14, 1);
  lcd.print('|');
  lcd.setCursor(14, 2);
  lcd.print('|');
  lcd.setCursor(14, 3);
  lcd.print('|');
}

void loop() {
  if (code_pos == 0) {
    char key = customKeypad.getKey();
    if (key) {
      Serial.print(key);
      inputSequence[inputNumber] = customKeypad.getKey();
      inputNumber++;
      lcd.setCursor(inputNumber, 3);
      lcd.print(key);
      if (key == '*') {
        lcd.setCursor(0, 3);
        lcd.print("    ");
        inputNumber = -1;
        ipass = "";
      }
      else if (key == '#') {
        if (rand1 == ipass) {
          Serial.println("rand1 is correct");
          lcd.setCursor(4, 3);
          lcd.print("|");
          lcd.setCursor(0, 1);
          lcd.print("crct");
          code_pos = 1;
        }
        else {
          Serial.println("rand1 is incorrect");
          lcd.setCursor(0, 2);
          lcd.print("icrt");
          lcd.setCursor(0, 3);
          lcd.print("    ");
          inputNumber = -1;
        }
        ipass = "";
      }
      else {
        ipass += key;
      }
    }
  }
  if (code_pos == 1) {
    char key2 = customKeypad.getKey();
    if (key2) {
      inputSequence[inputNumber2] = customKeypad.getKey();
      inputNumber++;
      lcd.setCursor(inputNumber, 3);
      lcd.print(key2);
      Serial.print(key2);
      if (key2 == '*') {
        lcd.setCursor(5, 3);
        lcd.print("    ");
        inputNumber = 4;
        ipass2 = "";
      }
      else if (key2 == '#') {
        if (rand2 == ipass2) {
          Serial.println("rand2 is correct");
          lcd.setCursor(9, 3);
          lcd.print("|");
          lcd.setCursor(5, 1);
          lcd.print("crct");
          code_pos = 2;
        }
        else {
          Serial.println("rand2 is incorrect");
          lcd.setCursor(5, 2);
          lcd.print("icrt");
          lcd.setCursor(5, 3);
          lcd.print("    ");
          inputNumber = 4;
        }
        ipass2 = "";
      }
      else {
        ipass2 += key2;
      }
    }
  }
  if (code_pos == 2) {
    char key3 = customKeypad.getKey();
    if (key3) {
      inputSequence[inputNumber3] = customKeypad.getKey();
      inputNumber++;
      lcd.setCursor(inputNumber, 3);
      lcd.print(key3);
      Serial.print(key3);
      if (key3 == '*') {
        lcd.setCursor(10, 3);
        lcd.print("    ");
        inputNumber = 9;
        ipass3 = "";
      }
      else if (key3 == '#') {
        if (rand3 == ipass3) {
          Serial.println("rand3 is correct");
          lcd.setCursor(14, 3);
          lcd.print("|");
          lcd.setCursor(10, 1);
          lcd.print("crct");
          code_pos = 3;
        }
        else {
          Serial.println("rand3 is incorrect");
          lcd.setCursor(10, 2);
          lcd.print("icrt");
          lcd.setCursor(10, 3);
          lcd.print("    ");
          inputNumber = 9;
        }
        ipass3 = "";
      }
      else {
        ipass3 += key3;
      }
    }
  }
  if (code_pos == 3) {
    char key4 = customKeypad.getKey();
    if (key4) {
      inputSequence[inputNumber4] = customKeypad.getKey();
      inputNumber++;
      lcd.setCursor(inputNumber, 3);
      lcd.print(key4);
      Serial.print(key4);
      if (key4 == '*') {
        lcd.setCursor(15, 3);
        lcd.print("    ");
        inputNumber = 14;
        ipass4 = "";
      }
      else if (key4 == '#') {
        if (rand4 == ipass4) {
          Serial.println("rand4 is correct");
          lcd.setCursor(19, 3);
          lcd.print("|");
          lcd.setCursor(15, 1);
          lcd.print("crct");
          code_pos = 4;
        }
        else {
          Serial.println("rand4 is incorrect");
          lcd.setCursor(15, 2);
          lcd.print("icrt");
          lcd.setCursor(15, 3);
          lcd.print("    ");
          inputNumber = 14;
        }
        ipass4 = "";
      }
      else {
        ipass4 += key4;
      }
    }
  }
}