Continue to Site

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.

[SOLVED] Convert Keypad Input Char Array into String To Compare with Predefined String

Status
Not open for further replies.

Nabil Fikri

Newbie level 3
Joined
Aug 1, 2015
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
Hello guys. Thank for stopping by here.
I made a PC based door lock keypad project. From the keypad, each key press will store a char to an array. After that, I want to convert the char array to string. The string will be send to PC to compare with the database there. The problem here is I cant find a way to convert the char array into string. I have done multiple trials but it the end it messed up the whole project. So, I need help from someone who have done this before.

Here is my full code. The code only works as stand alone door lock system. I can't find a way to send the string from here,
Code:
// Lcd module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
// End Lcd module connections
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void beep_once();
void correct();
void wrong();
int scan_keypad();
void init();

char keypadPort at PORTD;
unsigned short kp;
unsigned int x = 6;

unsigned char password_count=0;
// Declare an array to stall the 6-digit key in password
unsigned char keyin_char[6];
// Declare an array to stall the 6-digit desired password
unsigned char stalled_char[6]={'1','2','3','4','5','6'};

void main(){
    IRCF2_bit = 1;       //internal oscillator
    IRCF1_bit = 1;
    IRCF0_bit = 0;
    INTSRC_bit = 1;
    PLLEN_bit = 0;
    
    TRISA = 0b0000000;   //0-red, 1-green, 2-relay, 3-buzzer
    PORTA = 0b0000000;
    
    TRISB = 0b0000000;   //lcd
    TRISC = 0b0000000;   //0-3 are row
    TRISD = 0b0000111;   //0-2 are column

    ANSELA = 0;          //set all port as digital port I/O
    ANSELB = 0;
    ANSELC = 0;
    ANSELD = 0;
    
    Keypad_Init();       //initialize keypad
    init();
    Lcd_Init();          //initialize Lcd
    Lcd_Cmd(_LCD_CLEAR);              //Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);         //Cursor off
    Lcd_Out(1,2,"Enter Password");      //Prompt user to insert password
    
    while(1){           //main program running continuously
        scan_keypad();

        if(password_count==6){
            password_count=0;
            //compare the keyin value with stalled value to test whether password is correct
            if((keyin_char[0]==stalled_char[0])&&(keyin_char[1]==stalled_char[1])&&
                (keyin_char[2]==stalled_char[2])&&(keyin_char[3]==stalled_char[3])&&
                (keyin_char[4]==stalled_char[4])&&(keyin_char[5]==stalled_char[5]))
                {
                    Lcd_Cmd(_LCD_CLEAR);
                    Lcd_Out(1,2,"Door Unlocked!");
                    Lcd_Out(2,3,"Welcome Back!");

                    PORTA.F2 = 1;
                    PORTA = 0b0000101;
                    correct();
                    
                    delay_ms(10000);    //door open for 10 seconds
                    PORTA = 0b0000000; //reset output
                    init();
                    x = 6;
                    continue;
                }
            else
                {
                    Lcd_Cmd(_LCD_CLEAR);
                    Lcd_Out(1,2,"Wrong Password!");
                    Lcd_Out(2,1,"Please Try Again");

                    PORTA = 0b0000010;
                    wrong();

                    delay_ms(1000);
                    PORTA = 0b0000000; //reset output
                    init();
                    x = 6;
                    continue;
                }
        }
    }
    Delay_ms(250);
}

