Part of my current assignment involves reading the input from a 4x4 keypad connected to an Arduino UNO. While this may be done easily using <Keypad.h>, the project is being done with Tinkercad's online simulator, meaning I don't have access to that library. I have to do it the manual way.
This is the code I've got so far:
#include <Wire.h>
#define DIMENSIONS 4
const unsigned long period = 50;
unsigned long prevMillis = 0;
byte modo = 0;
const byte rowsPins[DIMENSIONS] = {11, 10, 9, 8};
const byte columnsPins[DIMENSIONS] = {7, 6, 5, 4};
char keys[DIMENSIONS][DIMENSIONS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void setup()
{
Serial.begin(9600);// debug
for(byte i = 0; i < DIMENSIONS; i++){
pinMode(columnsPins[i], INPUT);
digitalWrite(columnsPins[i], HIGH);
pinMode(rowsPins[i], INPUT_PULLUP);
}
Wire.begin();
}
void loop()
{
switch(modo){
case 0:
Serial.print("Master (2) boot up\n");
modo++;
break;
case 1:
if(millis() >= 1000){
modo++;
}
break;
case 2:
if (millis() - prevMillis > period){
prevMillis = millis();
for(byte c = 0; c < DIMENSIONS; c++){
pinMode(columnsPins[c], OUTPUT);
digitalWrite(columnsPins[c], LOW);
for(byte r = 0; r < DIMENSIONS; r++){
if(digitalRead(columnsPins[r]) == LOW){
Serial.println(keys[r][c]);
}
}
digitalWrite(columnsPins[c], HIGH);
pinMode(columnsPins[c], INPUT);
}
}
break;
default:
break;
}
}
(the "modes" and Wire.h are a different part of the assignment and not related to my issue)
This code should, in theory, read the input from the connected 4x4 keypad and then print on the serial monitor which key was pressed. I've done a fair bit of searching, and other solutions for detecting key presses without the Keypad library aren't super different from what I've done here. However, for some reason, what I wrote is not working properly for me.
On starting the simulation, the serial monitor prints 1, 5, 9, D continuously and repeatedly on a loop. Pressing the keys does sometimes print the appropriate character, but it does not stop the flood of 1s, 5s, 9s and Ds.
I know for a fact that it's not a matter of the pins being misconnected, I've checked for that at least a dozen times by now. It's most certainly a matter of code... but I can't figure out where the problem lies.