Skip to content

Instantly share code, notes, and snippets.

@kumekay
Created February 23, 2016 10:26
Show Gist options
  • Save kumekay/910876b5564364668a26 to your computer and use it in GitHub Desktop.
Save kumekay/910876b5564364668a26 to your computer and use it in GitHub Desktop.
Arduino micro sketch: using joystick as keyboard with arrows and enter
/*
Written by Sergei Silnov @kumekay [email protected]
Based on:
https://github.com/NicoHood/HID/wiki/Keyboard-API#boot-keyboard
*/
#include "HID-Project.h"
const int pinLed = LED_BUILTIN;
const int pinButton = A3;
const int pinY = A2;
const int pinX = A1;
bool action = false;
void setup() {
pinMode(pinLed, OUTPUT);
pinMode(pinButton, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
BootKeyboard.begin();
}
void loop() {
// Light led if keyboard uses the boot protocol (normally while in bios)
// Keep in mind that on a 16u2 and Arduino Micro HIGH and LOW for TX/RX Leds
// are inverted.
if (BootKeyboard.getProtocol() == HID_BOOT_PROTOCOL)
digitalWrite(pinLed, HIGH);
else
digitalWrite(pinLed, LOW);
int valueY = analogRead(pinY);
int valueX = analogRead(pinX);
int button = digitalRead(pinButton);
// Send actions
if (!button) {
BootKeyboard.write(KEY_ENTER);
action = true;
}
if (valueX > 1000 ) {
BootKeyboard.write(KEY_RIGHT);
action = true;
}
if (valueX < 24 ) {
BootKeyboard.write(KEY_LEFT);
action = true;
}
if (valueY > 1000 ) {
BootKeyboard.write(KEY_UP);
action = true;
}
if (valueY < 24 ) {
BootKeyboard.write(KEY_DOWN);
action = true;
}
if (action) {
// Simple debounce
delay(300);
action = false;
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment