Last active
February 23, 2018 23:09
-
-
Save hhayley/8abe088b2c864ee874211a106e030b1f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
keypad library | |
Example-Keypad-multikey | |
Editing for the Sparkfun 1x4 silicone button pad | |
*/ | |
#include <Keypad.h> | |
const byte ROWS = 1; //four rows | |
const byte COLS = 4; //three columns | |
char keys[ROWS][COLS] = { | |
{'1','2','3','4'} | |
}; | |
byte rowPins[ROWS] = {13}; //connect to the row pinouts of the kpd | |
byte colPins[COLS] = {8,9,10,11}; //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