Hello,
I tried creating a code for a 4X4 keypad without the keypad library. Unfortunately it doesn't perfectly work. I'm sure the witring is correct and it is normal if my pins are not in INPUT_PULLUP because I wired real resistors in my wiring.
The buttons 4,5,6,B and *,#,D work perfectly. The buttons 7,8,9,C work weird beacause they always display respectively 1,2,3,A. And finally, the buttoons 1,2,3,A seems to work well but in fact, when Ipress them a short time, they display respectively 7,8,9,C.
I hope you cold help me,
Quintus
const int pinCols[4]={30,32,34,36}; //pins of the differents columns
const int pinRows[4]={22,24,26,28}; //pins of the differents rows
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);
}
}
void loop() {
readKey();
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++){
digitalWrite(pinCols[0], LOW); //putting all the cols low
digitalWrite(pinCols[1], LOW);
digitalWrite(pinCols[2], LOW);
digitalWrite(pinCols[3], LOW);
digitalWrite(pinCols[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
trueCols=x;
Serial.println(keys[trueRows][trueCols]); //we dispalay the right symbol
}
}
}
}
digitalWrite(pinCols[0], HIGH); //re-putting the cols to high position to be ready for an upcoming reading
digitalWrite(pinCols[1], HIGH);
digitalWrite(pinCols[2], HIGH);
digitalWrite(pinCols[3], HIGH);
}