I am new to Arduino. I am learning from Arduino Cookbook. I was using tinkercad.com for running my Arduino code. But it is not working. The Serial monitor should show the key which I have pressed on the keypad. I think there is some syntactical error in the code, I believe the connection is all right. Any suggestion will be appreciated. Please look into this:
const char keymap[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const int rpin[]={9,8,7,6};
const int cpin[]={5,4,3,2};
void setup()
{
Serial.begin(9600);
Serial.println("Program starting");//for debugging purpose and canbe removed later
for(int i=0;i<4;i++)
{
pinMode(rpin[i],INPUT);
digitalWrite(rpin[i],HIGH);
}
for(int j=0;j<4;j++)
{
pinMode(cpin[j],INPUT);
digitalWrite(cpin[j],HIGH);
}
}
void loop()
{
char key=getkey();
if(key!=0)
{
Serial.println(key);
}
}
char getkey()
{
char key=0;
for(int i=0;i<4;i++)
{
digitalWrite(rpin[i],LOW);
for(int j=0;j<4;j++)
{
if (digitalRead(cpin[j])==LOW)
key=keymap[i][j];
}
digitalWrite(rpin[i],HIGH);
}
return key;
}
and the connection pic: