ferrybig

Arduino key matrix example

Feb 6th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1.  
  2.  
  3. int8_t rowpins[] = {16, 15, 14};
  4. int8_t colpins[] = {13, 12, 11, 10, 9, 8};
  5. int8_t rotatoryPins[] = {7, 6, 5, 4};
  6.  
  7. bool btnStatus[6 * 3]; // We have 6(cols) * 3(rows) buttons/switches
  8. int8_t rotatoryStatus[2 * 3]; // We have 2 rotatory buttons in every row (3)
  9.  
  10. void setup() {
  11.     Serial.println(9600);
  12.     // put your setup code here, to run once:
  13.     for(uint8_t row = 0; row < (sizeof(rowpins) / sizeof(rowpins[0])); row++) {
  14.         pinMode(rowpins[row], OUTPUT);
  15.         digitalWrite(rowpins[row], HIGH);
  16.     }
  17.     for(uint8_t col = 0; col < (sizeof(colpins) / sizeof(colpins[0])); col++) {
  18.         pinMode(colpins[col], INPUT_PULLUP);
  19.     }
  20.     for(uint8_t i = 0; i < (sizeof(rotatoryPins) / sizeof(rotatoryPins[0])); i++) {
  21.         pinMode(rotatoryPins[i], INPUT_PULLUP);
  22.     }
  23. }
  24.  
  25. void switchUpdate(uint8_t row, uint8_t col, bool status) {
  26.     Serial.print("The switch/button at col ");
  27.     Serial.print(col);
  28.     Serial.print(" and row ");
  29.     Serial.print(row);
  30.     Serial.print(" was updated to: ");
  31.     Serial.println(status);
  32. }
  33.  
  34. void rotatoryUpdate(uint8_t row, uint8_t col, uint8_t newVal, uint8_t oldVal) {
  35.     Serial.print("The rotatory at col ");
  36.     Serial.print(col);
  37.     Serial.print(" and row ");
  38.     Serial.print(row);
  39.     Serial.print(" was updated to ");
  40.     Serial.print(newVal);
  41.     Serial.print(" from ");
  42.     Serial.println(oldVal);
  43. }
  44.  
  45. void checkInput() {
  46.     for(uint8_t row = 0; row < (sizeof(rowpins) / sizeof(rowpins[0])); row++) {
  47.         digitalWrite(rowpins[row], LOW);
  48.         uint8_t partialIndex = row * (sizeof(colpins) / sizeof(colpins[0]));
  49.  
  50.         // Check normal buttons
  51.         for(uint8_t col = 0; col < (sizeof(colpins) / sizeof(colpins[0])); col++) {
  52.             uint8_t index = partialIndex + col;
  53.             bool status = digitalRead(colpins[col]) == LOW ? true : false;
  54.             bool old_status = btnStatus[index];
  55.  
  56.             if(status != old_status) {
  57.                 switchUpdate(row, col, status);
  58.                 btnStatus[index] = status;
  59.             }
  60.            
  61.         }
  62.         // Check rotatory encoders
  63.        
  64.         for(uint8_t i = 0; i < (sizeof(rotatoryPins) / sizeof(rotatoryPins[0])); i+=2) {
  65.             uint8_t index = partialIndex + (i / 2);
  66.             uint8_t status =
  67.                 (digitalRead(rotatoryPins[i]) == LOW ? 1 : 0) ^
  68.                 (digitalRead(rotatoryPins[i + 1]) == LOW ? 2 : 0);
  69.             bool old_status = rotatoryStatus[index];
  70.            
  71.             if(status != old_status) {
  72.                 rotatoryUpdate(row, i / 2, status, old_status);
  73.                 rotatoryStatus[index] = status;
  74.             }
  75.         }
  76.         digitalWrite(rowpins[row], HIGH); // reset this row
  77.     }
  78. }
  79.  
  80. void loop() {
  81.     // put your main code here, to run repeatedly:
  82.     checkInput();
  83. }
Advertisement
Add Comment
Please, Sign In to add comment