help with a keypad reader

Thread Starter

eessoo

Joined Jan 29, 2012
23
Instead of a switch statement in keypad( ), use an array, a look-up-table.
This is much more simple and efficient.
the maximum value is 100
WOW very good idea on the array :D
now we remove the keypad function and add

int keypad[]={0,55,56,57,0,52,53,54,0,49,50,51,0,13,48,46,0} ;

i tested it
it give the same output ,wrong output
if i type 13.3 it gives output 10 :confused:
 

Thread Starter

eessoo

Joined Jan 29, 2012
23
Rich (BB code):
int keypad[]={0,55,56,57,0,52,53,54,0,49,50,51,0,13,48,46,0} ;
int key=0;
float x;
int h=10;
char txt[15];
float kpt=0;


char keypadPort at PORTD;
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;


void main() {

 Keypad_Init();
 lcd_init();
 while(1)
 {
         do
         key=keypad_key_click();
         while(!key);
                           key=keypad[key];
                        if(key==13)
                              {
                              lcd_cmd(_lcd_clear);
                              break;
                              }

                        else if(key==46)
                         {
                                        while(1)
                                        {lcd_chr_cp(key);
                                          do
                                         key=keypad_key_click();
                                         while(!key);
                                          key=keypad[key];
                                          kpt=((key-48)/10)+kpt;

                                                         if(key==13)
                                                         {lcd_cmd(_lcd_clear);
                                                         goto finaly;}

                                         }
                                        break;
                         }
                        else if(key!=13)
                         {lcd_chr_cp(key);
                          kpt=kpt*10+(key-48);}
}
finaly:
floattostr(kpt,txt);
Lcd_Out_CP(txt);
}
 

Thread Starter

eessoo

Joined Jan 29, 2012
23
I FINALLY FIXED IT (i didn't really fix it, i just change some things) :D
Rich (BB code):
int keypad[]={0,55,56,57,0,52,53,54,0,49,50,51,0,13,48,46,0} ;
int key=0;
float x;
int h=10;
char txt[15];
float kpt=0;
int dot=0;

char keypadPort at PORTD;
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;


void main() {

 Keypad_Init();
 lcd_init();
 while(1)
 {
         do
         key=keypad_key_click();
         while(!key);
                           key=keypad[key];
                        if(key==13)
                              {
                              lcd_cmd(_lcd_clear);
                              break;
                              }

                        else if(key==46)
                         {  lcd_chr_cp(key);
                            dot=1;
                         }
                        else
                         {lcd_chr_cp(key);
                          kpt=kpt*10+(key-48);}
                          
}
if (dot==1)
{kpt=(kpt/10);
}
finaly:
floattostr(kpt,txt);
Lcd_Out_CP(txt);
}
now help me figure out a way to make the user add more than one number after the Dot :D

thanks MrChips
 
Last edited:

MrChips

Joined Oct 2, 2009
30,809
Ok. When you set dot = 1, set divisor = 1,
Every time a digit is pressed when dot == 1, multiply the divisor by 10.
When ENTER is pressed divide the results by the divisor.

(What do you do if the dot is pressed more than once? Error checking is always the most tedious part of any program.)
 

Thread Starter

eessoo

Joined Jan 29, 2012
23
Finally done with the keypad :D
i don't wan to do the error part (enough keypad for now)

tomorrow i will start a line following robot :)
any advice?
 
Top