2017-09-15 125 views
0
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++ IDE的Arduino列 基於其壓在它應該只是返回所設置的列中的時間返回0-3的隨機值正確的列數當一排讀數很高時,該數值變高。 不知道如何解決相當肯定有一個問題與void loop()中的第一個for循環。鍵盤矩陣代碼不返回當按鈕被按下

回答

0

我認爲最好是將索引打印到串行監視器以查看它是否正常工作。嘗試下面的這些代碼。

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); 
     } 
    } 
} 

如果上面的代碼輸出精確的列和行,那麼閃爍的for-loop是問題。我希望這有幫助。這是我第一次回答。 :)

相關問題