/* Flight Sim Lights & Switches USB controller Uses Teensy Joystick interface (16 or 32 buttons and 6 axis input) You must select Joystick from the "Tools > USB Type" menu Pushbuttons should be connected between the digital pins and ground. Potentiometers should be connected to analog inputs 0 to 5. */ // Configure the number of buttons. Be careful not // to use a pin for both a digital button and analog // axis. The pullup resistor will interfere with // the analog voltage. const int numButtons = 13; // Storage for previous button states byte prevButtons[numButtons]; void setup() { // you can print to the serial monitor while the joystick is active! // Serial.begin(9600); // 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); // Initialize buttons (switches)and previous button states for (int i = 0; i < numButtons; i++) { pinMode(i, INPUT_PULLUP); prevButtons[i] = 0; } // Magnetos will use analog pin A0 pinMode (A0, INPUT); // Magneto } byte allButtons[numButtons]; // Current button (switch) states (when read) int magVal=0; // Current Magneto value int lastMagVal = 0; // Last Magneto value int lastMagButton = 0; // Last Magneto button sent to sim // This routine takes a single on/off switch state (newState) // and sends either the on or off button to the sim // (when joystick buttons sent to sim) // pinNum = Teensy pin number // newState = 1 if switch is on, 0 if switch is off // onOff = 1 to send the appropriate joystick button press, 0 to to relase the joystick void updateSwitchPair (int pinNum, byte newState, int onOff) { // Joystick button number for the On state = (Teensy pin number (0 through 10) x 2 + 1) int buttonNum = pinNum * 2 + 1; if (newState) { Joystick.button(buttonNum, onOff); } else { Joystick.button(buttonNum + 1, onOff); // buttonNum + 1 = joystick button for OFF state } } // Returns the Joystick Button number for the magneto switch position int ignitionButton () { int ignButton = 24; // OFF if (magVal < 225) { ignButton = 28; // START } else if (magVal < 300) { ignButton = 27; // BOTH } else if (magVal < 425) { ignButton = 26; // LEFT } else if (magVal < 770) { ignButton = 25; // RIGHT } else { ignButton = 24; // OFF } return ignButton; } // Update the ignition switch position Joyset button if needed boolean updateIgnition () { // Get current analog value for the switch magVal = analogRead (0); // Not connected if (magVal < 20){ return false; } // Value has not changed significantly if (abs (magVal - lastMagVal) < 50) { return false; } // Get the Joystick button for current ignition switch position int ignButton = ignitionButton (); // Same as last? if (ignButton == lastMagButton) { return false; } // Set the button to pressed Joystick.button (ignButton, 1); // If going from RIGHT to OFF, we have to send OFF twice if (ignButton == 24 && lastMagButton == 25) { Joystick.send_now (); delay (50); Joystick.button (ignButton, 0); Joystick.send_now (); delay (50); Joystick.button (ignButton, 1); } lastMagButton = ignButton; lastMagVal = magVal; return true; } // Main processing loop void loop() { // read analog input for ignition switch boolean anyChange = updateIgnition (); // read digital pins and use them for the buttons for (int i = 0; i < numButtons; i++) { if (digitalRead(i)) { // when a pin reads high, the switch is off // the pullup resistor creates the "on" signal allButtons[i] = 0; } else { // when a pin reads low, the switch is on (connecting to ground). allButtons[i] = 1; } } // check to see if any button changed since last time for (int i = 0; i < numButtons; i++) { if (allButtons[i] != prevButtons[i]) { anyChange = true; // Send the matching button press to the sim updateSwitchPair (i, allButtons[i], 1); } } // If there were any changes, send on state, wait and then send the button relase if (anyChange) { Joystick.send_now (); // This actually send all the current joystick changes to the sim for (int i = 0; i < numButtons; i++) { if (allButtons[i] != prevButtons[i]) { updateSwitchPair (i, allButtons[i], 0); prevButtons[i] = allButtons[i]; } } delay (50); Joystick.send_now (); // Once again send changes to the sim } delay (5); }