0

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:

enter image description here

update 1: enter image description here

16
  • 2
    Not working doesn't give us any information. What is not working? How is not working? Is it hardware or software? What is the right result in your opinion?
    – user31481
    Dec 16, 2017 at 19:27
  • The serial monitor is blank. Dec 16, 2017 at 19:28
  • The serial monitor should provide the key pressed on it. Dec 16, 2017 at 19:29
  • I think so software problem. Dec 16, 2017 at 19:29
  • 4
    Check pin modes. Should be INPUT_PULLUP and OUTPUT. Also consider debouncing. Dec 16, 2017 at 20:27

1 Answer 1

4

Cited comment:

Check pin modes. Should be INPUT_PULLUP and OUTPUT. Also, consider debouncing. – Mikael Patel

Please, try:

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++)
  {
//Should be INPUT_PULLUP and OUTPUT
    pinMode(rpin[i],OUTPUT);
    pinMode(cpin[i],INPUT_PULLUP);
  }
}
void loop()
{
  char key=getkey();
  if(key!=0)
  {
   Serial.println(key);
   delay(200);              // Also, consider debouncing. 
  }  
}
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;
}
4
  • I tested this code and it works. And the debouncing time can be lowered to something like 10.
    – gre_gor
    Dec 16, 2017 at 23:11
  • @gre_gor , for what purpose do you reduce to 10?
    – AltAir
    Dec 16, 2017 at 23:16
  • So it's faster to respond.
    – gre_gor
    Dec 16, 2017 at 23:17
  • @gre_gor , to manually control the buttons, a 0.2-second delay is acceptable. And 0.01-second delay may not be enough.
    – AltAir
    Dec 16, 2017 at 23:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.