MultiKey.ino (2104B)
1 /* @file MultiKey.ino 2 || @version 1.0 3 || @author Mark Stanley 4 || @contact mstanley@technologist.com 5 || 6 || @description 7 || | The latest version, 3.0, of the keypad library supports up to 10 8 || | active keys all being pressed at the same time. This sketch is an 9 || | example of how you can get multiple key presses from a keypad or 10 || | keyboard. 11 || # 12 */ 13 14 #include <Keypad.h> 15 16 const byte ROWS = 4; //four rows 17 const byte COLS = 3; //three columns 18 char keys[ROWS][COLS] = { 19 {'1','2','3'}, 20 {'4','5','6'}, 21 {'7','8','9'}, 22 {'*','0','#'} 23 }; 24 byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the kpd 25 byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the kpd 26 27 Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 28 29 unsigned long loopCount; 30 unsigned long startTime; 31 String msg; 32 33 34 void setup() { 35 Serial.begin(9600); 36 loopCount = 0; 37 startTime = millis(); 38 msg = ""; 39 } 40 41 42 void loop() { 43 loopCount++; 44 if ( (millis()-startTime)>5000 ) { 45 Serial.print("Average loops per second = "); 46 Serial.println(loopCount/5); 47 startTime = millis(); 48 loopCount = 0; 49 } 50 51 // Fills kpd.key[ ] array with up-to 10 active keys. 52 // Returns true if there are ANY active keys. 53 if (kpd.getKeys()) 54 { 55 for (int i=0; i<LIST_MAX; i++) // Scan the whole key list. 56 { 57 if ( kpd.key[i].stateChanged ) // Only find keys that have changed state. 58 { 59 switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED 60 case PRESSED: 61 msg = " PRESSED."; 62 break; 63 case HOLD: 64 msg = " HOLD."; 65 break; 66 case RELEASED: 67 msg = " RELEASED."; 68 break; 69 case IDLE: 70 msg = " IDLE."; 71 } 72 Serial.print("Key "); 73 Serial.print(kpd.key[i].kchar); 74 Serial.println(msg); 75 } 76 } 77 } 78 } // End loop