int scan_keypad(){

    PORTC = 0b0001110;                //scan 1st row
    if(PORTD.F0==0){                  //1
    while(PORTD.F0==0)continue;
    kp = 49;
    keyin_char[password_count]='1';
    password_count+=1;
    Lcd_Chr(2,x,'1');
    beep_once();
    x++;
    }
    else if (PORTD.F1==0){            //2
    while(PORTD.F1==0)continue;
    kp = 50;
    keyin_char[password_count]='2';
    password_count+=1;
    Lcd_Chr(2,x,'2');
    beep_once();
    x++;
    }
    else if (PORTD.F2==0){            //3
    while(PORTD.F2==0)continue;
    kp = 51;
    keyin_char[password_count]='3';
    password_count+=1;
    Lcd_Chr(2,x,'3');
    beep_once();
    x++;
    }

    PORTC = 0b0001101;                //scan 2nd row
    if(PORTD.F0==0){                  //4
    while(PORTD.F0==0)continue;
    kp = 52;
    keyin_char[password_count]='4';
    password_count+=1;
    Lcd_Chr(2,x,'4');
    beep_once();
    x++;
    }
    else if (PORTD.F1==0){            //5
    while(PORTD.F1==0)continue;
    kp = 53;
    keyin_char[password_count]='5';
    password_count+=1;
    Lcd_Chr(2,x,'5');
    beep_once();
    x++;
    }
    else if (PORTD.F2==0){            //6
    while(PORTD.F2==0)continue;
    kp = 54;
    keyin_char[password_count]='6';
    password_count+=1;
    Lcd_Chr(2,x,'6');
    beep_once();
    x++;
    }

    PORTC = 0b0001011;                //scan 3rd row
    if(PORTD.F0==0){                  //7
    while(PORTD.F0==0)continue;
    kp = 55;
    keyin_char[password_count]='7';
    password_count+=1;
    Lcd_Chr(2,x,'7');
    beep_once();
    x++;
    }
    else if (PORTD.F1==0){            //8
    while(PORTD.F1==0)continue;
    kp = 56;
    keyin_char[password_count]='8';
    password_count+=1;
    Lcd_Chr(2,x,'8');
    beep_once();
    x++;
    }
    else if (PORTD.F2==0){            //9
    while(PORTD.F2==0)continue;
    kp = 57;
    keyin_char[password_count]='9';
    password_count+=1;
    Lcd_Chr(2,x,'9');
    beep_once();
    x++;
    }

    PORTC = 0b0000111;                //scan 4th row
    if(PORTD.F0==0){                  //*
    while(PORTD.F0==0)continue;
    kp = 42;
    keyin_char[password_count]='*';
    password_count+=1;
    Lcd_Chr(2,x,'*');
    beep_once();
    x++;
    }
    else if (PORTD.F1==0){            //0
    while(PORTD.F1==0)continue;
    kp = 48;
    keyin_char[password_count]='0';
    password_count+=1;
    Lcd_Chr(2,x,'0');
    beep_once();
    x++;
    }
    else if (PORTD.F2==0){            //#
    while(PORTD.F2==0)continue;
    kp = 35;
    keyin_char[password_count]='#';
    password_count+=1;
    Lcd_Chr(2,x,'#');
    beep_once();
    x++;
    }
    return x;
}

void beep_once(){
    PORTA.F3 = 1;
    Delay_ms(100);
    PORTA.F3 = 0;
    Delay_ms(100);
}

void correct(){
    PORTA.F1 = 1;
    PORTA.F3 = 1;
    Delay_ms(250);
    PORTA.F1 = 0;
    PORTA.F3 = 0;
    Delay_ms(250);
    PORTA.F1 = 1;
    PORTA.F3 = 1;
    Delay_ms(250);
    PORTA.F1 = 0;
    PORTA.F3 = 0;
    Delay_ms(250);
}

void wrong(){
    PORTA.F0 = 1;
    PORTA.F3 = 1;
    Delay_ms(250);
    PORTA.F0 = 0;
    PORTA.F3 = 0;
    Delay_ms(250);
    PORTA.F0 = 1;
    PORTA.F3 = 1;
    Delay_ms(250);
    PORTA.F0 = 0;
    PORTA.F3 = 0;
    Delay_ms(250);
    PORTA.F0 = 1;
    PORTA.F3 = 1;
    Delay_ms(250);
    PORTA.F0 = 0;
    PORTA.F3 = 0;
    Delay_ms(250);
}

void init(){
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);              //Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);         //Cursor off
    Lcd_Out(1,2,"Enter Password");      //Prompt user to insert password
}

Any help is really appreciated.
 

You had not mentioned what compiler are using, but if it contains builtin ANSI C libraries such as the "string.h", you can enjoy with many functions that performs string handling.
 

I'm using MikroC Pro. Sorry I forgot to mention it.
 

I want to convert the array here
Code:
unsigned char keyin_char[6];
to string first before I send it through UART.

For example,
Code:
unsigned char keyin_char[6]={'1','2','3','4','5','6'};

//then how to convert to 123456 and then send via UART?

I tried multiple references from other forum but it did not work. I dont know where is the problem
 

try this,
Code:
unsigned char keyin_char[6];    // you can only store 5 characters in this arrray. Last char is the null terminator for string.
char *string_pointer = &keyin_char[0];

void main()
{
// your code...
//after you taking all the input from the keypad and before using "string_pointer" you need to add 

keyin_char[5] = NULL; // if "NULL" is not define then -> keyin_char[5] = '\0';

now you can use "string_pointer" where you need to get "keyin_char" as a string.

for safety you need to check whether the "string_pointer" is null right before use it.

Code:
if(string_pointer != NULL)
{
// because "string_pointer" is not null we can use it
}
 
Finally it works. Here is the full code
Code:
unsigned char keyin_char[5];    
unsigned char keyin_string[6];
int i;

for(x=0;x<6;x++){
keyin_string[i]=keyin_char[i];
}

keyin_string[5]='\0';

Thanks again guys
 

Instead of doing this

Code:
for(x=0;x<6;x++){
keyin_string[i]=keyin_char[i];
}

can you just do this

Code:
unsigned char keyin_char[5];    

keyin_char[5] = '\0';
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top