Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save steveatinfincia/22c5a92d4676fbea7d11334fa8aa542d to your computer and use it in GitHub Desktop.
Save steveatinfincia/22c5a92d4676fbea7d11334fa8aa542d to your computer and use it in GitHub Desktop.
/* Complete USB Joystick Example
You must select Joystick from the "Tools > USB Type" menu and edit the Teensy headers to enable
the mega-joystick mode, otherwise some of the axis will not be available
*/
void setup() {
// you can print to the serial monitor while the joystick is active!
Serial.begin(115200);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
}
void loop() {
// read 4 analog inputs and use them for the joystick axis
int stick1 = analogRead(0) * 64;
int stick2 = analogRead(1) * 64;
int stick3 = analogRead(2) * 64;
int stick4 = analogRead(3) * 64;
// pots
int dial1 = analogRead(4) * 64;
int dial2 = analogRead(5) * 64;
// 2 of the 4 switches (flysky ppm output only carries 8 channels)
// 8th is disabled due to odd behavior
int switch1 = analogRead(6) * 64;
//int switch2 = analogRead(7) * 64;
Serial.print(" Stick1: ");
Serial.print(stick1);
Serial.print(" Stick2: ");
Serial.print(stick2);
Serial.print(" Stick3: ");
Serial.print(stick3);
Serial.print(" Stick4: ");
Serial.print(stick4);
Serial.print(" Dial1: ");
Serial.print(dial1);
Serial.print(" Dial2: ");
Serial.print(dial2);
Serial.println("");
Serial.print(" Switch1: ");
Serial.print(switch1);
Serial.print(" Switch2: ");
Serial.print(switch2);
Serial.println("");
Joystick.X(stick1);
Joystick.Y(stick2);
Joystick.Z(stick3);
Joystick.Xrotate(stick4);
Joystick.Yrotate(dial1);
Joystick.Zrotate(dial2);
Joystick.slider(1, switch2);
//Joystick.slider(2, switch1);
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
// a brief delay, so this runs "only" 200 times per second
delay(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment