igendel icon

QuickShot Joystick > Arduino Leonardo > Keyboard Input

igendel | PRO | 02/17/15 10:15:35 PM UTC | 0 ⭐ | 669 👁️ | Never ⏰ | []
C++ |

1.99 KB

|

None

|

0 👍

/

0 👎

// QuickShot Joystick to Keyboard Converter
// For the Arduino Leonardo
// For more information and the previous version, see:
// http://www.idogendel.com/en/archives/290
// by Ido Gendel, 2015. Share and enjoy!
 
// 5 inputs: Up, Down, Left, Right and Fire
#define INPUTS 5
 
// Comment the next line to disable autofire
#define RAPID_FIRE_ON
// Number of milliseconds between auto-fire events
#define RAPID_FIRE_SPACING 100
 
struct tJSInput {
  byte pin;
  boolean state;
  boolean isButton;
  byte key;
  unsigned long lastSendTime;
} JSInput[INPUTS] = {{8,  0, false, 232, 0}, 
                                 {9,  0, false, 226, 0}, 
                                 {10, 0, false, 228, 0}, 
                                 {11, 0, false, 230, 0}, 
                                 {12, 0, true, 229, 0}};
 
void setup() {
  pinMode(13, OUTPUT);
  for (int j = 0; j < INPUTS; j++) {
    pinMode(JSInput[j].pin, INPUT_PULLUP);
    JSInput[j].state = digitalRead(JSInput[j].pin);
  }  // for
 
 // Time for re-programming in case of trouble
 delay(4000);
 digitalWrite(13, HIGH); // "active" Indication
 Keyboard.begin();
} // setup
 
void loop() {
  for (int j = 0; j < INPUTS; j++) {
    if (digitalRead(JSInput[j].pin) != JSInput[j].state) {
 
      JSInput[j].state = !JSInput[j].state;
      if (JSInput[j].state) Keyboard.release(JSInput[j].key);
       else {
          Keyboard.press(JSInput[j].key);
          JSInput[j].lastSendTime = millis();
       } // else   
       
    } 
#if defined RAPID_FIRE_ON
      else 
         // is it a long-pressed button?
         if (JSInput[j].isButton && (!JSInput[j].state) &&
             (millis() - JSInput[j].lastSendTime > RAPID_FIRE_SPACING)) {
 
               // re-press
               Keyboard.release(JSInput[j].key);
               JSInput[j].lastSendTime = millis();
               Keyboard.press(JSInput[j].key);
               
             } // if
#endif             
  } // for
  delay(5);
  
} // loop

Comments