forked from tardate/LittleArduinoProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyMatrixInput.ino
More file actions
50 lines (42 loc) · 1.37 KB
/
Copy pathKeyMatrixInput.ino
File metadata and controls
50 lines (42 loc) · 1.37 KB
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
/*
KeyboardMatrixModule/KeyMatrixInput
Demo multiplexed key input on a simple pushbutton/LED matrix module
For info and circuit diagrams see https://github.com/tardate/LittleArduinoProjects/tree/master/playground/KeyboardMatrixModule/KeyMatrixInput
*/
const int START_L_PIN=2;
const int START_R_PIN=6;
const int MATRIX_SIZE=4;
const int POLL_DELAY=150;
void setup() {
Serial.begin(9600);
// set the L pins as output LOW by default
for(int lpin = START_L_PIN; lpin < START_L_PIN + MATRIX_SIZE; ++lpin) {
pinMode(lpin, OUTPUT);
digitalWrite(lpin, HIGH);
}
// set the R pins as input with pullup (normally high)
for(int rpin = START_R_PIN; rpin < START_R_PIN + MATRIX_SIZE; ++rpin) {
pinMode(rpin, INPUT_PULLUP);
}
Serial.println(F("Scanning for key presses.."));
}
void loop() {
scanKeyMatrix();
delay(POLL_DELAY);
}
void scanKeyMatrix() {
for(int lpin = START_L_PIN; lpin < START_L_PIN + MATRIX_SIZE; ++lpin) {
// pull a set of L keys LOW
digitalWrite(lpin, LOW);
for(int rpin = START_R_PIN; rpin < START_R_PIN + MATRIX_SIZE; ++rpin) {
// see if any corresponding R inputs are LOW
if(!digitalRead(rpin)) {
// key is pressed
int key = (lpin - START_L_PIN) * MATRIX_SIZE + rpin - START_R_PIN + 1;
Serial.print(F("Key pressed: K"));
Serial.println(key);
}
}
digitalWrite(lpin, HIGH);
}
}