how to set the code of a 4x4 keypad matrix to make it to a 3x3 keypad matrix.

#include <Keypad.h>
const byte ROWS = 3; //three rows
const byte COLS = 3; // three columns
const char key2[ROWS2][COLS2] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
byte rowPins[ROWS2] = {9,10,11};
byte colPins[COLS2] = {13,14,15};
Keypad keypad = Keypad( makeKeymap(key), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(115200); // set up serial monitor 115 kbps

}

void loop() {
char pressed = keypad.getKey();
if(pressed)
Serial.println(pressed);

The code posted does not compile

[#include <Keypad.h>
 
const byte ROWS2 = 3;    //three rows 
const byte COLS2 = 3;   // three columns
const char key[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'}
};
byte rowPins2[ROWS] = {9,10,11};
byte colPins[COLS] = {13,14,15};
Keypad keypad = Keypad( makeKeymap(key), rowPins, colPins, ROWS, COLS);
void setup() {
  Serial.begin(115200);      // set up serial monitor 115 kbps
}

void loop() {
 char pressed = keypad.getKey();
  if(pressed)
    Serial.println(pressed);
}

It should compile now

It still does not compile

It compiles and it works on my Arduino uno.

Doesn't compile for me.

There is no keypad instance declared.

keypad2 is fine however.

It's possible that we have different variants of the keypad library of course. Can you confirm again that what you posted does compile for you?

Can you confirm again that what you posted does compile for you?

The fact that the posted code starts with a [ precludes that even leaving aside problems with the keypad

I think you might be playing Mr. P. E. Dantic a bit too closely there. It's just a posting cockup, but even after taking out the obvious errors, it's still broken on my machine at least.

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; // four columns
const char key[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {0,8,2,3};
byte colPins[COLS] = {4,5,6,7};
Keypad keypad = Keypad( makeKeymap(key), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(115200);

}
void loop() {
char pressed = keypad.getKey();
if(pressed)
Serial.println(pressed);

This is what I have for my 3x3 keypad matrix code.

[
#include <Keypad.h>
const byte ROWS2 = 3;    //three rows 
const byte COLS2 = 3;   // three columns
const char key[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'}
};
byte rowPins[ROWS] = {9,10,11};
byte colPins[COLS] = {13,14,15};
Keypad keypad = Keypad( makeKeymap(key), rowPins, colPins, ROWS, COLS );
void setup() {
  Serial.begin(115200);

}
void loop() {
  char pressed = keypad.getKey();
  if(pressed)
    Serial.println(pressed);
}]

UKHeliBob:
The fact that the posted code starts with a [ precludes that even leaving aside problems with the keypad

Ok, your way is best.

Doesn't compile, again!

annchairez

Look very closely at the code that you have posted. Why has it got a [ at the start and a ] at the end ? They are surely not in the code when you edit it in the IDE on your PC

However, even with those removed it does not compile because of multiple errors, mostly missing variable declarations

why would you want to replace the 2d array? what are you trying to achieve? if you are looking to break down coordinates to something else there should be quick math for this