keypad function return wrong number

92 views Asked by At

in ScanKey function which is for keypad 3*4 its work well until numbers length under 4 it works like this number = (number * 10) but for a number like this 123456 which have more than 4 number it gives me other numbers what should I do? my program works with an int that's I use integer. when I enter a number like 555555 it changes to 31267. IDE CodeVisionAVR C language for ATmega16a

struct User
{
    unsigned long int username;
    unsigned long int password;
    bool role;
};

unsigned long int username;
unsigned long int password;

 do
            {
                lcd_clear();
                lcd_putsf("Enter Username:");
                lcd_gotoxy(0, 1);
                sprintf(show, "%d", username);
                lcd_puts(show);
                lcd_gotoxy(0, 2);
                lcd_putsf("Enter Password:");
                lcd_gotoxy(0, 3);
                password = ScanKey();
                if (!password)
                {
                    break;
                }
                else if (password / 10 < 10)
                {
                    PORTC .1 = 1;
                    lcd_clear();
                    lcd_putsf("At least 3 numbers !");
                    delay_ms(150);
                    PORTC .1 = 0;
                }
            } while (password / 10 < 10);

        if (username && password)
        {
            user[UL].role = false;
            lcd_clear();
            if (currentUser < 0 && !currentUser)
            {
                lcd_putsf("Promote to admin ? ");
                lcd_gotoxy(0, 1);
                lcd_putsf("Yes press 1");
                lcd_gotoxy(0, 2);
                lcd_putsf("No press 2");
                if (ScanKey() == 1)
                {
                    user[UL].role = true;
                }
                lcd_gotoxy(0, 3);
            }
            user[UL].username = username;
            user[UL].password = password;
            UL++;
            PORTC .0 = 1;
            lcd_putsf("User added !!");
            delay_ms(150);
            PORTC .0 = 0;
        }
unsigned long int ScanKey(void)
{
    unsigned long int num = 0;
    _lcd_write_data(0x0F);
    PORTC .2 = 1;
    while (1)
    {
        PORTD .0 = 0;
        PORTD .1 = 1;
        PORTD .2 = 1;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0)
                ;
            lcd_putchar('1');
            num = (num * 10) + 1;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0)
                ;
            lcd_putchar('4');
            num = (num * 10) + 4;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0)
                ;
            lcd_putchar('7');
            num = (num * 10) + 7;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0)
                ;
            _lcd_write_data(0x10);
            lcd_putchar(' ');
            _lcd_write_data(0x10);
            num /= 10;
        }

        PORTD .0 = 1;
        PORTD .1 = 0;
        PORTD .2 = 1;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0)
                ;
            lcd_putchar('2');
            num = (num * 10) + 2;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0)
                ;
            lcd_putchar('5');
            num = (num * 10) + 5;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0)
                ;
            lcd_putchar('8');
            num = (num * 10) + 8;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0)
                ;
            lcd_putchar('0');
            num = (num * 10) + 0;
        }

        PORTD .0 = 1;
        PORTD .1 = 1;
        PORTD .2 = 0;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0)
                ;
            lcd_putchar('3');
            num = (num * 10) + 3;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0)
                ;
            lcd_putchar('6');
            num = (num * 10) + 6;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0)
                ;
            lcd_putchar('9');
            num = (num * 10) + 9;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0)
                ;
            break;
        }
    }
    PORTC .2 = 0;
    _lcd_write_data(0x0C);
    return num;
}```
0

There are 0 answers