1

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;
}```
  • 2
    `31267 = 555555 - 16*32768 = 555555 - 8*65536` looks like you're discarding high-order bits of `num`. `num` is an `unsigned long int` but `ScanKey()` returns a value of type `int`. You may be able to configure your compiler to warn you about "conversion to smaller type" – pmg Jun 08 '21 at 11:52
  • Can you change the return type of `ScanKey` to `unsigned long`? That will allow you to return 9-digit numbers. – Ian Abbott Jun 08 '21 at 12:06
  • Aside: I'm wondering what sort of magic is allowing numeric member names for `PIND` etc. – Ian Abbott Jun 08 '21 at 12:08
  • I've changed the ScanKey return type to an unsigned long int but still have this problem. I've also printed the num variable inside ScanKey it gives me the wrong number. – Zavosh Ghorbanpour Jun 08 '21 at 12:12
  • also change the variable type inside the calling function, ie `int foo = ScanKey();` must be changed to `long unsigned foo = ScanKey();` ... remember to print it with `"%lu"` is `printf("ScanKey was %lu\n", foo);`. **Mind your compiler warnings** – pmg Jun 08 '21 at 12:13
  • @pmg I've added some of my variables and part of my function that call ScanKey. I've tried `"%lu"` but it prints u in LCD. also I don't have any warning after build. – Zavosh Ghorbanpour Jun 08 '21 at 12:34
  • Does your version of the compiler support long long data type? – cup Jun 08 '21 at 12:37
  • @cup no it doesn't – Zavosh Ghorbanpour Jun 08 '21 at 13:04

0 Answers0