summaryrefslogtreecommitdiff
path: root/keyboard-1/keyboard-1.ino
blob: 5b76bdd79a6b5aa4194e0caa87d6e573ff2d4f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const int rows[] = {4, 5};
const int kRows = 2;

const int columns[] = {6, 7};
const int kColumns = 2;

bool buttons[] = {false, false, false, false};

void setup() {
  for (int i = 0; i < kRows; i++) {
    pinMode(rows[i], INPUT_PULLUP);
  }
  for (int i = 0; i < kColumns; i++) {
    pinMode(columns[i], INPUT);
  }
  Serial.begin(9600);
}

bool scanKey(int row, int column) {
  pinMode(columns[column], OUTPUT);
  digitalWrite(columns[column], LOW);
  bool pressed = digitalRead(rows[row]) == LOW;
  pinMode(columns[column], INPUT);
  return pressed;
}

void loop() {
//  pinMode(4, INPUT_PULLUP);
//  if (digitalRead(4) == LOW) {
//    Serial.print("pushed: ");
//    Serial.println(columns[0]);
//  } else {
//    Serial.println("not pushed");
//  }
  
  for (int i = 0; i < kRows; i++) {
    for (int j = 0; j < kColumns; j++) {
      bool pressed = scanKey(i, j);
      if (pressed != buttons[i * kColumns + j]) {
        if (pressed) {
          Serial.print("Button pressed: ");
        } else {
          Serial.print("Button released: ");
        }
        Serial.println(i * kColumns + j);
        buttons[i * kColumns + j] = pressed;
      }
    }
  }
}