-3

Can anyone explain to me what is the meaning of this code :

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3

How do they get the number of {9, 8, 7, 6} and {5, 4, 3, 2}. Here is the full code:

/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/
#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]= {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup() {
  Serial.begin(9600);
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process

void loop() {
  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY) {
    Serial.print(keypressed);
  }
}
dda
  • 6,030
  • 2
  • 25
  • 34
Anthony Lauly
  • 319
  • 2
  • 5
  • 17

2 Answers2

0

Everything in the code seems easy to understand. Along with the comment it is clear as a crystal. But as you said, you need an explanation, I will provide an answer :

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

In the above given piece of code, two byte vaiables would be declared named numRows and numCols and initialized each with the value 4.

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3

So here is the code that you are stuck at. Two byte array would be declared named rowPins and colPins each of size 4 (since value of numRows and numCols are 4). Which would range from 0 to 3 (like array in c or java). In this numbers 9,8,7,6 would be assigned to array rowPins and 5,4,3,2 will be assigned to array colPins. Now how or where will these values be. They will be stored in a sequential way from index 0 to index 3. i.e

rowPins[0]=9
rowPins[1]=8
rowPins[2]=7
rowPins[3]=6
colPins[0]=5
colPins[1]=4
colPins[2]=3
colPins[3]=2

This is how they get those numbers.

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31
0

They are not getting the numbers they are assigning it to the arduino pins 2,3,4,5,6,7,8,9

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3

you can also use 4,5,6,7,8,9,10,11 pins too. Just check what pins you are assigning for the row and column inputs and then write the code according to it.