Code is as follows:
#include <arduino.h>
#include <Keyboard.h>
#include <XPLDirect.h> // include file for the X-plane direct interface
XPLDirect Xinterface(&Serial); // create an instance of it
const int buttonPin2 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3; // the number of the pushbutton pin
const int buttonPin4 = 4; // the number of the pushbutton pin
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
const int buttonPin11 = 11;
const int buttonPin12 = 12;
const int buttonPin13 = 13;
int buttonState = 0; // variable for reading the pushbutton status
bool shiftqdoneFlag = false;
bool shiftwdoneFlag = false;
bool shiftedoneFlag = false;
bool shiftrdoneFlag = false;
bool shifttdoneFlag = false;
bool shiftydoneFlag = false;
bool shiftudoneFlag = false;
bool shiftidoneFlag = false;
bool shiftodoneFlag = false;
bool shiftpdoneFlag = false;
bool shiftadoneFlag = false;
bool shiftsdoneFlag = false;
bool shiftddoneFlag = false;
bool shiftfdoneFlag = false;
bool shiftgdoneFlag = false;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
Serial.begin(9600);
Keyboard.begin();
// Serial.begin(XPLDIRECT_BAUDRATE); // start serial interface. Baudrate is specified in the header, dont change
// Xinterface.begin("XPLDirect Command Demo"); // needed for initialization. Send a texual identifier of your device as a parameter.
}
void loop()
{
// Xinterface.xloop(); // needs to run every cycle
// BATTERY SWITCH // FLIGHT READY/POWER OFF & EXIT SWITCH
if (digitalRead (2) == 1 & shiftqdoneFlag == false)
{
shiftqdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Q');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftqdoneFlag = true;
}
if (digitalRead (2) == 0 & shiftqdoneFlag == true)
{
shiftqdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Q');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftqdoneFlag = false;
}
//GENERATOR SWITCH
if (digitalRead (3) == 1 & shiftwdoneFlag == false)
{
shiftwdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('W');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftwdoneFlag = true;
}
if (digitalRead (3) == 0 & shiftwdoneFlag == true)
{
shiftwdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('W');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftwdoneFlag = false;
}
// AUX BATT SWITCH
if (digitalRead (4) == 0 & shiftedoneFlag == false)
{
shiftedoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('E');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftedoneFlag = true;
}
if (digitalRead (4) == 1 & shiftedoneFlag == true)
{
shiftedoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('E');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftedoneFlag = false;
}
// GEN RESET BUTTON
if (digitalRead (5) == 0 & shiftrdoneFlag == false)
{
shiftrdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('R');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftrdoneFlag = true;
}
if (digitalRead (5) == 1 & shiftrdoneFlag == true)
{
shiftrdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('R');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftrdoneFlag = false;
}
// STARTER SWITCH MANUAL (DOUBLE MOMENTARY)
if (digitalRead (6) == 0 & shiftydoneFlag == false)
{
shiftydoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Y');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftydoneFlag = true;
}
if (digitalRead (6) == 1 & shiftydoneFlag == true)
{
shiftydoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Y');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftydoneFlag = false;
}
// STARTER SWITCH AUTO/RESET (DOUBLE MOMENTARY)
if (digitalRead (7) == 0 & shifttdoneFlag == false)
{
shifttdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('T');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shifttdoneFlag = false;
}
// IGNITION SWITCH
if (digitalRead (8) == 1 & shiftudoneFlag == false)
{
shiftudoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('U');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftudoneFlag = true;
}
if (digitalRead (8) == 0 & shiftudoneFlag == true)
{
shiftudoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('U');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftudoneFlag = false;
}
// AUTO FUEL BAL
if (digitalRead (9) == 1 & shiftidoneFlag == false)
{
shiftidoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('I');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftidoneFlag = true;
}
if (digitalRead (9) == 0 & shiftidoneFlag == true)
{
shiftidoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('I');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftidoneFlag = false;
}
// MANUAL FUEL BAL - LEFT (ON OFF ON)
if (digitalRead (10) == 1 & shiftodoneFlag == false)
{
shiftodoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('O');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftodoneFlag = true;
}
if (digitalRead (10) == 0 & shiftodoneFlag == true)
{
shiftodoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('P');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftodoneFlag = false;
}
// MANUAL FUEL BAL - RIGHT (ON OFF ON)
if (digitalRead (11) == 1 & shiftodoneFlag == false)
{
shiftodoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('P');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftpdoneFlag = true;
}
if (digitalRead (11) == 0 & shiftpdoneFlag == true)
{
shiftpdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('O');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftpdoneFlag = false;
}
// AVIONICS MASTER SWITCH
if (digitalRead (12) == 1 & shiftadoneFlag == false)
{
shiftadoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('A');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftadoneFlag = true;
}
if (digitalRead (12) == 0 & shiftadoneFlag == true)
{
shiftadoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('A');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftadoneFlag = false;
}
// BUS TIE SWITCH
if (digitalRead (13) == 1 & shiftsdoneFlag == false)
{
shiftsdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('S');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftsdoneFlag = true;
}
if (digitalRead (13) == 0 & shiftsdoneFlag == true)
{
shiftsdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('S');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftsdoneFlag = false;
}
// BUS TIE SWITCH
if (digitalRead (A0) == 1 & shiftddoneFlag == false)
{
shiftddoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('D');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftddoneFlag = true;
}
if (digitalRead (A0) == 0 & shiftddoneFlag == true)
{
shiftddoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('D');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftddoneFlag = false;
}
// BOOST PUMP SWITCH
if (digitalRead (A1) == 1 & shiftfdoneFlag == false)
{
shiftfdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('F');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftfdoneFlag = true;
}
if (digitalRead (A1) == 0 & shiftfdoneFlag == true)
{
shiftfdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('F');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftfdoneFlag = false;
}
// PMU SWITCH
if (digitalRead (A2) == 1 & shiftgdoneFlag == false)
{
shiftgdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('G');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftgdoneFlag = true;
}
if (digitalRead (A2) == 0 & shiftgdoneFlag == true)
{
shiftgdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('G');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftgdoneFlag = false;
}
}