Components and supplies
Arduino Leonardo
SparkFun Qwiic Keypad - 12 Button
Apps and platforms
Arduino IDE
Project description
Code
code
c_cpp
upload this from Arduino ide to your Leonardo
1// this is a simple code for an emoji keyboard with an 2//arduino leonardo or micro. this cannot be done with an uno 3#include <Keyboard.h> 4// Pins 1-7 of the keypad connected to the Arduino respectively: 5int keypadPins[7] = {2, 3, 4, 5, 6, 7, 8}; 6int keypadStatus; // Used to monitor which buttons are pressed. 7int timeout; // timeout variable used in loop 8 9void setup() 10{ 11 for (int i=0; i<7; i++) 12 { 13 pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs 14 digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high 15 } 16} 17 18void loop() 19{ 20 keypadStatus = getKeypadStatus(); // read which buttons are pressed 21 if (keypadStatus != 0) // If a button is pressed go into here 22 { 23 sendKeyPress(keypadStatus); // send the button over USB 24 timeout = 2000; // top of the repeat delay 25 while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down 26 delayMicroseconds(1); 27 while (getKeypadStatus() == keypadStatus) // while the same button is held down 28 { 29 sendKeyPress(keypadStatus); // continue to send the button over USB 30 delay(50); // 50ms repeat rate 31 } 32 } 33} 34 35/* sendKeyPress(int key): This function sends a single key over USB 36 It requires an int, of which the 12 LSbs are used. Each bit in 37 key represents a single button on the keypad. 38 This function will only send a key press if a single button 39 is being pressed */ 40void sendKeyPress(int key) 41{ 42 switch(key) 43 { 44 case 1: // 0x001 45 Keyboard.write('\U0001F600'); //the emojis can be changed to whatever you want 46 break; 47 case 2: // 0x002 48 Keyboard.write('\U0001F601'); 49 break; 50 case 4: // 0x004 51 Keyboard.write('\U0001F602�'); 52 break; 53 case 8: // 0x008 54 Keyboard.write('\U0001F923'); 55 break; 56 case 16: // 0x010 57 Keyboard.write('\U0001F603'); 58 break; 59 case 32: // 0x020 60 Keyboard.write('\U0001F604'); 61 break; 62 case 64: // 0x040 63 Keyboard.write('\U0001F605'); 64 break; 65 case 128: // 0x080 66 Keyboard.write('\U0001F606'); 67 break; 68 case 256: // 0x100 69 Keyboard.write('\U0001F609'); 70 break; 71 case 512: // 0x200 72 Keyboard.write('\U0001F60A'); 73 break; 74 case 1024: // 0x400 75 Keyboard.write('\U0001F60E'); 76 break; 77 case 2048: // 0x800 78 Keyboard.press(KEY_HOME); 79 Keyboard.write('.'); 80 Keyboard.release(KEY_HOME);// Sends the 'windows period' command 81 break; 82 83 } 84} 85 86 87int getKeypadStatus() 88{ 89 int rowPins[4] = {keypadPins[2], keypadPins[6], keypadPins[5], keypadPins[0]}; // row pins are 2, 7, 6, and 1 of the keypad 90 int columnPins[3] = {keypadPins[1], keypadPins[3], keypadPins[4]}; // column pins are pins 2, 4, and 5 of the keypad 91 int keypadStatus = 0; // this will be what's returned 92 93 /* initialize all pins, inputs w/ pull-ups */ 94 for (int i=0; i<7; i++) 95 { 96 pinMode(keypadPins[i], INPUT); 97 digitalWrite(keypadPins[i], HIGH); 98 } 99 100 for (int row=0; row<4; row++) 101 { // initial for loop to check all 4 rows 102 pinMode(rowPins[row], OUTPUT); // set the row pin as an output 103 digitalWrite(rowPins[row], LOW); // pull the row pins low 104 for (int col=0; col<3; col++) 105 { // embedded for loop to check all 3 columns of each row 106 if (!digitalRead(columnPins[col])) 107 { 108 keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value 109 } 110 } 111 pinMode(rowPins[row], INPUT); // reset the row pin as an input 112 digitalWrite(rowPins[row], HIGH); // pull the row pin high 113 } 114 115 return keypadStatus; 116}
code
c_cpp
upload this from Arduino ide to your Leonardo
1// this is a simple code for an emoji keyboard with an 2//arduino leonardo or micro. this cannot be done with an uno 3#include <Keyboard.h> 4// Pins 1-7 of the keypad connected to the Arduino respectively: 5int keypadPins[7] = {2, 3, 4, 5, 6, 7, 8}; 6int keypadStatus; // Used to monitor which buttons are pressed. 7int timeout; // timeout variable used in loop 8 9void setup() 10{ 11 for (int i=0; i<7; i++) 12 { 13 pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs 14 digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high 15 } 16} 17 18void loop() 19{ 20 keypadStatus = getKeypadStatus(); // read which buttons are pressed 21 if (keypadStatus != 0) // If a button is pressed go into here 22 { 23 sendKeyPress(keypadStatus); // send the button over USB 24 timeout = 2000; // top of the repeat delay 25 while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down 26 delayMicroseconds(1); 27 while (getKeypadStatus() == keypadStatus) // while the same button is held down 28 { 29 sendKeyPress(keypadStatus); // continue to send the button over USB 30 delay(50); // 50ms repeat rate 31 } 32 } 33} 34 35/* sendKeyPress(int key): This function sends a single key over USB 36 It requires an int, of which the 12 LSbs are used. Each bit in 37 key represents a single button on the keypad. 38 This function will only send a key press if a single button 39 is being pressed */ 40void sendKeyPress(int key) 41{ 42 switch(key) 43 { 44 case 1: // 0x001 45 Keyboard.write('\U0001F600'); //the emojis can be changed to whatever you want 46 break; 47 case 2: // 0x002 48 Keyboard.write('\U0001F601'); 49 break; 50 case 4: // 0x004 51 Keyboard.write('\U0001F602�'); 52 break; 53 case 8: // 0x008 54 Keyboard.write('\U0001F923'); 55 break; 56 case 16: // 0x010 57 Keyboard.write('\U0001F603'); 58 break; 59 case 32: // 0x020 60 Keyboard.write('\U0001F604'); 61 break; 62 case 64: // 0x040 63 Keyboard.write('\U0001F605'); 64 break; 65 case 128: // 0x080 66 Keyboard.write('\U0001F606'); 67 break; 68 case 256: // 0x100 69 Keyboard.write('\U0001F609'); 70 break; 71 case 512: // 0x200 72 Keyboard.write('\U0001F60A'); 73 break; 74 case 1024: // 0x400 75 Keyboard.write('\U0001F60E'); 76 break; 77 case 2048: // 0x800 78 Keyboard.press(KEY_HOME); 79 Keyboard.write('.'); 80 Keyboard.release(KEY_HOME);// Sends the 'windows period' command 81 break; 82 83 } 84} 85 86 87int getKeypadStatus() 88{ 89 int rowPins[4] = {keypadPins[2], keypadPins[6], keypadPins[5], keypadPins[0]}; // row pins are 2, 7, 6, and 1 of the keypad 90 int columnPins[3] = {keypadPins[1], keypadPins[3], keypadPins[4]}; // column pins are pins 2, 4, and 5 of the keypad 91 int keypadStatus = 0; // this will be what's returned 92 93 /* initialize all pins, inputs w/ pull-ups */ 94 for (int i=0; i<7; i++) 95 { 96 pinMode(keypadPins[i], INPUT); 97 digitalWrite(keypadPins[i], HIGH); 98 } 99 100 for (int row=0; row<4; row++) 101 { // initial for loop to check all 4 rows 102 pinMode(rowPins[row], OUTPUT); // set the row pin as an output 103 digitalWrite(rowPins[row], LOW); // pull the row pins low 104 for (int col=0; col<3; col++) 105 { // embedded for loop to check all 3 columns of each row 106 if (!digitalRead(columnPins[col])) 107 { 108 keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value 109 } 110 } 111 pinMode(rowPins[row], INPUT); // reset the row pin as an input 112 digitalWrite(rowPins[row], HIGH); // pull the row pin high 113 } 114 115 return keypadStatus; 116}
Downloadable files
schematics
wire keypad pin 1 to d2, pin 2 to d3, pin 3 to d4, pin 4 to d5, pin 5 to d6, pin 6 to d7, and pin 7 to d8.
schematics
Comments
Only logged in users can leave comments