Hello, I tried to program a 4x4 keypad to my liking using the 'keypad.h' library but as I am relatively new to the arduino platform and coding in general, I couldn't figure out how I could utilise the language reference to the corresponding library.
So I decided to to program a 4x4 keypad to my liking without using a library, I came up with the following code:
// all the needed libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// initialising the pins of the lcd
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
/*schematic of the keypad
C4 C3 C2 C1
R1 * * * *
R2 * * * *
R3 * * * *
R4 * * * *
*/
// variables for the column pins
int C4_pin = 2;
int C3_pin = 3;
int C2_pin = 4;
int C1_pin = 5;
// booleans for the column pins, to check if these pins are HIGH or LOW
bool C4_value = true;
bool C3_value = true;
bool C2_value = true;
bool C1_value = true;
//variables for the row pins
int R1_pin = 6;
int R2_pin = 7;
int R3_pin = 8;
int R4_pin = 9;
// booleans for the row pins, to check if these pins are HIGH or LOW
bool R1_value = true;
bool R2_value = true;
bool R3_value = true;
bool R4_value = true;
// array for the column pins
byte columnPins[4] = {C4_pin, C3_pin, C2_pin, C1_pin};
// array for the row pins
byte rowPins[4] = {R1_pin, R2_pin, R3_pin, R4_pin};
void setup() {
Serial.begin(9600);
// to initialise the row pins and set their mode
for(int i = 0; i<4; ++i){
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], LOW);}
// to initialise the column pins and set their mode
for(int x = 0; x<4; ++x){
pinMode(columnPins[x], OUTPUT);
digitalWrite(rowPins[x], HIGH);}}
void loop() {
// to check if a button has been pressed
if(digitalRead(columnPins[0] == LOW)){C4_value == false;}
if(digitalRead(columnPins[1] == LOW)){C3_value == false;}
if(digitalRead(columnPins[2] == LOW)){C2_value == false;}
if(digitalRead(columnPins[3] == LOW)){C4_value == false;}
// to figure out the placemant of the pressed button: column 4
if(C4_value == false){digitalWrite(rowPins[0], HIGH);}
if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");
Serial.println("wished character");}
if(C4_value == false){digitalWrite(rowPins[1], HIGH);}
if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
if(C4_value == false){digitalWrite(rowPins[2], HIGH);}
if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
if(C4_value == false){digitalWrite(rowPins[3], HIGH);}
if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
// to figure out the placemant of the pressed button: column 3
if(C3_value == false){digitalWrite(rowPins[0], HIGH);}
if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
if(C3_value == false){digitalWrite(rowPins[1], HIGH);}
if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
if(C3_value == false){digitalWrite(rowPins[2], HIGH);}
if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
if(C3_value == false){digitalWrite(rowPins[3], HIGH);}
if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
// to figure out the placemant of the pressed button: column 2
if(C2_value == false){digitalWrite(rowPins[0], HIGH);}
if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
if(C2_value == false){digitalWrite(rowPins[1], HIGH);}
if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
if(C2_value == false){digitalWrite(rowPins[2], HIGH);}
if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
if(C2_value == false){digitalWrite(rowPins[3], HIGH);}
if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
// to figure out the placemant of the pressed button: column 1
if(C1_value == false){digitalWrite(rowPins[0], HIGH);}
if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
if(C1_value == false){digitalWrite(rowPins[1], HIGH);}
if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
if(C1_value == false){digitalWrite(rowPins[2], HIGH);}
if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
if(C1_value == false){digitalWrite(rowPins[3], HIGH);}
if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
}
The problem is, it doesn't work for some reason. I uploaded a photo of my circuit that I built, I doubt that there is anything wrong with that so I think that there's something wrong with either my code or my understanding of 4x4 keypads. Any help would be greatly appreciated.
Note: my knowledge of 4x4 keypads is based of this article: How to Set Up a Keypad on an Arduino - Circuit Basics
Note: the uploaded diagram of the circuit doesn't contain a 4x4 keypad so you would have to imagine one, sorry.