Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

simple if else program for pushbuttons (password entering) help

Status
Not open for further replies.

numair_noor

Member level 2
Joined
Mar 15, 2012
Messages
50
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,715
hi, i need some one's help in my code. my code does this:
1:it ask for entering user name:
if user name is correct e.g nasir,basit,imran etc
2.it ask's for entering password
otherwise go back to entering username
PROBLEM:
1.it is not entering into the ''entering password'' screen.
2.i dont konw if it is comparing the user names or not.


code:
Code:
bit pass_flag;
bit bb,nn,ii;
 char kp[3];
 char basit[5]={'b','a','s','i','t'},nasir[5]={'n','a','s','i','r'},imran[5]={'i','m','r','a','n'};//saeed[5]={'s','a','e','e','d'};
 char basit_pass[5]={'1','2','3','4','5'},nasir_pass[5]={'2','3','4','5','6'},imran_pass[5]={'3','4','5','6','7'};


//const char nreg[], nreg[];
unsigned char x,y,f,i=0,j=0;
void LCD_SHOW(char x,char y);
void name();
void Lcd_Init();
void dis_ent_pass();
void lcd_welcome();

// Lcd module connections
sbit LCD_RS at P0_0_bit;
sbit LCD_EN at P0_1_bit;

sbit LCD_D4 at P0_2_bit;
sbit LCD_D5 at P0_3_bit;
sbit LCD_D6 at P0_4_bit;
sbit LCD_D7 at P0_5_bit;
// End Lcd module connections

///////start lcd function
LCD_SHOW(char x,char y)
{
if(pass_flag==0)
{
 Lcd_chr(2,x+1,y);}
 else if(pass_flag==1)
 {
 Lcd_out(2,x+1,"*");
 }
 //return(x,y);
}
///end lcd function


void main()
{
//again:
p1=0x00;
p2=0x00;
p3=0x00;
Lcd_Init();
  while(1)
          {
////////push buttons FUNCTIONS START////////////////
             pass_flag=0;
             Lcd_Cmd(_LCD_CLEAR);                     // Clear display
             Lcd_Cmd(_LCD_CURSOR_OFF);
             LCD_OUT(1,1,"enter name");
              if(pass_flag==0)
              {
              name();//enter name
              }

              else if(pass_flag==1)
              {
              name(); //enter password if password flag is 1
              }
              pass_flag=0;
          }
}

