首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按下按钮时,键盘矩阵代码未返回正确的列号

按下按钮时,键盘矩阵代码未返回正确的列号
EN

Stack Overflow用户
提问于 2017-09-15 12:27:17
回答 1查看 58关注 0票数 0
代码语言:javascript
复制
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()中的第一个for循环存在问题。

EN

回答 1

Stack Overflow用户

发布于 2017-09-15 15:43:23

我认为最好是将索引打印到串行监视器上,看看它是否正常工作。试试下面这些代码。

代码语言:javascript
复制
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);
        }
    }
}

如果上面的代码输出了准确的列和行,那么blink for循环就是问题所在。我希望这能帮到你。这是我第一次回答。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46231706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档