Trying to Convert Keypad Matrix into Int Array but Values get messed up

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int i = 0;
int m = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'D','C','B','A'},
{'#','9','6','3'},
{'*','7','4','1'},
{'0','8','5','2'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key = keypad.getKey();

void setup(){
lcd.begin(16,2);
lcd.backlight();
Serial.begin(9600);

}

void loop(){
lcd.setCursor(0,0);
lcd.print("Password: ");
lcd.setCursor(0,1);
byte pwtrue[8] = {1,2,3,4,5,6,7,8};
int pwtrial[8];
while (i<8) {
key = keypad.getKey();
if (key) {
pwtrial = key;

  • Serial.print(i);*
  • i = i + 1;*
    _ Serial.println(pwtrial*);*_

* }*
* }*
* while (m<8) {*
* if (pwtrial[m] != pwtrue[m]) {*
* lcd.print("Wrong Password");*
* m = 8; *
* }*
* m = m + 1;*
* }*
}
This is my code right now and I'm trying to get the keypad.getLey() into pwtrial*. However, this is the results on the serial. I also put the variable i and m as global variables otherwise the loop keeps on restarting.*
0 -256
1 256
2 -16126
3 8703
4 770
5 513
6 1
7 513
The left numbers are i, and the numbers on the right are pwtrial from 1 - 7.

Why have you declared your keys[] matrix in a format that does not match with the physical layout of the 4x4 Keypad? Do you really need to do it or an experiment?
4x4Keypad.png

4x4Keypad.png

You declared pwtrial as an 8-element array of integers

int pwtrial[8];

You then reference the array in your code:

if (key) {
      pwtrial = key;
      Serial.print(i);
      i = i + 1;
      Serial.println(pwtrial);
      
    }

Used without the brackets, pwtrial is a pointer to the first byte of the array.. To set a specific element of the array to a value, you need to specify which element of the array to use, in your case using pwtrial[ i ]

You did this properly later on in the code:

if (pwtrial[m] != pwtrue[m])

But this line of code will not work the way you expect it to. keypad.getKey() returns an ASCII character, not an integer, so even if you enter the correct keys, you will not get a match.

it looks like you want to capture a sequence of key presses in the pwtrial array and after 8 key presses, compare it the the values in the pwtrue array.

loop() should check for a key press and only when there is one, do something.

looks like you want to display something on the LCD, but you should only need to do that once when something new needs to be display. you may need to keep track of state and i'll guess you only want to display "Wrong Password" for a limited amount of time

so i think one part of loop() should check for a keypress and store it. Since pwtrial is defined in loop(), it's values won't be persistent between loop() iterations. it should be declared globally like "i" is. pwtrue doesn't need to be redefined each loop() iteration and should also be global.

And only compare sequences when 8 key presses have occurred

    if (key = keypad.getKey())  {
        pwtrial [i++] = key;

        Serial.println(pwtrial [i-1]);
    }

    if (8 <= i)  {
        i = 0;      // needs to be reset somewhere

        for (int j = 0; j < 8; j++)  {
            if (pwtrue [j] != pwtrial [j])
                break;
        }

        if (8 > j)  {
            lcd.print("Wrong Password");
            delay (5000);
            lcd.print("Password: ");
        }

        else {
            // do what happens when password is correct
        }
    }