0

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;
   }
  }
 } 
0

Your primary loop code sets key, and then has a while (key != nokey) {} loop after, but the value of key never changes inside the while loop. Thus, if key is not equal to noKey, the while loop will never exit.

key = keypad.getKey();

while (key != noKey) {
  //code that does not change key
}

That is an endless loop. The fact that there’s a switch statement inside the while loop is immaterial. None of that code changes the value of key, so the loop will never exit.

Write it like this instead:

EDIT

key = keypad.getKey();
while (key != noKey) {
  //Your current code
  key = keypad.getKey();
}

You have the same logic problem in your enterPin1 function. You don’t provide the code for your enterPin2 function, but I would guess it also has the same logic problem.

  • That gives me error "expected ')' before ';' token", is there another part i should add in there? – Nicole Mar 19 at 1:11
  • Ok, I wasn’t sure if that syntax was legal or not. Apparently not. I’m at a tablet where I don’t have access to a C compiler. You could use your code, but add a final ` key = kaypad.getKey()` as the last statement in your while loop to fetch another value for key before looping back again. – Duncan C Mar 19 at 1:41
  • See the edit to my answer. There’s a more elegant way to do this but I don’t have access to a compiler at the moment to get it right for you. – Duncan C Mar 19 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.