So I am new to the arduino scene and have good knowledge of physics but limited knowledge of micro-electronics. I have been coding for years however
Basically what I'd like to do is have a 4x5 matrix of buttons that works using 4 output pins and 5 input pins (that looks similar to the attached image but with one extra column). My understanding is that I should be able to write a high signal to the output pins in turn (I'm using 2, 3, 4 and 5 in my code) and if i detect a high signal on the input pins (I'm using 6, 7, 8, 9 and 10) then that means that a particular button has been pressed.
I wrote the code below to test this concept, however when I plug in my arduino, it randomly detects key presses that are not present. Having said this when I attempt to short two pins (same effect as a button would have) it does detect them correctly but I can't have it detecting random key presses that I am not inputting.
Any ideas on why I might have a problem here and what I could do to fix it?
#include <Keyboard.h>
int OUT[] = {2, 3, 4, 5};
int IN[] = {6, 7, 8, 9, 10};
void setup() {
for(int i = 0; i < 4; i++){
pinMode(OUT[i], OUTPUT);
}
for(int i = 0; i < 5; i++){
pinMode(IN[i], INPUT);
}
Keyboard.begin();
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < 4; i++){
digitalWrite(OUT[i], HIGH);
for(int e = 0; e < 5; e++){
if(digitalRead(IN[e]) == HIGH)
{
Keyboard.println("" + String(i+2) + " , " + String(e+ 6) + "\n\n");
}
}
digitalWrite(OUT[i], LOW);
delay(300);
}
}