Last active
July 13, 2022 04:22
-
-
Save fordprefect480/ee0a2391f7d408b7e2f049c1762406cd to your computer and use it in GitHub Desktop.
Macro keyboard - arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------- | |
// Key definitions | |
#define BUTTON1 KEY_F13 | |
#define BUTTON2 KEY_F14 | |
#define BUTTON3 KEY_F15 | |
#define BUTTON4 KEY_F16 | |
#define BUTTON5 KEY_F17 | |
#define BUTTON6 KEY_F18 | |
// Pin definitions | |
#define PIN_A0 18 | |
#define PIN_A1 19 | |
#define PIN_A2 20 | |
#define PIN_D9 9 | |
#define PIN_D10 10 | |
#define PIN_D11 11 | |
#define PIN_LED 13 | |
// --------------------------------- | |
#include "Keyboard.h" | |
const uint8_t ledPin = PIN_LED; | |
// Button helper class for handling press/release and debouncing | |
class button { | |
public: | |
const char key; | |
const uint8_t pin; | |
button(uint8_t k, uint8_t p) : key(k), pin(p) {} | |
void press(boolean state) { | |
if (state == pressed || (millis() - lastPressed <= debounceTime)) { | |
return; // Nothing to see here, folks | |
} | |
lastPressed = millis(); | |
if (state) { | |
Keyboard.press(key); | |
//Serial.print("Pressed "); | |
//Serial.print(pin); | |
//Serial.println(); | |
} | |
else { | |
Keyboard.release(key); | |
//Serial.print("Released "); | |
//Serial.print(pin); | |
//Serial.println(); | |
} | |
pressed = state; | |
} | |
void update() { | |
press(!digitalRead(pin)); | |
} | |
private: | |
const unsigned long debounceTime = 30; | |
unsigned long lastPressed = 0; | |
boolean pressed = 0; | |
} ; | |
// Button objects, organized in array | |
button buttons[] = { | |
{BUTTON1, A0}, | |
{BUTTON2, A1}, | |
{BUTTON3, A2}, | |
{BUTTON4, PIN_D9}, | |
{BUTTON5, PIN_D10}, | |
{BUTTON6, PIN_D11} | |
}; | |
const uint8_t NumButtons = sizeof(buttons) / sizeof(button); | |
void setup() { | |
//Serial.begin(9600); | |
// while the serial stream is not open, do nothing: | |
//while (!Serial) ; | |
// Safety check. Ground pin #1 (RX) to cancel keyboard inputs. | |
pinMode(1, INPUT_PULLUP); | |
if (!digitalRead(1)) { | |
failsafe(); | |
} | |
// Set LEDs Off. Active low. | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, LOW); | |
pinMode(PIN_A0, INPUT_PULLUP); | |
pinMode(PIN_A1, INPUT_PULLUP); | |
pinMode(PIN_A2, INPUT_PULLUP); | |
pinMode(PIN_D9, INPUT_PULLUP); | |
pinMode(PIN_D10, INPUT_PULLUP); | |
pinMode(PIN_D11, INPUT_PULLUP); | |
} | |
void printDebug() { | |
for (int i=0; i<6; i++) | |
{ | |
Serial.print(digitalRead(buttons[i].pin)); | |
} | |
Serial.println(); | |
} | |
void loop() { | |
buttons[0].update(); | |
buttons[1].update(); | |
buttons[2].update(); | |
buttons[3].update(); | |
buttons[4].update(); | |
buttons[5].update(); | |
//printDebug(); | |
//delay(100); | |
} | |
void failsafe() { | |
for (;;) {} // Just going to hang out here for awhile :D | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment