Trouble Using the Keypad to Log Inputs

Hey guys,

First time posting here so sorry if this has been discussed already. After looking through google i could not find a definitive answer on why i am still getting an infinite loop for my keypad. The goal is to treat the system like a safe. The keypad will log an array of 5 numbers and if they match the code then it can progress to the next state. If not it will revert back to a locked state after logging an attempt made on opening. Any help would be appreciated. Below is a snapshot of the code i am trying to figure out.

case Input: {
char keypressed;
char code_key[5];
for(int u = 0; u <= 4; u++){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY){
code_key = keypressed;
}
}
Serial.println(code_key);
}
break;
The print is just to see if it is working or not.
Although it is still a work in progress and should have more updates tomorrow; here is my whole code at the time being.
/* CSM ENGR 100 F18
Example of State Machine w/ Switch Case unsing Serial Monitor as Events
Author:J. Gutierrez

*/
#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{’*’, ‘0’, ‘#’, ‘D’}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
//Declare all the STATES that your program can be in (syntax used is STATE_STATENAME in all caps)
const byte Locked = 0;
const byte Input = 1;
const byte Unlocking = 2;
const byte Unlocked = 3;
const byte Locking = 4;
const byte Incorrect = 5;
const byte Timed_Out = 6;
const byte NUM_STATES = 7; //It can be useful to have the number of STATES
// Declare Arduino Pins
const int buttonPin1 = 11;
const int ledPin = 12; // the number of the LED pin
const char code[] = {1,3,5,7,9};
//Declare the Global Variable that keeps track of the STATE the program is in
byte CurrentState = Locked; //initialize to what ever state you want your program to start in
int serialInput = 0; // initialize Serial input
int buttonState1; //initialize button pin state
int lastbuttonState1;//initialize variable to remeber last button state
bool ledState = LOW; // variable for determining if LED is On or Off
bool ledBlinkState = LOW; // variable for determining if LED is Blinking or not
unsigned long previousTime = 0; // will store last time LED was updated
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println(“The Safe is currently Locked”);
Serial.println(“Please press the Button”);
Serial.println(“To prompt the code reader”);
}
void loop() {
// put your main code here, to run repeatedly:
serialInput = Serial.read();//read serial monitor
switch (CurrentState) {//Example of implementing FSM w/ a Switch Case
case Locked: {
//check for events first
if ((checkFor_Button1() == true)) { //switch to Input
CurrentState = Input;
Serial.println(“Please input designated code”);
break;// exit current switch case w/o doing STATE_A things
}
}
break;
case Input: {
char keypressed;
char code_key[5];
for(int u = 0; u <= 4; u++){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY){
code_key = keypressed;
}
}
Serial.println(code_key);
}
break;
case Unlocking: {
}
break;
case Unlocked: {
}
break;

case Locking: {
//check for events first
if ((checkFor_Button1() == true)) { //switch to Input
CurrentState = Input;
Serial.println(“Please input designated code”);
break;// exit current switch case w/o doing STATE_A things
}
}
break;
case Incorrect: {
//check for events first
if ((checkFor_Button1() == true)) { //switch to Input
CurrentState = Input;
Serial.println(“Please input designated code”);
break;// exit current switch case w/o doing STATE_A things
}
}
break;

case Timed_Out: {
//check for events first
if ((checkFor_Button1() == true)) { //switch to Input
CurrentState = Input;
Serial.println(“Please input designated code”);
break;// exit current switch case w/o doing STATE_A things
}
}
break;

}
}

//USER FUNCTIONS BELOW THIS LINE*******************************
//-------------Event Checker Functions-----------------------------------------------------------------
bool checkFor_Button1() {

bool result = false;
buttonState1 = digitalRead(buttonPin1);
if ( ((buttonState1 == HIGH) && (buttonState1 != lastbuttonState1)) || (serialInput == ‘B’) ) {
result = true;
delay(250);//delay to stop button from double reading a Button Event
}
lastbuttonState1 = buttonState1; //reset LastbuttonState1
return result;
}
//Functions To Manipulate LEDs*******************************************
void blinkLED(int ledpin, int blinkTime) {
unsigned long currentTime = millis();
//Serial.println(“currentTime =”,currentTime);
if (currentTime - previousTime >= blinkTime) {
// save the last time you blinked the LED
previousTime = currentTime;
// if the LED is off turn it on and vice-versa:
if (ledBlinkState == LOW) {
ledBlinkState = HIGH;
//Serial.println(“ledBlinkState Changed to HIGH”);
} else {
ledBlinkState = LOW;
//Serial.println(“ledBlinkState Changed to LOW”);
}
// set the LED with the ledState of the variable:
digitalWrite(ledpin, ledBlinkState);
}
}
Thanks for any and all help.
Tyler

Tyler - did you not notice that the majority of your post is underlined due to the [­u] early in the post ?

Please read this before posting a programming question and follow the advice on posting code