Saving input on a 4x4 keypad

Hi, I am a new programmer and im trying to make a 4-letter passcode system.
But I have a problem where I can't save inputs, only display them.
just for reference I am using a 16x2 lcd, Arduino mega, and a 4x4 keypad.
This is my code:

#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
    int f = 0;
int unlocked = 0;// using 1, and 0 to tell if the passcode is correct
 int passcode = 0; // the pascode that is set
 int apasscode = 0; //means attempted passcode 
 int position = 0;// same as pos
 int pos = 0;// where the courser is on the x axis
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {53, 51, 49, 47};
byte colPins[COLS] = {43, 41, 39, 37}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(115200);
  lcd.setCursor(0,0);
  pos = -5;
  lcd.print("please set code");
  delay(2000);
  pos = 0;
  lcd.clear();
}

void loop()
{
  char key = keypad.getKey();
  if ((key)&&(pos > -1))
  {
    pos = pos + 1;
    lcd.print(key);
  }

  if (pos > 3) {
    lcd.clear();
    lcd.print("Code set"); // need to save input so I can set the passcode to what was typed in.
    delay(2000);
    lcd.clear();
    delay(2000);
      lcd.print("Enter code");
      delay(1500);
      lcd.clear();
      pos = -5;
      f = 1;
    }
    if((f == 1)&&(key))
{lcd.print(key); 
position = position + 1;
}
if ((position == 4)&&(apasscode == passcode)) {
  f = 0;
  lcd.clear();
  lcd.print("Unlocked");
  delay(1000);
  lcd.clear();
  unlocked = 1;
}
if ((position == 4)&&(apasscode != passcode)) {
  f = 0;
  lcd.clear();
  lcd.print("Wrong");
  delay(1000);
  lcd.clear();
  lcd.print("Enter code");
   delay(1000);
  lcd.clear();
 
}
  if (unlocked == 1) {} // will put what happens here
  
}

If you need anything else clarified just ask.

You should create different functions for setting new passcode, attempting passcode but wrong, attempting passcode and correct, what happens if input is wrong, what happens if input is correct.
@alto777 mentioned this yesterday and this site is slick, imo. Wish this was around when I started Arduino in about 2014!
Take a look:
https://wokwi.com/projects/344891391763022419
compare it to yours, (don't forget to change the pin numbers as needed for mega vs uno), I highly recommend you take the time to understand what this code is doing that's different from yours.
Of particular note, breaking your whole code into functions and learning to call them from the main loop. It makes your code easier borrow from later on, reuse within a larger code, maintain and troubleshoot, easier for others to look through and understand.
Note: the code on the referenced page is not mine.

1 Like

Thanks, you helped me with both of these questions and been super helpful

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.