#include <Keyboard.h>
static byte rowPin[6]={7,6,5,4,3,2};
static byte colPin[15]={24,26,28,30,32,34,36,38,40,42,44,46,48,50,52};
static byte overrideKey=1;
bool keystate[90],prevKeystate[90],debouncedKeys[90];
unsigned long prevStateChanged[90],stateChanged[90];
unsigned char row1Layout[15]={'KEY_ESC','KEY_F1','KEY_F2','KEY_F3','KEY_F4','KEY_F5','KEY_F6','KEY_F7','KEY_F8','KEY_F9','KEY_F10','KEY_F11','KEY_F12','KEY_DELETE','KEY_INSERT'};
unsigned char row2Layout[15]={'`','1','2','3','4','5','6','7','8','9','0','-','=','KEY_BACKSPACE','KEY_PAGE_UP'};
unsigned char row3Layout[15]={'KEY_TAB','q','w','e','r','t','y','u','i','o','p','[',']',char(92),'KEY_PAGE_DOWN'};
unsigned char row4Layout[15]={'KEY_CAPS_LOCK','a','s','d','f','h','j','k','l',';',char(39),'KEY_RIGHT_SHIFT','KEY_RETURN','KEY_HOME'};
unsigned char row5Layout[15]={'KEY_LEFT_CTRL','KEY_LEFT_SHIFT','z','x','c','v','b','n','m',',','.','/','KEY_RIGHT_CTRL','KEY_UP_ARROW','KEY_END'};
unsigned char row6Layout[15];
void setup(){
row6Layout[0]='KEY_LEFT_GUI';
row6Layout[2]='KEY_LEFT_ALT';
row6Layout[6]=char(32);
row6Layout[10]='KEY_RIGHT_ALT';
row6Layout[12]='KEY_LEFT_ARROW';
row6Layout[13]='KEY_DOWN_ARROW';
row6Layout[14]='KEY_RIGHT_ARROW';
Keyboard.begin();
for(byte temp=0;temp<6;temp++){
pinMode(rowPinLed[temp],OUTPUT);(rowPin[temp],INPUT);
digitalWrite(rowPinLed[temp],LOW);
}
for(byte temp=0;temp<15;temp++){
pinMode(colPinLed[temp],INPUT);(colPin[temp],INPUT_PULLUP);
}
pinMode(overrideKey,INPUT_PULLUP);
}
void loop(){
unsigned long lastLoop=millis();
scanForKeys();
debounceKeys();
outputKeystrokes();
configureBrightness();
configureBacklight();
outputBacklight();
while(millis()-lastLoop<1){
delayMicroseconds(70);
}
while(digitalRead(overrideKey)==LOW){
delay(100);
}
}
void scanForKeys(){
for(byte temp=0;temp<90;temp++){ //save all the current values to use later
prevStateChanged[temp]=stateChanged[temp];
prevKeystate[temp]=keystate[temp];
}
for(byte rowTemp=0;rowTemp<6;rowTemp++){
pinMode(rowPin[rowTemp],OUTPUT);
digitalWrite(rowPin[rowTemp],LOW); //make row pins work as ground one by one
for(byte colTemp=0;colTemp<15;colTemp++){
keystate[15*rowTemp+colTemp]=!digitalRead(colPin[colTemp]); //read keys that are pressed, one row at a time
if(keystate[15*rowTemp+colTemp]!=prevKeystate[15*rowTemp+colTemp]){ //if the keystate has changed,
stateChanged[15*rowTemp+colTemp]=millis(); //save the current time to debounce the key
}
}
pinMode(rowPin[rowTemp],INPUT); //reset the row pins to their former state
}
keystate[79]=!digitalRead(bnKey); //read the state of the key that is not in the matrix
if(keystate[79]!=prevKeystate[79]){
stateChanged[79]=millis(); //save the current time to debounce the key
}
}
void debounceKeys(){
for(byte temp=0;temp<90;temp++){
if(stateChanged[temp]-prevStateChanged[temp]>50){ //if the keystate is stable,
debouncedKeys[temp]=HIGH; //the key is debounced
}else{ //if not,
debouncedKeys[temp]=LOW; //the key is not debounced
}
}
if(stateChanged[79]-prevStateChanged[79]>75){ //debounce the standalone key
debouncedKeys[79]=HIGH;
}else{
debouncedKeys[79]=LOW;
}
}
void outputKeystrokes(){
for(byte rowTemp=0;rowTemp<6;rowTemp++){
for(byte colTemp=0;colTemp<15;colTemp++){
if(debouncedKeys[15*rowTemp+colTemp]==HIGH){ //if the keystate changed stabily,
if(keystate[15*rowTemp+colTemp]==HIGH){ //check the state. if its pressed, press the key
if(rowTemp==0){
Keyboard.press(row1Layout[colTemp]);
}
if(rowTemp==1){
Keyboard.press(row2Layout[colTemp]);
}
if(rowTemp==2){
Keyboard.press(row3Layout[colTemp]);
}
if(rowTemp==3){
Keyboard.press(row4Layout[colTemp]);
}
if(rowTemp==4){
Keyboard.press(row5Layout[colTemp]);
}
if(rowTemp==5){
Keyboard.press(row6Layout[colTemp]);
}
}else{ //if its released, release the key
if(rowTemp==0){
Keyboard.release(row1Layout[colTemp]);
}
if(rowTemp==1){
Keyboard.release(row2Layout[colTemp]);
}
if(rowTemp==2){
Keyboard.release(row3Layout[colTemp]);
}
if(rowTemp==3){
Keyboard.release(row4Layout[colTemp]);
}
if(rowTemp==4){
Keyboard.release(row5Layout[colTemp]);
}
if(rowTemp==5){
Keyboard.release(row6Layout[colTemp]);
}
}
}
}
}
}
will the code function like a keyboard?
if you see errors, the post about it would greatly be appreciated
thanks in advance!