Advertisement
igendel

QuickShot Joystick > Arduino Leonardo > Keyboard Input

Feb 17th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. // QuickShot Joystick to Keyboard Converter
  2. // For the Arduino Leonardo
  3. // For more information and the previous version, see:
  4. // http://www.idogendel.com/en/archives/290
  5. // by Ido Gendel, 2015. Share and enjoy!
  6.  
  7. // 5 inputs: Up, Down, Left, Right and Fire
  8. #define INPUTS 5
  9.  
  10. // Comment the next line to disable autofire
  11. #define RAPID_FIRE_ON
  12. // Number of milliseconds between auto-fire events
  13. #define RAPID_FIRE_SPACING 100
  14.  
  15. struct tJSInput {
  16.   byte pin;
  17.   boolean state;
  18.   boolean isButton;
  19.   byte key;
  20.   unsigned long lastSendTime;
  21. } JSInput[INPUTS] = {{8,  0, false, 232, 0},
  22.                                  {9,  0, false, 226, 0},
  23.                                  {10, 0, false, 228, 0},
  24.                                  {11, 0, false, 230, 0},
  25.                                  {12, 0, true, 229, 0}};
  26.  
  27. void setup() {
  28.   pinMode(13, OUTPUT);
  29.   for (int j = 0; j < INPUTS; j++) {
  30.     pinMode(JSInput[j].pin, INPUT_PULLUP);
  31.     JSInput[j].state = digitalRead(JSInput[j].pin);
  32.   }  // for
  33.  
  34.  // Time for re-programming in case of trouble
  35.  delay(4000);
  36.  digitalWrite(13, HIGH); // "active" Indication
  37.  Keyboard.begin();
  38. } // setup
  39.  
  40. void loop() {
  41.   for (int j = 0; j < INPUTS; j++) {
  42.     if (digitalRead(JSInput[j].pin) != JSInput[j].state) {
  43.  
  44.       JSInput[j].state = !JSInput[j].state;
  45.       if (JSInput[j].state) Keyboard.release(JSInput[j].key);
  46.        else {
  47.           Keyboard.press(JSInput[j].key);
  48.           JSInput[j].lastSendTime = millis();
  49.        } // else  
  50.        
  51.     }
  52. #if defined RAPID_FIRE_ON
  53.       else
  54.          // is it a long-pressed button?
  55.          if (JSInput[j].isButton && (!JSInput[j].state) &&
  56.              (millis() - JSInput[j].lastSendTime > RAPID_FIRE_SPACING)) {
  57.  
  58.                // re-press
  59.                Keyboard.release(JSInput[j].key);
  60.                JSInput[j].lastSendTime = millis();
  61.                Keyboard.press(JSInput[j].key);
  62.                
  63.              } // if
  64. #endif            
  65.   } // for
  66.   delay(5);
  67.  
  68. } // loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement