Click here to Skip to main content
14,390,083 members
Rate this:
Please Sign up or sign in to vote.
See more:
when i run this function now that has a case to check whether the password is correct or not it looks like it compares the first element and ignore the rest this is in c

void Enter_password(){

	key= get_key();

	if (key >= 0 && key <=9){

		switch (password_input){

		case FIRST:
			password[0]= key;

			HAL_Delay(300);
			password_input = SECOND;

			break;

		case SECOND:

			password[1]= key;

			HAL_Delay(300);
			password_input = THIRD;

			break;

		case THIRD:

			password[2]= key;

			HAL_Delay(300);
			password_input = FOURTH;

			break;

		case FOURTH:

			password[3]= key;

			HAL_Delay(300);
			password_input = CHECK;

		case CHECK:

			for (int i=0; i<sizeof(password_ACT_DEACT); i++){

				if (password[i] != password_ACT_DEACT[i]){

					TextLCD_Cmd(&lcd,0x80);
					TextLCD_Puts(&lcd,"Wrong!");
FLAG= false;
                    HAL_Delay(2000);
                    password_input= FIRST;
					break;
				}

				else {

					TextLCD_Cmd(&lcd,0x80);
					TextLCD_Puts(&lcd,"RIGHT!!");
FLAG=true;
					HAL_Delay(2000);
					password_input= FIRST;

					break;

				}

			}

		default:
			TextLCD_Clear(&lcd);
			password_input= FIRST;
			break;
		}
	}

}


What I have tried:

I have done a finite state machine to take the user 4 digit input and check if its correct or not
Posted
Updated 22-Mar-18 5:59am
v2
Comments
Rick York 21-Mar-18 21:16pm
   
Your observation is correct. It only looks at the first element.

This is a Q&A section. Do you have a question?
H W 22-Mar-18 5:45am
   
how to fix it?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your code should be enclosed in a neverending loop
void Enter_password()
{
  for (;;)
  {
    key= get_key();
    if (key >= 0 && key <=9){
      //...
    }//<- if closing brace
  }
}


Please note your code could be simplified without using a state machine: a for loop followed by a password check would be just enough.
   
v2
Comments
H W 22-Mar-18 5:23am
   
• Based on a STM32 MCU
• The alarm has an access panel consisting of a display and a keypad. A user interface is
shown on the display presenting the status of the alarm, and the possibility to
activate/deactivate the alarm via the keypad.
o The code is 4 digits long.
o There is an ordinary activation/deactivation code (7392)

4
o There is a master code to turn off a tripped alarm (6510)
• Three LEDs (green, yellow and red) indicating the state
o The green indicates that the alarm is deactivate (IDLE-state). (Pressing the activation
code will enter the ARMING state)
o The yellow indicates that the alarm is in ARMING state. (Either deactivate the alarm
or leave the premises within 30 seconds. Time is counting down shown on the
display otherwise the alarm is tripped)
o The red indicates that the alarm is full ARMED and active. (Any sensor tripped in
this state will sound the alarm)
H W 22-Mar-18 5:24am
   
Im trying to do the above that why im using switch for the password and then will be one for the leds@CPallini
Rate this:
Please Sign up or sign in to vote.

Solution 2

First, since this is the Q&A section, always remember to ask a question. You need to revise the CHECK case in your switch statement to something like this :
case CHECK :
    Flag = true;    // initialize to true
    for( int i=0; i < sizeof( password_ACT_DEACT ); i++ )
    {
        if( password[i] != password_ACT_DEACT[i] )
        {
            Flag = false;   // character did not match
            break;          // end the loop, have an answer
        }
    }

    TextLCD_Cmd(&lcd,0x80);
    if( Flag )
        TextLCD_Puts(&lcd,"RIGHT!!");
    else
        TextLCD_Puts(&lcd,"Wrong!");

    HAL_Delay(2000);
    password_input= FIRST;
    break;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100