#include <ShiftRegister74HC595.h>
const int kbdRows = 4;
const int kbdCols = 4;
int serialDataPin = 4; // DS
int clockPin = 2; // SHCP
int latchPin = 3; // STCP
unsigned long time_now = 0;
ShiftRegister74HC595<1> sr(serialDataPin, clockPin, latchPin);
// C5 C4 C3 C2
//const int pinCols[4]={9,8,7,6};
// R1 R2 R3 R4
const int pinRows[4]={13,12,11,10}; //pins of the differents rows
char act = "", ant = "";
int trueCols;
int trueRows;
char keys[4][4]={ //correspondence with the keys of the keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
void setup() {
Serial.begin(9600);
for(int i=0;i<4;i++){ //defining the output and input pins
//pinMode(pinCols[i], OUTPUT);
pinMode(pinRows[i], INPUT);
//digitalWrite(pinCols[i], HIGH);
}
sr.set(0, HIGH);
sr.set(1, HIGH);
sr.set(2, HIGH);
sr.set(3, HIGH);
}
void loop() {
time_now = millis();
readKey();
/*while(millis() < time_now + 100){
}
*/
/*
if (act != ant){
lastDebounceTime = millis();
}
else{
ant = act; //3
}
*/
//delay(100);
}
void readKey(){
for(int i=0; i<4;i++){
if(digitalRead(pinRows[i])==HIGH){ //reading if a row is active
trueRows=i; //so the right row is the one how is high
for(int x=0; x<4; x++){
sr.set(0, LOW); //putting all the cols low
sr.set(1, LOW);
sr.set(2, LOW);
sr.set(3, LOW);
sr.set(x, HIGH); //except the one we want to test
if(digitalRead(pinRows[trueRows])==HIGH){ //if now the row is high when a special column is high, we know the button how is pressed
while(digitalRead(pinRows[trueRows])==HIGH){
digitalRead(pinRows[trueRows]);
}
trueCols=x;
act = keys[trueRows][trueCols];
Serial.println(act); //we display the right symbol
}
}
}
}
sr.set(0, HIGH); //re-putting the cols to high position to be ready for an upcoming reading
sr.set(1, HIGH);
sr.set(2, HIGH);
sr.set(3, HIGH);
}