const byte numRow =4;
const byte numCol=4;
int debounceTime=10;
char keymap[numRow][numCol]={ {'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'} };
byte colPin[numRow]={8,7,6,5};
byte rowPin[numRow]={13,12,11,10};
void setup()
{
for (int r=0 ;r<numRow;r++)
pinMode(rowPin[r], INPUT_PULLUP);
for (int c=0 ; c<numCol;c++)
{
pinMode(colPin[c], OUTPUT);
digitalWrite(colPin[c],HIGH);
}
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char key=getKey();
if(key!=0)
Serial.println("THE KEY IS ");
Serial.println(key);
delay(2000);
}
char getKey(){
char key =0;
for (int c=0; c<numCol;c++)
{
digitalWrite(colPin[c], LOW);
for (int r=0 ;r<numRow;r++){
if(digitalRead(rowPin[r])==LOW)
{
delay(debounceTime);
while(digitalRead(rowPin[r]) == LOW);
key = keymap[r][c];
}}
digitalWrite(colPin[c],HIGH);
}
return key;
}