void name()         //function for registeration number
{
         for(i=0;i<5;i++)
         {
         while( (p1.b0!=1) && (p1.b1!=1) && (p1.b2!=1) && (p1.b3!=1) && (p1.b4!=1) && (p1.b5!=1) && (p1.b6!=1) && (p1.b7!=1) && (p3.b0!=1) && (p3.b1!=1) && (p3.b2!=1) && (p3.b3!=1) && (p3.b4!=1) && (p3.b5!=1) && (p3.b6!=1) && (p3.b7!=1) && (p2.b0!=1) && (p2.b1!=1) && (p2.b2!=1) && (p2.b3!=1) && (p2.b4!=1) && (p2.b5!=1) && (p2.b6!=1) && (p2.b7!=1) ); //wait for any key to be pressed
          //////////////////new start/////////////////
          if(p2.b0==1) //for a
                 {
                 f='a';
                 kp[i]='a';
                 LCD_SHOW(i,f);
                 while(p2.b0!=0);
                 }

         else if(p2.b1==1)   //for b
                 {
                 f='b';
                 kp[i]='b';
                 LCD_SHOW(i,f);
                 while(p2.b1!=0);
                 }


         else if(p2.b2==1)   //for d
                 {
                 f='d';
                 kp[i]='d';
                 LCD_SHOW(i,f);
                 while(p2.b2!=0);
                 }

         else if(p2.b3==1) //for l
                 {
                 f='e';
                 kp[i]='e';
                 LCD_SHOW(i,f);
                 while(p2.b3!=0);
                 }

         else if(p2.b4==1)   //for m
                 {
                 f='i';
                 kp[i]='i';
                LCD_SHOW(i,f);
                 while(p2.b4!=0);
                 }


         else if(p2.b5==1)   //for m
                 {
                 f='m';
                 kp[i]='m';
                 LCD_SHOW(i,f);
                 while(p2.b5!=0);
                 }


         else if(p2.b6==1)   ///for n
                 {
                 f='n';
                 kp[i]='n';
                 LCD_SHOW(i,f);
                 while(p2.b6!=0);
                 }

         else if(p2.b7==1)   //////for r
                 {
                 f='r';
                 kp[i]='r';
                 LCD_SHOW(i,f);
                 while(p2.b7!=0);
                 }
        else if(p3.b7==1)   ////for s
                 {
                 f='s';
                 kp[i]='s';
                 LCD_SHOW(i,f);
                 while(p3.b7!=0);
                 }

        else if(p3.b6==1)   ////for t
                 {
                 f='t';
                 kp[i]='t';
                 LCD_SHOW(i,f);
                 while(p3.b6!=0);
                 }

          else if(p1.b0==1)    ////for 0
                 {
                 f='0';
                 kp[i]='0';
                 LCD_SHOW(i,f);
                 while(p1.b0!=0);
                 }

          else if(p1.b1==1)        //for 1
                 {
                 f='1';
                 kp[i]='1';
                 LCD_SHOW(i,f);
                 while(p1.b1!=0);
                 }

          else if(p1.b2==1)        //for 2
                 {
                 f='2';
                 kp[i]='2';
                 LCD_SHOW(i,f);
                 while(p1.b2!=0);
                 }

          else if(p1.b3==1)        //for 3
                 {
                 f='3';
                 kp[i]='3';
                 LCD_SHOW(i,f);
                 while(p1.b3!=0);
                 }

          else if(p1.b4==1)        //for 4
                 {
                 f='4';
                 kp[i]='4';
                 LCD_SHOW(i,f);
                 while(p1.b4!=0);
                 }

          else if(p1.b5==1)        //for 5
                 {
                 f='5';
                 kp[i]='5';
                 LCD_SHOW(i,f);
                 while(p1.b5!=0);
                 }

          else if(p1.b6==1) ///for 6
                 {
                 f='6';
                 kp[i]='6';
                 LCD_SHOW(i,f);
                 while(p1.b6!=0);
                 }

          else if(p1.b7==1)        //for 7
                 {
                 f='7';
                 kp[i]='7';
                 LCD_SHOW(i,f);
                 while(p1.b7!=0);
                }

          else if(p3.b0==1) //for 8
                 {
                 f='8';
                 kp[i]='8';
                 LCD_SHOW(i,f);
                 while(p3.b0!=0);
                 }

          else if(p3.b1==1)        //for 9
                 {
                 f='9';
                 kp[i]='9';
                 LCD_SHOW(i,f);
                 while(p3.b1!=0);
                 }
          else if(p3.b2==1) //////////for *
                 {
                 f='*';
                 kp[i]='*';
                 LCD_SHOW(i,f);
                 while(p3.b0!=0);
                 }

          else if(p3.b3==1)    /////for #
                 {
                 f='#';
                 kp[i]='#';
                 LCD_SHOW(i,f);
                 while(p3.b3!=0);
                 }
           /////////////////////name end before comparison/////////////
         }
                         if( (kp[0]==nasir[0]) &&   ( kp[1]==nasir[1]) &&     ( kp[2]==nasir[2]) && ( kp[3]==nasir[3]) && (kp[4]==nasir[4]) && (pass_flag==0))
                    {
                      nn=1;
                      dis_ent_pass();

                    }
                    else if( (kp[0]==basit[0])  &&  ( kp[1]==basit[1])   &&   ( kp[2]==basit[2]) &&  ( kp[3]==basit[3]) && (kp[4]==basit[4])&& (pass_flag==0))
                    {
                      bb=1;
                      dis_ent_pass();

                    }
                    else if( (kp[0]==imran[0])  &&  ( kp[1]==imran[1])   &&   ( kp[2]==imran[2]) &&  ( kp[3]==imran[3]) && ( kp[4]==imran[4])&& (pass_flag==0))
                    {
                      ii=1;
                      dis_ent_pass();

                    }
                    else
                    {
                    LCD_OUT(1,1,"enter name");
                    }

        // else if(pass_flag==1)
          //          {
                    if((kp[0]==nasir_pass[0])  &&  ( kp[1]==nasir_pass[1])   &&   ( kp[2]==nasir_pass[2])  &&  ( kp[3]==nasir_pass[3])   &&   ( kp[4]==nasir_pass[4]) && (nn==1))
                    {
                     lcd_welcome();
                    }
               else if((kp[0]==basit_pass[0])  &&  ( kp[1]==basit_pass[1])   &&   ( kp[2]==basit_pass[2]) &&  ( kp[3]==basit_pass[3])   &&   ( kp[4]==basit_pass[4]) && (bb==1))
                    {
                     lcd_welcome();
                    }
               else if (  (kp[0]==imran_pass[0])  &&  ( kp[1]==imran_pass[1])   &&   ( kp[2]==imran_pass[2]) &&  ( kp[3]==imran_pass[3])   &&   ( kp[4]==imran_pass[4]) && (ii==1) )
                    {
                     lcd_welcome();
                    }

                    //}
}

