I am writing a code for a user to enter a 4 digit pin, then use this pin later to unlock a box. The loop is stuck between the "ready to unlock box" and "enter pin" it just keeps flipping between values but i want both to be displayed, just at different times.
#include <Keypad.h> //install keypad library
#include <LiquidCrystal.h> //install LCD screen library
const byte ROWS = 4; //include four rows of keypad
const byte COLS = 4; //indclude four cols of keypad
char keys[ROWS][COLS] = { //set keypad array equal to numbers on keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
int pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin8;
int code[] = {pin1, pin2, pin3, pin4};
byte rowPins[ROWS] = {45,43,41,39}; //connect to the row pins of keypad
byte colPins[COLS] = {37,35,33,31}; //connent to the column pins of keypad
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //set up pins lcd will be connected to
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //set keypad up to use as variable
int solenoidPin = 1; //connect motor pin to code
bool in_press = false;
void setup() {
Serial.begin(9600); //start code
lcd.begin(16,2); //start screen with dimensions
}
void loop() //loop of functions to run through
{
int key; //initialize key as integer
lcd.setCursor(0,0);
lcd.print("Press # to lock,");//welcome screen
lcd.setCursor(0,1);
lcd.print("or * to unlock.");
key = keypad.getKey(); //let arduino know it is waiting for a value
while (in_press && key == NO_KEY) //check if they want to begin
{
in_press = false; //if no key was clicked
return; //exit the loop and restart
}
while (key != NO_KEY){ //if a key was clicked
switch(key){
case '#':
lcd.clear();
lcd.setCursor(0,0);
*lcd.print("Ready to lock"); //let user know ready for next step of process
lcd.setCursor(0,1);
lcd.print("keys!");
delay(1000);
EnterPin1();
break;*
case '*':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ready to unlock"); //let user know ready for next step of process
lcd.setCursor(0,1);
lcd.print("keys!");
delay(1000);
EnterPin5();
break;
default:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No input");
lcd.setCursor(0,1);
lcd.print("detected.");
break;
}
}
}
void EnterPin1()
{
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
bool in_press = false;
int pin1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter your pin.");//let user know you are waiting for input
delay(1000);
pin1 = keypad.getKey(); //let arduino know it is waiting for a value
while (in_press && pin1 == NO_KEY) //check if they want to begin
{
in_press = false; //if no key was clicked
return; //exit the loop and restart
}
while (pin1 != NO_KEY) //if a key was clicked
{
switch(pin1){
case '1':
pin1 = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*"); //let user know ready for next step of process
EnterPin2(); //go to enter pin function
break;
case'2':
pin1 = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*"); //let user know ready for next step of process
delay(1000);
EnterPin2();
break;
case '3':
pin1 = 3;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '4':
pin1 = 4;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '5':
pin1 = 5;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '6':
pin1 = 6;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '7':
pin1 = 7;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '8':
pin1 = 8;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
case '9':
pin1 = 9;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*");
EnterPin2();
break;
}
}
}