[SOLVED] Programming a matrix 3x4 keypad

Problem solved
I checked the keypad. And I found out that there was a little bit of soldering on the circuitboard. I removed it and it works. It was a stupid mistake of mine which I’m not going to make anymore! I appreciate the help that I got of all of you!

Thank you

Hi people,

I’m trying to program a 3x4 matrix keypad but it doesn’t work.

I tried this code example:

void loop()
{

   char keypressed = kpd.getKey();

   if (keypressed != NO_KEY)
   {
      Serial.println(keypressed);
   }

}

And that worked => Each key is printed, but when I want to check each key with a switch it won’t work => when I press a key it’s not printed.

   char keypressed = keypad.getKey();
  if (keypressed != NO_KEY)
  {

    switch (keypressed)
    {
      case '*':
        tone(soundPin, 440, 100);
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        Serial.println(keypressed);
        break;
      case '#':
        tone(soundPin, 440, 100);
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        Serial.println(keypressed);
        break;
      default:
        Serial.println(keypressed);
    }
  }

This is the entire sketch

#include <Keypad.h>

// ARDUINO PIN - PRINTPLAAT PIN
// 2 - 10 (rij1)
// 4 - 9  (rij2)
// 5 - 8  (rij3)
// 6 - 7  (rij4)

// 8 -  14  (col1)
// 9 -  13  (col2)
// 10 - 12  (col3)

const byte ROWS = 4; //vier rijen
const byte COLS = 3; //drie kolommen
//kolommen en rijen specifieren
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

//byte rowPins[ROWS] = {6, 5, 4, 2};
byte rowPins[ROWS] = {2, 4, 5, 6}; //pinnen verbinden van de rijen van rij 1 => 4
byte colPins[COLS] = {8, 9, 10}; //pinnen verbinden van de kolommen van kolom 1 => 3
//byte colPins[COLS] = {10, 9, 8};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //object maken van keybad

#define ledPin 13
#define soundPin 7
#define pirPin 3

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  digitalWrite(ledPin, LOW);
  pinMode(ledPin, OUTPUT);
  digitalWrite(soundPin, LOW);
  pinMode(soundPin, OUTPUT);
  pinMode(pirPin, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  char keypressed = keypad.getKey();
  if (keypressed != NO_KEY)
  {

    switch (keypressed)
    {
      case '*':
        tone(soundPin, 440, 100);
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        Serial.println(keypressed);
        break;
      case '#':
        tone(soundPin, 440, 100);
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        Serial.println(keypressed);
        break;
      default:
        Serial.println(keypressed);
    }
  }
}

If someone can help me with this that would be awesome.
Thank you in advance :slight_smile:

I'm trying to program a 3x4 matrix keypad but it doesn't work.

I'm not surprised. I've never seen a programmable keypad.

Arduinos are programmable. Keypads are not.

but when I want to check each key with a switch it won't work.

The snippet you posted won't even compile, so it's silly to talk about it working, or not.

Please post your entire sketch.

aarg: Please post your entire sketch.

And a good description of what you mean by "doesn't work".

aarg: Please post your entire sketch.

I posted the entire sketch.

Delta_G: And a good description of what you mean by "doesn't work".

void loop() {
  // put your main code here, to run repeatedly:
  char keypressed = keypad.getKey();
  if (keypressed != NO_KEY)
  {
    Serial.print("keypressed: "); Serial.println(keypressed); // Why not learn what key was actually pressed?
    switch (keypressed)
    {

PaulS: void loop() {   // put your main code here, to run repeatedly:   char keypressed = keypad.getKey();   if (keypressed != NO_KEY)   {     Serial.print("keypressed: "); Serial.println(keypressed); // Why not learn what key was actually pressed?     switch (keypressed)     {

Yes I can do that but the switch is not working.

Are my posts not showing up?

What do you mean by not working? What is it doing or not doing? Can you describe the problem?

When you put in the Serial prints as Paul suggests, what do you see?

Hello Cynni,

It looks like a really simple sketch for testing the keypad. I don't see anything wrong with it. Is this your first time using the Arduino software? If so then look at the bottom of the screen for any errors when you try to 'verify' your sketch. If you see any warnings or errors can you tell us what they are?

But like I said, your sketch should be working as far as I can tell.

Delta_G: Are my posts not showing up?

What do you mean by not working? What is it doing or not doing? Can you describe the problem?

yes I'm sorry but there was a problem with the internet connection. Well when I try this code snippet:

I get the value of the key that is pressed. If I press * I get * printed out.

void loop()
{

   char keypressed = kpd.getKey();

   if (keypressed != NO_KEY)
   {
      Serial.println(keypressed);
   }

}

But when I use the switch case nothing happens or sometimes it says that button 2 is pressed while I'm doing nothing.

And there are no errors showing up.

mstanley: Hello Cynni,

It looks like a really simple sketch for testing the keypad. I don't see anything wrong with it. Is this your first time using the Arduino software? If so then look at the bottom of the screen for any errors when you try to 'verify' your sketch. If you see any warnings or errors can you tell us what they are?

But like I said, your sketch should be working as far as I can tell.

Hi,

It's not my first time but I'm a beginner. And I don't get any errors that is why I don't get what the problem is.

Delta_G: When you put in the Serial prints as Paul suggests, what do you see?

I get key pressed: 2 2

But I'm not pressing any key...

But I'm not pressing any key...

Then your keypad is wired incorrectly.

PaulS: Then your keypad is wired incorrectly.

But it's not always that I get this printed out. It worked before and now not anymore.

You might want to look at Keybad.h and make sure it sets the inputs to have pullups. If not, you may need to add pullup resistors. Dwight

PaulS: Then your keypad is wired incorrectly.

I checked the keypad. And I found out that there was a little bit of soldering on the circuitboard. I removed it and it works. It was a stupid mistake of mine which I'm not going make anymore!