hi,
i didn't got much help online while writing a code to collect the numbers entered from a 4X4 matrix keypad and converting it into a 8 digit number..
here i am pasting a small code (may not be 100% ok, but works ) which collects the keys pressed and converts them into a 8 digit code ..
suggestions are appreciated !!!
// Program to collect the 8 digit pressed data and display on LCD
// You can skip the lcd program and see the result on serial port ..
#include <Keypad.h>
#include <LiquidCrystal.h>
int bitcounter = 0; // counter unless 8 digits are reached
int bitcount =1; // counter for switch statement to run 8 times .. ( see the switch statement in funtion )
volatile long int a,b,c,d,e,f,g,h,sum; // these are the variables where data will be collected from keypad ..
LiquidCrystal lcd(21,20,19,18,17,16);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'7','8','9','A'},
{'4','5','6','B'},
{'1','2','3','C'},
{'L','0','C','D'}
};
byte rowPins[ROWS] = {4,5,6,7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
lcd.print(" Restarted ");
delay(200);
lcd.clear();
}
void loop()
{
bitcount = 1;
lcd.setCursor(0, 0);
lcd.print ( " Main ");
lcd.setCursor(0, 1);
lcd.print(" Loop " );
char key = keypad.getKey();
if (key != NO_KEY){
if ( key == 'A' ) // jump to 'getnumber' funtion
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Programing Now");
delay(1000);
lcd.clear();
lcd.print("Enter 8 digit No");
getnumber (); // funtions returns the card entered from keypad ...
}
if ( key == 'D' ) // displays the contents on serial port or / and lcd
{
// Test display code on serial port
// Serial.print("a = ");
// Serial.println(a);
// Serial.print("b = ");
// Serial.println(b);
// Serial.print("c = ");
// Serial.println(c);
// Serial.print("d = ");
// Serial.println(d);
// Serial.print("e = ");
// Serial.println(e);
// Serial.print("f = ");
// Serial.println(f);
// Serial.print("g = ");
// Serial.println(g);
// Serial.print("h = ");
// Serial.println(h);
//
// Serial.print("sum = ");
// Serial.println(sum);
// Serial.flush();
lcd.clear();
lcd.print(sum); // print the 8 digit number
delay(1000);
//
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ( " Main ");
lcd.setCursor(0, 1);
lcd.print(" Loop " );
}
}
//temp1=0;
}
//GET NUMBER ENTRY FUNTIONS
void getnumber ()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
if ( bitcounter <= 7)
{
switch (bitcount)
{
case 1: { lcd.clear(); a=key ; lcd.print(a,BYTE); bitcount++; break ; }
case 2: { b=key; lcd.print(b,BYTE); bitcount++; break ; }
case 3: { c=key ; lcd.print(c,BYTE); bitcount++; break ; }
case 4: { d=key ; lcd.print(d,BYTE); bitcount++; break ; }
case 5: { e=key ; lcd.print(e,BYTE); bitcount++; break ; }
case 6: { f=key ; lcd.print(f,BYTE); bitcount++; break ; }
case 7: { g=key ; lcd.print(g,BYTE); bitcount++; break ; }
case 8: {
h=key ;
lcd.print(h,BYTE);
delay(1000);
lcd.clear();
lcd.print("Data accepted");
delay(1000); lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Press 'A' ");
lcd.setCursor(0,1);
lcd.print(" to exit ");
bitcount++; break ;
}
}
bitcounter++;
}
}
bitcounter= 0;
if ( key != 'A' ) getnumber (); // repeats the 'getnumber' funtion unless exit program is not pressed ..
else // this part coverts the input data from keypad into 8 digit number and exit the 'getnumber' funtion
{
lcd.setCursor(0, 0);
lcd.print ( "Program Ends.. ");
lcd.setCursor(0, 1);
lcd.print("please wait " );
delay(200);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ( "Press D to ");
lcd.setCursor(0, 1);
lcd.print(" see the result " );
delay (2000);
lcd.clear();
a= a-48; b=b-48; c=c-48; d=d-48; e=e-48; f=f-48; g=g-48; h=h-48; // converting ascii value to interger
// multiplying numbers to get into 10's places ..
a=(a<<7)*78125;
b=(b<<6)*15625;
c=(c<<5)*3125;
d=(d<<4)*625;
e=(e<<3)*125;
f =(f<<2)*25;
g=(g<<1)*5;
sum = a + b + c +d + e + f + g +h; // you get the 8 digit code / number ...
}
}