void dis_ent_pass()
{
                    Lcd_Cmd(_LCD_CLEAR);
                    Lcd_Out(1,1,"ENTER PASSWORD");
                    pass_flag==1;
}

void lcd_welcome()
{
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,4,"WELCOME");
 pass_flag=0;
 nn=ii=bb=0;
}

- - - Updated - - -

this is the schematic for above code: this is 8052 based project written in c language in mikroc software.
2.png
 

sky_123

Advanced Member level 4
Joined
Feb 26, 2011
Messages
1,170
Helped
279
Reputation
566
Reaction score
273
Trophy points
1,363
Activity points
9,602
Your logic in your loop in the main function is screwed. The 'else if' won't execute if the pass flag was zero and the name() function had been called, even if the name() function has set the pass flag to 1. So, pass flag gets set to zero again at the end of the loop.
The if() and the else if() can't both execute in a single pass through the loop. You can't have your cake and eat it too, as it were.
 

numair_noor

Member level 2
Joined
Mar 15, 2012
Messages
50
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,715
thats what i coded in that way. i want to use name function twice. first time when the user enters the name and if the name is correct then only the name function is called again. for password entering. otherwise he has to enter the name again.
problem is:
when user enters name(right or wrong) the name function is not called again for password scaning.:(:(:(
 

sky_123

Advanced Member level 4
Joined
Feb 26, 2011
Messages
1,170
Helped
279
Reputation
566
Reaction score
273
Trophy points
1,363
Activity points
9,602
I know that's what you may "want", but that's not what you've coded. As mentioned, if the if() statement is true, then the else if() won't execute. That's a fact since 1972.
The if() and else if() can't function the way you seem to think it does.
 

numair_noor

Member level 2
Joined
Mar 15, 2012
Messages
50
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,715
OK. may be its is right what u are saying. but i am not getting your point dude. can u please write it correctly for me. thanks..

- - - Updated - - -

i get your point about if/else if. but still the problem is same. i am unable to find other mistake. where the flag is getting 0 again in loop. please tell me. thnks.
 

numair_noor

Member level 2
Joined
Mar 15, 2012
Messages
50
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,715
is it ok now?
its having same problem still.. please help..:(
Code:
bit bb,nn,ii,pass_flag,name_flag;
 char kp[3];
 char basit[5]={'b','a','s','i','t'},nasir[5]={'n','a','s','i','r'},imran[5]={'i','m','r','a','n'};//saeed[5]={'s','a','e','e','d'};
 char basit_pass[5]={'1','2','3','4','5'},nasir_pass[5]={'2','3','4','5','6'},imran_pass[5]={'3','4','5','6','7'};


//const char nreg[], nreg[];
unsigned char x,y,f,i=0,j=0;
void LCD_SHOW(char x,char y);
void name();
void Lcd_Init();
void dis_ent_pass();
void lcd_welcome();

// Lcd module connections
sbit LCD_RS at P0_0_bit;
sbit LCD_EN at P0_1_bit;

sbit LCD_D4 at P0_2_bit;
sbit LCD_D5 at P0_3_bit;
sbit LCD_D6 at P0_4_bit;
sbit LCD_D7 at P0_5_bit;
// End Lcd module connections

///////start lcd function
LCD_SHOW(char x,char y)
{
if(pass_flag==0)
{
 Lcd_chr(2,x+1,y);}
 else if(pass_flag==1)
 {
 Lcd_out(2,x+1,"*");
 }
 //return(x,y);
}
///end lcd function


void main()
{
//again:
p1=0x00;
p2=0x00;
p3=0x00;
Lcd_Init();
  while(1)
          {
////////push buttons FUNCTIONS START////////////////
             pass_flag=0;
             name_flag=1;
             Lcd_Cmd(_LCD_CLEAR);                     // Clear display
             Lcd_Cmd(_LCD_CURSOR_OFF);
             LCD_OUT(1,1,"enter name");
              if(pass_flag==0)
              {
                name();//enter name
              }
              if(pass_flag==1)
                   {
                   name(); //enter password if password flag is 1
                  // pass_flag=0;
                   }
              pass_flag=0;


          }
}

void name()         //function for registeration number
{
         for(i=0;i<5;i++)
         {
         while( (p1.b0!=1) && (p1.b1!=1) && (p1.b2!=1) && (p1.b3!=1) && (p1.b4!=1) && (p1.b5!=1) && (p1.b6!=1) && (p1.b7!=1) && (p3.b0!=1) && (p3.b1!=1) && (p3.b2!=1) && (p3.b3!=1) && (p3.b4!=1) && (p3.b5!=1) && (p3.b6!=1) && (p3.b7!=1) && (p2.b0!=1) && (p2.b1!=1) && (p2.b2!=1) && (p2.b3!=1) && (p2.b4!=1) && (p2.b5!=1) && (p2.b6!=1) && (p2.b7!=1) ); //wait for any key to be pressed
          //////////////////new start/////////////////
          if(p2.b0==1) //for a
                 {
                 f='a';
                 kp[i]='a';
                 LCD_SHOW(i,f);
                 while(p2.b0!=0);
                 }

         else if(p2.b1==1)   //for b
                 {
                 f='b';
                 kp[i]='b';
                 LCD_SHOW(i,f);
                 while(p2.b1!=0);
                 }


         else if(p2.b2==1)   //for d
                 {
                 f='d';
                 kp[i]='d';
                 LCD_SHOW(i,f);
                 while(p2.b2!=0);
                 }

         else if(p2.b3==1) //for l
                 {
                 f='e';
                 kp[i]='e';
                 LCD_SHOW(i,f);
                 while(p2.b3!=0);
                 }

         else if(p2.b4==1)   //for m
                 {
                 f='i';
                 kp[i]='i';
                LCD_SHOW(i,f);
                 while(p2.b4!=0);
                 }

         else if(p2.b5==1)   //for m
                 {
                 f='m';
                 kp[i]='m';
                 LCD_SHOW(i,f);
                 while(p2.b5!=0);
                 }
                 
         else if(p2.b6==1)   ///for n
                 {
                 f='n';
                 kp[i]='n';
                 LCD_SHOW(i,f);
                 while(p2.b6!=0);
                 }

         else if(p2.b7==1)   //////for r
                 {
                 f='r';
                 kp[i]='r';
                 LCD_SHOW(i,f);
                 while(p2.b7!=0);
                 }
        else if(p3.b7==1)   ////for s
                 {
                 f='s';
                 kp[i]='s';
                 LCD_SHOW(i,f);
                 while(p3.b7!=0);
                 }

        else if(p3.b6==1)   ////for t
                 {
                 f='t';
                 kp[i]='t';
                 LCD_SHOW(i,f);
                 while(p3.b6!=0);
                 }

          else if(p1.b0==1)    ////for 0
                 {
                 f='0';
                 kp[i]='0';
                 LCD_SHOW(i,f);
                 while(p1.b0!=0);
                 }

          else if(p1.b1==1)        //for 1
                 {
                 f='1';
                 kp[i]='1';
                 LCD_SHOW(i,f);
                 while(p1.b1!=0);
                 }

          else if(p1.b2==1)        //for 2
                 {
                 f='2';
                 kp[i]='2';
                 LCD_SHOW(i,f);
                 while(p1.b2!=0);
                 }

          else if(p1.b3==1)        //for 3
                 {
                 f='3';
                 kp[i]='3';
                 LCD_SHOW(i,f);
                 while(p1.b3!=0);
                 }

          else if(p1.b4==1)        //for 4
                 {
                 f='4';
                 kp[i]='4';
                 LCD_SHOW(i,f);
                 while(p1.b4!=0);
                 }

          else if(p1.b5==1)        //for 5
                 {
                 f='5';
                 kp[i]='5';
                 LCD_SHOW(i,f);
                 while(p1.b5!=0);
                 }

          else if(p1.b6==1) ///for 6
                 {
                 f='6';
                 kp[i]='6';
                 LCD_SHOW(i,f);
                 while(p1.b6!=0);
                 }

          else if(p1.b7==1)        //for 7
                 {
                 f='7';
                 kp[i]='7';
                 LCD_SHOW(i,f);
                 while(p1.b7!=0);
                }

          else if(p3.b0==1) //for 8
                 {
                 f='8';
                 kp[i]='8';
                 LCD_SHOW(i,f);
                 while(p3.b0!=0);
                 }

          else if(p3.b1==1)        //for 9
                 {
                 f='9';
                 kp[i]='9';
                 LCD_SHOW(i,f);
                 while(p3.b1!=0);
                 }
          else if(p3.b2==1) //////////for *
                 {
                 f='*';
                 kp[i]='*';
                 LCD_SHOW(i,f);
                 while(p3.b0!=0);
                 }

          else if(p3.b3==1)    /////for #
                 {
                 f='#';
                 kp[i]='#';
                 LCD_SHOW(i,f);
                 while(p3.b3!=0);
                 }
           /////////////////////name end before comparison/////////////
         }


         if((name_flag==1)&&(pass_flag==1))
        {
                    if((kp[0]==nasir_pass[0])  &&  ( kp[1]==nasir_pass[1])   &&   ( kp[2]==nasir_pass[2])  &&  ( kp[3]==nasir_pass[3])   &&   ( kp[4]==nasir_pass[4]) && (nn==1))
                    {
                     lcd_welcome();
                    }
               else if((kp[0]==basit_pass[0])  &&  ( kp[1]==basit_pass[1])   &&   ( kp[2]==basit_pass[2]) &&  ( kp[3]==basit_pass[3])   &&   ( kp[4]==basit_pass[4]) && (bb==1))
                    {
                     lcd_welcome();
                    }
               else if (  (kp[0]==imran_pass[0])  &&  ( kp[1]==imran_pass[1])   &&   ( kp[2]==imran_pass[2]) &&  ( kp[3]==imran_pass[3])   &&   ( kp[4]==imran_pass[4]) && (ii==1) )
                    {
                     lcd_welcome();
                    }

          }
          
        if((pass_flag==0)&&(name_flag==1))
         {
                         if( (kp[0]==nasir[0]) &&   ( kp[1]==nasir[1]) &&     ( kp[2]==nasir[2]) && ( kp[3]==nasir[3]) && (kp[4]==nasir[4]) && (pass_flag==0))
                    {
                      nn=1;
                      dis_ent_pass();

                    }
                    else if( (kp[0]==basit[0])  &&  ( kp[1]==basit[1])   &&   ( kp[2]==basit[2]) &&  ( kp[3]==basit[3]) && (kp[4]==basit[4])&& (pass_flag==0))
                    {
                      bb=1;
                      dis_ent_pass();

                    }
                    else if( (kp[0]==imran[0])  &&  ( kp[1]==imran[1])   &&   ( kp[2]==imran[2]) &&  ( kp[3]==imran[3]) && ( kp[4]==imran[4])&& (pass_flag==0))
                    {
                      ii=1;
                      dis_ent_pass();

                    }
         }


}

void dis_ent_pass()
{
                    Lcd_Cmd(_LCD_CLEAR);
                    Lcd_Out(1,1,"ENTER PASSWORD");
                    pass_flag=1;

}

void lcd_welcome()
{
 //pass_flag=0;
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,4,"WELCOME");
 nn=0;
 ii=0;
 bb=0;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top