Skip to content

Instantly share code, notes, and snippets.

@di0ib

di0ib/8key.ino Secret

Last active December 7, 2016 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save di0ib/c108dec4ce15d8cc38c2e540d47a7674 to your computer and use it in GitHub Desktop.
Save di0ib/c108dec4ce15d8cc38c2e540d47a7674 to your computer and use it in GitHub Desktop.
//install HID Project from Library Manager
#include "HID-Project.h"
//debounce milliseconds
const int debounce = 10;
//Switch Pins
const byte k[8] = { 21, 20, 19, 18, 2, 3, 4, 5 };
//Switch status
boolean s[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
//M for Media Key, K for Keyboard.
//The commands for consumer (media keys) is different than normal keyboard output.
//This indicates if the switch position is one or the other
const char codetype[8] = { 'K', 'M', 'M', 'M', 'M', 'M', 'M', 'M' };
//Keycodes for Media Keys
const ConsumerKeycode ccode[8] = { MEDIA_VOLUME_DOWN,
MEDIA_VOLUME_DOWN,
MEDIA_VOLUME_MUTE,
MEDIA_VOLUME_UP,
MEDIA_PREVIOUS,
MEDIA_PLAY_PAUSE,
MEDIA_STOP,
MEDIA_NEXT
};
//Keycodes for Regular Keys
const KeyboardKeycode kcode[8] = { KEY_ESC,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8
};
void setup() {
Keyboard.begin();
Consumer.begin();
//setup inputs, turn on pullups
for (int i = 0; i <= 7; i++) {
pinMode(k[i], INPUT);
digitalWrite(k[i], 1);
}
}
void loop() {
CheckKeys();
delay(debounce);
}
void CheckKeys() {
for (int i = 0; i <= 7; i++) {
if (codetype[i] == 'M') {
if (digitalRead(k[i]) == 0) {
if (s[i] == 0) {
Consumer.press((ccode[i]));
s[i] = 1;
}
}
else {
if (s[i] == 1) {
s[i] = 0;
Consumer.release((ccode[i]));
}
}
}
if (codetype[i] == 'K') {
if (digitalRead(k[i]) == 0) {
if (s[i] == 0) {
Keyboard.press((kcode[i]));
s[i] = 1;
}
}
else {
if (s[i] == 1) {
s[i] = 0;
Keyboard.release((kcode[i]));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment