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.