user55004: 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;
   }
  }
 } 


user2967: 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.

55004: <p>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.
</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;Keypad.h&gt; //install keypad library
#include &lt;LiquidCrystal.h&gt; //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 &amp;&amp; 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 &amp;&amp; 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;
   }
  }
 } 
</code></pre>
 2967: <p>Your primary loop code sets <code>key</code>, and then has a <code>while (key != nokey) {}</code> 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.</p>

<pre><code>key = keypad.getKey();

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

<p>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 <code>key</code>, so the loop will never exit.</p>

<p>Write it like this instead: </p>

<p><strong>EDIT</strong></p>

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

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