Teensy Hardware Joystick & Keyboard emulator

Testing if Teensy can be a Joystick and a Keyboard.. as some game emulators expect joystick commands as default, but require keyboard keys also. Handy in installation environment.

 

/* Basic USB Joystick Example

  GPIO 0 Keyboard - F1
  GPIO 1 Keyboard - F3
  GPIO 2 Keyboard - F5
  GPIO 3 Joystick -X
  GPIO 4 Joystick +X
  GPIO 5 Joystick -Y
  GPIO 6 Joystick +Y
  GPIO 7 Joystick Button 1 FIRE

Set Teensy 3.2 to be Serial/Joystick/Host.
*/

#include <Bounce.h>

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.

Bounce button0 = Bounce(0, 10);  // F1
Bounce button1 = Bounce(1, 10);  // F3 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10);  // F5  which is appropriate for

int ledPin = 13;
const int joystick_X_neg_pin = 3;
const int joystick_X_pos_pin = 4;
const int joystick_Y_neg_pin = 5;
const int joystick_Y_pos_pin = 6;
const int joystick_fire_pin = 7; 

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);  // Teensy++ LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);     

  Serial.begin(9600);
  Serial.println("... Setup ... ");
}

void loop() {
 
  // Update all the buttons for the keyboard.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.

  button0.update(); // F1 Start
  button1.update(); // F3 1/2 Player
  button2.update(); // F5 Restart

// Check each button for "falling" edge.

  if (button0.fallingEdge()) {
    Keyboard.press(KEY_F1); // Button 0 = F1 = Start Game
    delay(10);
    Keyboard.release(KEY_F1);
  }

  if (button1.fallingEdge()) {
    Keyboard.press(KEY_F3); // Button 1 = F3 = 1 Player / 2 Player
    delay(10);
    Keyboard.release(KEY_F3);
  }
 
  if (button2.fallingEdge()) {
    Keyboard.press(KEY_F5); // Button 2 = F5 = Restart Game
    delay(10);
    Keyboard.release(KEY_F5);    
  }

if (digitalRead(joystick_X_neg_pin) == LOW)
    {    
    Joystick.X(0);          // Min X
    } else if (digitalRead(joystick_X_pos_pin) == LOW)
    {
    Joystick.X(1023);          // Max X
    } else   {
    Joystick.X(512);          // X Centred
   }

if (digitalRead(joystick_Y_neg_pin) == LOW)
   {
    Joystick.Y(1023);          // Y Max
  } else if (digitalRead(joystick_Y_pos_pin) == LOW)
    {
    Joystick.Y(0);          // Y Min
  } else {
    Joystick.Y(512);          // Y Centred
  }

if (digitalRead(joystick_fire_pin) == LOW) {
    Joystick.button(1, HIGH);  // Button  1 = FIRE High
    delay(10);
  } else {
    Joystick.button(1, LOW);  // Button  1 = FIRE Low
  }

  delay(50);  // a brief delay
}
Advertisements
This entry was posted in Arduino-Projects, electronics and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s