I'm trying to read the input from a user using keypad 4x4 and compare the input using a finite state machine as shown below.
When I come to case fourth my state machine does not go to Check case but to the default instead and then takes a key to go to CHECK immediately which makes the user press five times instead of 4.
while (1)
{
key=get_key();
if (key>=0 && key <=9){
switch (password){
case FIRST:
if (key==7 && i==0)
{
temp++; // increasing every time we enter correct number
password=SECOND;
HAL_Delay(300);
break;
}
HAL_Delay(300);
i++; // To check if we have fed in wrong digit
if(i==4) {
password=CHECK;
}
break;
//**********************************************************************************************************
case SECOND:
if (key==3 && temp==1){
temp++;
HAL_Delay(300);
password=THIRD;
break;
}
HAL_Delay(300);
i++;
if((i==3) || (i==2)) {
password=CHECK;
}
break;
//****************************************************************************************************
case THIRD:
if (key==9 && temp==2)
{
temp++;
HAL_Delay(300);
password = FOURTH;
break;
}
HAL_Delay(300);
i++;
if((i==2) || (i==1)) {
password=CHECK;
}
break;
//*******************************************************************************************************
case FOURTH:
if (key==2 && temp==3)
{
temp++;
password=CHECK;
break;
}
//HAL_Delay(300);
i++;
if(i==1) {
password=CHECK;
}
break;
//****************************************************************************************************
case CHECK:
if (temp==4){ // if temp has incresed to 4 that means that all the numbers entered are correct
HAL_GPIO_WritePin(Led_test_GPIO_Port,Led_test_Pin,1);
HAL_GPIO_WritePin(Led2_test_GPIO_Port,Led2_test_Pin,0);
password=IDLE_PASSWORD;
}
else if((i==4) || (i==3) || (i==2) || (i==1)) {
HAL_GPIO_WritePin(Led2_test_GPIO_Port,Led2_test_Pin,1);
HAL_GPIO_WritePin(Led_test_GPIO_Port,Led_test_Pin,0);
password=IDLE_PASSWORD;
}
break;
//******************************************************************************************************
case IDLE_PASSWORD:
temp=0;
i=0;
password = FIRST;
break;
default:
password = FIRST;
break;
}
}
}