按下按钮时,键盘矩阵代码未返回正确的列号
Keyboard Matrix code not returning correct column number when a button is pressed
int ColumnPins = 4; //How many column pins
int RowPins = 5; //How many row pins
int Columns[] = {0, 1, 2, 3}; //Column pinNumbers
int Rows[] = {4, 6, 7, 8, 9}; //Row pinNumbers
void setup()
{
pinMode(13, OUTPUT);
for (int counter = 0; counter < ColumnPins; counter++)
{ //set all the columns to output
pinMode(Columns[counter], OUTPUT);
}
for (int counter = 0; counter < RowPins; counter++)
{ //set all rows to input
pinMode(Rows[counter],INPUT_PULLDOWN);
}
}
void loop()
{
for (int ColumnIndex = 0; ColumnIndex < ColumnPins; ColumnIndex++) //sets column pins to high 1 by 1
{
digitalWrite(Columns[ColumnIndex], HIGH);
RowScan(ColumnIndex); //calls function rowscan checks whether and row is HIGH
digitalWrite(Columns[ColumnIndex], LOW);
}
}
void RowScan(int ColumnIndex) //Scans each rowpin and checks whether any reads high
{
for (int RowIndex = 0; RowIndex < RowPins; RowIndex++)
{
if (digitalRead(Rows[RowIndex]) == HIGH) //if Rows at rowindex is high
{
for (int blinks = 0; blinks <= RowIndex; blinks++)
{ //Outputs # of blinks based on which row HIGH was read
// print index
digitalWrite(13, HIGH); //LEDPIN high
delay(250);
digitalWrite(13, LOW);
delay(500);
}
for (int blinks = 0; blinks <= ColumnIndex; blinks++)
{ //Outputs # of blinks based on which row HIGH was read
// print index
digitalWrite(13, HIGH); //LEDPIN high
delay(100);
digitalWrite(13, LOW);
delay(100);
}
}
else if (digitalRead(Rows[RowIndex]) == LOW) //Rows at index o is LOW
{
digitalWrite(13,LOW);
}
}
}
C Arduino IDE列是根据按下按时间返回0-3的随机值,而在该行读取较高时,它应该返回设置为高的列。不确定如何确定可以很好地确定void loop中的第一个循环()。
我认为,如果我们将索引打印到串行显示器以查看是否有效,那是最好的。在下面尝试这些代码。
int totalCols = 4;
int totalRows = 5;
// exclude pins 0 and 1 since these are used for Serial communication
int keyCols[] = { 2, 4, 5, 6 };
int keyRows[] = { 7, 8, 9, 10, 11 };
void setup()
{
Serial.begin(9600); // begin Serial Communication at 9600 baudrate
pinMode(13, OUTPUT);
for (int i = 0; i < totalCols; i++)
pinMode(keyCols[i], OUTPUT);
for (int i = 0; i < totalRows; i++)
pinMode(keyRows[i], INPUT); // you can use INPUT instead
}
void loop() {
for (int i = 0; i < totalCols; i++) {
digitalWrite(keyCols[i], HIGH);
ScanActiveRows(i);
digitalWrite(keyCols[i], LOW);
}
}
void ScanActiveRows(int col) {
delay(5); // wait for 5 milliseconds after setting column pin
for (int row = 0; row < totalRows; row++) {
if (digitalRead(keyRows[row]) == HIGH) {
// print column and row indices
Serial.print("Column : ");
Serial.println(col);
Serial.print("Row : ");
Serial.println(row);
}
else if (digitalRead(keyRows[row]) == LOW) {
digitalWrite(13,LOW);
}
}
}
如果以上代码输出精确的列和行,则眨眼前是问题。我希望这有帮助。这是我第一次回答。:)
相关文章:
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 为什么C++程序在太大时返回代码而不是答案?
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- NMAKE:致命错误 U1077:cl.exe 和 nmake.exe 返回代码'0x2'
- DoModal 主应用程序随机返回 -1 代码
- C++线程中调用 std::terminate 时程序返回代码
- IMMDevice::激活缺少接口的返回代码
- 退出应用并在 MFC 中返回代码
- 什么是线程锁返回代码值
- 引发异常与返回代码
- 为什么我的shell只检测0-255之间的返回代码
- waitpid/wexitstatus 返回 0 而不是正确的返回代码
- linux下的Boost Semaphores和EINTR返回代码
- 从std::thread获取返回代码
- 从本地类的 C++ 析构函数访问返回代码
- DLL 注册返回代码0x80029c4a
- 定义D3DERR_INVALIDCALL(D3D11CreateDevice的返回代码)的位置
- Windows 错误消息返回代码而不是消息
- 如何在DdeCallback函数中实现返回代码