grassytrainer

Example Teensy Joystick Code

Sep 25th, 2020
1,200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Basic USB Joystick Example
  2.    Teensy becomes a USB joystick
  3.  
  4.    You must select Joystick from the "Tools > USB Type" menu
  5.  
  6.    Pushbuttons should be connected to digital pins 0 and 1.
  7.    Wire each button between the digital pin and ground.
  8.    Potentiometers should be connected to analog inputs 0 to 1.
  9.  
  10.    This example code is in the public domain.
  11. */
  12.  
  13. void setup() {
  14.   pinMode(20, INPUT_PULLUP);
  15.   pinMode(21, INPUT_PULLUP);
  16. }
  17.  
  18. void loop() {
  19.   // read analog inputs and set X-Y position
  20.   Joystick.X(analogRead(0));
  21.   Joystick.Y(analogRead(1));
  22.  
  23.   // read the digital inputs and set the buttons
  24.   Joystick.button(1, digitalRead(0));
  25.   Joystick.button(2, digitalRead(1));
  26.  
  27.   // a brief delay, so this runs 20 times per second
  28.   delay(50);
  29. }
  30.  
RAW Paste Data