Created
August 3, 2014 21:13
-
-
Save Technicus/002fbf1d3def8d0317e6 to your computer and use it in GitHub Desktop.
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
/* @file MultiKey.ino | |
|| @version 1.0 | |
|| @author Mark Stanley | |
|| @contact mstanley@technologist.com | |
|| | |
|| @description | |
|| | The latest version, 3.0, of the keypad library supports up to 10 | |
|| | active keys all being pressed at the same time. This sketch is an | |
|| | example of how you can get multiple key presses from a keypad or | |
|| | keyboard. | |
|| # | |
*/ | |
#include <Keypad.h> | |
const byte ROWS = 5; //four rows | |
const byte COLS = 3; //three columns | |
char keys[ROWS][COLS] = { | |
{'1','2','3'}, | |
{'4','5','6'}, | |
{'7','8','9'}, | |
{'*','0','#'}, | |
{'A','B','C'}, | |
}; | |
byte rowPins[ROWS] = {5, 6, 7, 8, 9}; //connect to the row pinouts of the kpd | |
byte colPins[COLS] = {2, 3, 4}; //connect to the column pinouts of the kpd | |
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); | |
unsigned long loopCount; | |
unsigned long startTime; | |
String msg; | |
void setup() { | |
Serial.begin(9600); | |
loopCount = 0; | |
startTime = millis(); | |
msg = ""; | |
} | |
void loop() { | |
loopCount++; | |
if ( (millis()-startTime)>5000 ) { | |
Serial.print("Average loops per second = "); | |
Serial.println(loopCount/5); | |
startTime = millis(); | |
loopCount = 0; | |
} | |
// Fills kpd.key[ ] array with up-to 10 active keys. | |
// Returns true if there are ANY active keys. | |
if (kpd.getKeys()) | |
{ | |
for (int i=0; i<LIST_MAX; i++) // Scan the whole key list. | |
{ | |
if ( kpd.key[i].stateChanged ) // Only find keys that have changed state. | |
{ | |
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED | |
case PRESSED: | |
msg = " PRESSED."; | |
break; | |
case HOLD: | |
msg = " HOLD."; | |
break; | |
case RELEASED: | |
msg = " RELEASED."; | |
break; | |
case IDLE: | |
msg = " IDLE."; | |
} | |
Serial.print("Key "); | |
Serial.print(kpd.key[i].kchar); | |
Serial.println(msg); | |
} | |
} | |
} | |
} // End loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment