Last active
March 12, 2016 15:26
-
-
Save dav-m85/a8a34eeb7050e60a68ef to your computer and use it in GitHub Desktop.
4x4 keypad with interrupt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino Uno source | |
int keyInterrupt = 2; | |
volatile int row = -1; | |
volatile int col = -1; | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
while (!Serial) {} | |
pinMode(A0, OUTPUT); | |
digitalWrite(A0, LOW); | |
pinMode(A1, OUTPUT); | |
digitalWrite(A1, LOW); | |
pinMode(A2, OUTPUT); | |
digitalWrite(A2, LOW); | |
pinMode(A3, OUTPUT); | |
digitalWrite(A3, LOW); | |
pinMode(A4, INPUT_PULLUP); | |
pinMode(A5, INPUT_PULLUP); | |
pinMode(3, INPUT_PULLUP); | |
pinMode(4, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(keyInterrupt), keyInterruptHandler, LOW); | |
Serial.println("GO"); | |
} | |
void loop() { | |
if (row != -1) { | |
delayMicroseconds(5000); // 5ms | |
asm("SEI"); | |
Serial.print(row); Serial.print(" - "); Serial.println(col); | |
col = -1; | |
row = -1; | |
} | |
} | |
void keyInterruptHandler() { | |
asm("CLI"); | |
// Scan row | |
if (digitalRead(A4)==0) {row = A4;} | |
else if (digitalRead(A5)==0) {row = A5;} | |
else if (digitalRead(3)==0) {row = 3;} | |
else if (digitalRead(4)==0) {row = 4;} | |
// Scan column | |
if (row != -1) { | |
digitalWrite(A0, HIGH); | |
if (col == -1 && digitalRead(row)==1) { | |
col = A0; | |
} | |
digitalWrite(A0, LOW); | |
digitalWrite(A1, HIGH); | |
if (col == -1 && digitalRead(row)==1) { | |
col = A1; | |
} | |
digitalWrite(A1, LOW); | |
digitalWrite(A2, HIGH); | |
if (col == -1 && digitalRead(row)==1) { | |
col = A2; | |
} | |
digitalWrite(A2, LOW); | |
digitalWrite(A3, HIGH); | |
if (col == -1 && digitalRead(row)==1) { | |
col = A3; | |
} | |
digitalWrite(A3, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment