├── README.md ├── cncKeyboard.ino └── cncKeyboardTemplate.ino /README.md: -------------------------------------------------------------------------------- 1 | # arduino-keyboard 2 | This is the code mentioned and shocased in this video: https://youtu.be/Iq3oY91x9Vk 3 | Please refer to it if you have any questions. 4 | -------------------------------------------------------------------------------- /cncKeyboard.ino: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------- 2 | * This code was written by David Wieland aka. Datulab Tech 3 | * https://www.youtube.com/datulabtech to work with the 4 | * custom PCBs showcased in this video: https://youtu.be/eH8FnLNZwlk 5 | * A more detailed explanation can be found here: https://youtu.be/Iq3oY91x9Vk 6 | * Feel free to use it for your projects, just know that you will have to 7 | * change some things for it to work with your hardware. 8 | * ------------------------------------------------------------------------- */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | ClickEncoder *encoder; 16 | int16_t encoderLast, encoderValue; 17 | 18 | void timerIsr() { 19 | encoder->service(); 20 | } 21 | 22 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, 10, NEO_GRB + NEO_KHZ800); 23 | 24 | int longPressDelay = 350; //customizable encoderValues 25 | int spamSpeed = 15; 26 | 27 | byte inputs[] = {6,7,8,9}; //declaring inputs and outputs 28 | const int inCount = sizeof(inputs)/sizeof(inputs[0]); 29 | byte outputs[] = {2,3,4,5}; 30 | const int outCount = sizeof(outputs)/sizeof(outputs[0]); 31 | 32 | char layout[4][4] = { //layout grid for characters 33 | {'l',']','=','/'}, 34 | {'7','8','9','*'}, 35 | {'4','5','6','-'}, 36 | {'1','2','3','+'}, 37 | }; 38 | 39 | char lEncoderChar = 'l'; //characters to be sent if encoder is manipulated 40 | char rEncoderChar = 'r'; 41 | char pushEncoderChar = 'p'; 42 | char doubleEncoderChar = 'd'; 43 | 44 | int keyDown[4][4]; 45 | bool keyLong[4][4]; 46 | 47 | void setup(){ 48 | 49 | for(int i=0; i spamSpeed){ //if the key has been held long enough to warrant another keystroke set 111 | Keyboard.write(layout[row][col]); 112 | keyDown[row][col] = 1; 113 | } 114 | else if(keyDown[row][col] > longPressDelay){ //if the key has been held for longer that longPressDelay, it switches into spam mode 115 | keyLong[row][col] = true; 116 | } 117 | 118 | keyDown[row][col]++; 119 | } 120 | 121 | void resetKey(int row, int col){ //resetting the variables after key is released 122 | keyDown[row][col] = 0; 123 | keyLong[row][col] = false; 124 | } 125 | 126 | //Function to ckeck if the encoder has been used, if yes, it sends apporpriate keys and gives serial output 127 | void checkEncoder(){ 128 | encoderValue = encoder->getValue(); 129 | 130 | if (encoderValue != encoderLast) { 131 | encoderLast = encoderValue; 132 | if (encoderValue == 1){ 133 | Serial.print("Encoder Value: "); 134 | Serial.println(encoderValue); 135 | Keyboard.write(lEncoderChar); 136 | } 137 | else if (encoderValue == -1){ 138 | Serial.print("Encoder Value: "); 139 | Serial.println(encoderValue); 140 | Keyboard.write(rEncoderChar); 141 | } 142 | } 143 | 144 | ClickEncoder::Button b = encoder->getButton(); 145 | if (b != ClickEncoder::Open) { 146 | Serial.print("Button: "); 147 | #define VERBOSECASE(label) case label: Serial.println(#label); break; 148 | switch (b) { 149 | VERBOSECASE(ClickEncoder::Pressed); 150 | VERBOSECASE(ClickEncoder::Held) 151 | VERBOSECASE(ClickEncoder::Released) 152 | case ClickEncoder::Clicked: 153 | Serial.println("ClickEncoder::Clicked"); 154 | Keyboard.write(pushEncoderChar); 155 | break; 156 | case ClickEncoder::DoubleClicked: 157 | Serial.println("ClickEncoder::Clicked"); 158 | Keyboard.write(doubleEncoderChar); 159 | break; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /cncKeyboardTemplate.ino: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------- 2 | * This code was written by David Wieland aka. Datulab Tech 3 | * https://www.youtube.com/datulabtech to work with the 4 | * custom PCBs showcased in this video: https://youtu.be/eH8FnLNZwlk 5 | * A more detailed explanation can be found here: https://youtu.be/Iq3oY91x9Vk 6 | * Feel free to use it for your projects, just know that you will have to 7 | * change some things for it to work with your hardware. 8 | * ------------------------------------------------------------------------- */ 9 | 10 | #include 11 | #include 12 | 13 | #define ledCount 5 //how many LEDs are there 14 | #define ledPin 10 //which pin are they connected to 15 | 16 | int longPressDelay = 350; //customizable keyboard values 17 | int spamSpeed = 15; 18 | 19 | byte inputs[] = {6,7,8,9}; //declaring inputs and outputs 20 | #define inCount sizeof(inputs)/sizeof(inputs[0]) 21 | byte outputs[] = {2,3,4,5}; 22 | #define outCount sizeof(outputs)/sizeof(outputs[0]) 23 | 24 | char layout[outCount][inCount] = { //layout grid for characters (adjust if your grid is not 4x4) 25 | {'l',']','=','/'}, 26 | {'7','8','9','*'}, 27 | {'4','5','6','-'}, 28 | {'1','2','3','+'}, 29 | }; 30 | 31 | int keyDown[outCount][inCount]; 32 | bool keyLong[outCount][inCount]; 33 | 34 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledCount, ledPin, NEO_GRB + NEO_KHZ800); 35 | 36 | void setup(){ 37 | 38 | for(int i=0; i spamSpeed){ //if the key has been held long enough to warrant another keystroke set 101 | Keyboard.write(layout[row][col]); 102 | keyDown[row][col] = 1; 103 | } 104 | else if(keyDown[row][col] > longPressDelay){ //if the key has been held for longer that longPressDelay, it switches into spam mode 105 | keyLong[row][col] = true; 106 | } 107 | 108 | keyDown[row][col]++; 109 | } 110 | 111 | void resetKey(int row, int col){ //resetting the variables after key is released 112 | keyDown[row][col] = 0; 113 | keyLong[row][col] = false; 114 | } 115 | --------------------------------------------------------------------------------