Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino中的4x4键盘_C++_Arduino_Keypress_Keypad - Fatal编程技术网

C++ Arduino中的4x4键盘

C++ Arduino中的4x4键盘,c++,arduino,keypress,keypad,C++,Arduino,Keypress,Keypad,有人能给我解释一下这个代码的含义吗 byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3 他们如何获得{9,8,7,6}和{5,4,3,2}的数量。以下是完整的代码: /*4x4 Matrix Keypad connected to Arduino This code prints the key pressed on the keypa

有人能给我解释一下这个代码的含义吗

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
他们如何获得
{9,8,7,6}
{5,4,3,2}
的数量。以下是完整的代码:

/*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);
  }
}
/*连接到Arduino的4x4矩阵键盘
此代码将键盘上按下的键打印到串行端口*/
#包括
常量字节numRows=4//键盘上的行数
常量字节numCols=4//键盘上的列数
//keymap根据行和列定义按下的键,就像小键盘上显示的那样
char keymap[numRows][numCols]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*'、'0'、'#'、'D'}
};
//显示键盘与arduino终端连接的代码
字节行pins[numRows]={9,8,7,6}//第0至3行
字节colPins[numCols]={5,4,3,2}//第0列至第3列
//初始化Keypad类的实例
小键盘myKeypad=小键盘(makeyMap(keymap)、行pin、列pin、numrow、numCols);
无效设置(){
Serial.begin(9600);
}
//如果按下该键,该键将存储在“keypressed”变量中
//如果键不等于“NO_key”,则打印出该键
//如果计数=17,则计数将重置回0(这意味着在整个键盘扫描过程中没有按键
void循环(){
char keypressed=myKeypad.getKey();
如果(按下键!=无键){
串行打印(按键);
}
}

代码中的所有内容似乎都很容易理解。除了注释外,它就像水晶一样清晰。但正如您所说,您需要解释,我将提供一个答案:

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
在上述给定的代码段中,两个字节的VAIABLE将被声明为
numRows
numCols
,并用值4初始化

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
这就是您所遇到的代码。两字节数组将被声明为
rowPins
colPins
,每一个大小都是4(因为
numRows
numCols
的值都是4)。范围从0到3(类似于c或java中的数组)。在此数字中,9,8,7,6将被分配给数组
行接点
,5,4,3,2将被分配给数组
列接点
。现在这些值将如何或在哪里。它们将以从索引0到索引3的顺序存储。即

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

这就是他们获得这些数字的方式。

他们没有获得分配给arduino引脚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

您也可以使用4、5、6、7、8、9、10、11引脚。只需检查为行和列输入分配的引脚,然后根据它编写代码。

上面的代码注释有什么不清楚的地方?@AnthonyLauly如果我的帖子有帮助,您认为它应该有助于将来的人接受和投票。