SHARE
    TWEET
    ferrybig

    Arduino key matrix example

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

    AltStyle によって変換されたページ (->オリジナル) /