Hi all,
I thank you in advance for any help you can give me on this project.
At the moment the Com and Nav buttons haven't been programmed. For the rest, they all function individually, until the Battery and Alt switches are on, then only the fuel pump, Avionics, gear lever and encoders work. There seems to be something locking out the code for the other switches once they are on.
I have gone back bit by bit but come up with the same issue each time.
I am really new to this coding and have a Teensy 3.6 to run everything. I'm hoping someone will be able to read my code and see the problem.
Thanks again for any and all input.
#include <Bounce.h>
#include <Encoder.h>
// Input from hardware
Encoder vor1Encoder(16, 15); // vor1 encoder on pins 15 and 16
short vor1EncoderPrevious = 0;
Encoder vortwoEncoder(14, 13); // vor2 encoder on pins 13 and 14
short vortwoEncoderPrevious = 0;
Encoder adfEncoder(39, 38); // adf encoder on pins 38 and 39
short adfEncoderPrevious = 0;
elapsedMillis vor1EncoderClickInterval = 0;
elapsedMillis vortwoEncoderClickInterval = 0;
elapsedMillis adfEncoderClickInterval = 0;
// OUTPUT //////////////////////////////
const int greenLeftPin = 25;
const int greenNosePin = 24;
const int greenRightPin = 26;
const int gearSwitchPin = 12;
const int redLeftPin = 28;
const int redNosePin = 27;
const int redRightPin = 29;
const int fuelpumpLEDPin = 23;
const int beaconLEDPin = 22;
const int landingLEDPin = 21;
const int taxiLEDPin = 20;
const int navLEDPin = 19;
const int strobeLEDPin = 18;
const int pitotLEDPin = 17;
const int com1LEDPin = 32;
const int avionicsSwitchPin = 2;
const int flapsupSwitchPin = 11;
const int flapsdownSwitchPin = 10;
const int alternatorSwitchPin = 0;
const int fuelpumpSwitchPin = 3;
const int batterySwitchPin = 1;
const int beaconSwitchPin = 4;
const int landingSwitchPin = 5;
const int taxiSwitchPin = 6;
const int navSwitchPin = 7;
const int strobeSwitchPin = 8;
const int pitotSwitchPin = 9;
Bounce gearSwitch(gearSwitchPin, 5);
Bounce avionicsSwitch(avionicsSwitchPin, 5);
Bounce flapsupSwitch(flapsupSwitchPin, 5);
Bounce flapsdownSwitch(flapsdownSwitchPin, 5);
Bounce altSwitch(alternatorSwitchPin, 5);
Bounce fuelpumpSwitch(fuelpumpSwitchPin, 5);
Bounce batterySwitch(batterySwitchPin, 5);
Bounce beaconSwitch(beaconSwitchPin, 5);
Bounce landingSwitch(landingSwitchPin, 5);
Bounce taxiSwitch(taxiSwitchPin, 5);
Bounce navSwitch(navSwitchPin, 5);
Bounce strobeSwitch(strobeSwitchPin, 5);
Bounce pitotSwitch(pitotSwitchPin, 5);
int com1SwitchPin = 37;
int com1State = HIGH; // current state of com1 pin
int com1Reading; // current reading from com1 input
int com1Previous = LOW; // previous reading from com1 input
int com2SwitchPin = 36;
long time = 0;
long debounce = 200;
// Input/Output with simulator
FlightSimFloat vor1Dataref;
FlightSimFloat vortwoDataref;
FlightSimFloat adfDataref;
// Landing gear handle commands
FlightSimCommand gearUp;
FlightSimCommand gearDown;
// Landing gear actual position
FlightSimFloat gearDeployLeft;
FlightSimFloat gearDeployNose;
FlightSimFloat gearDeployRight;
// Landing gear handle position
FlightSimInteger gearHandleDown;
// Aircraft essential bus voltage
FlightSimFloat supplyVolts;
// Avionics switch position
FlightSimInteger avionicsPowerSwitch;
FlightSimCommand avionicsPowerOn;
FlightSimCommand avionicsPowerOff;
//Flaps switch position
FlightSimInteger flapsHandle;
FlightSimCommand flapsUp;
FlightSimCommand flapsDown;
// alternator master switch position
FlightSimInteger alternatorPowerSwitch;
FlightSimCommand altPowerOn;
FlightSimCommand altPowerOff;
// battery master switch position
FlightSimInteger batteryPowerSwitch;
FlightSimCommand batteryOn;
FlightSimCommand batteryOff;
// fuel pump switch position
FlightSimInteger fuelpumpPowerSwitch;
FlightSimCommand fuelPumpOff;
FlightSimCommand fuelPumpOn;
// beacon light switch position
FlightSimInteger beaconPowerSwitch;
FlightSimCommand beaconOn;
FlightSimCommand beaconOff;
// landing light switch position
FlightSimInteger landingPowerSwitch;
FlightSimCommand landingOn;
FlightSimCommand landingOff;
//taxi light switch position
FlightSimInteger taxiPowerSwitch;
FlightSimCommand taxiOn;
FlightSimCommand taxiOff;
//nav lights switch position
FlightSimInteger navlightPowerSwitch;
FlightSimCommand navlightsOn;
FlightSimCommand navlightsOff;
// strobe switch position /////
FlightSimInteger strobePowerSwitch;
FlightSimCommand strobeOn;
FlightSimCommand strobeOff;
// pitot heat position ////////////
FlightSimInteger pitotPowerSwitch;
FlightSimCommand pitotOn;
FlightSimCommand pitotOff;
// com 1 audio on/off/////////
FlightSimInteger com1Audio;
FlightSimCommand com1Toggle;
void setup() {
vor1Dataref = XPlaneRef("sim/cockpit2/radios/actuators/nav1_obs_deg_mag_pilot");
vortwoDataref = XPlaneRef("sim/cockpit2/radios/actuators/nav2_obs_deg_mag_pilot");
adfDataref = XPlaneRef("sim/cockpit/radios/adf1_cardinal_dir");
//////////////////
// Input
//
pinMode (gearSwitchPin, INPUT_PULLUP);
pinMode (avionicsSwitchPin, INPUT_PULLUP);
pinMode (flapsupSwitchPin, INPUT_PULLUP);
pinMode (flapsdownSwitchPin, INPUT_PULLUP);
pinMode (alternatorSwitchPin, INPUT_PULLUP);
pinMode (batterySwitchPin, INPUT_PULLUP);
pinMode (fuelpumpSwitchPin, INPUT_PULLUP);
pinMode (beaconSwitchPin, INPUT_PULLUP);
pinMode (landingSwitchPin, INPUT_PULLUP);
pinMode (taxiSwitchPin, INPUT_PULLUP);
pinMode (navSwitchPin, INPUT_PULLUP);
pinMode (strobeSwitchPin, INPUT_PULLUP);
pinMode (pitotSwitchPin, INPUT_PULLUP);
pinMode (com1SwitchPin, INPUT_PULLUP);
//////////////////
// Output
//
pinMode(greenLeftPin, OUTPUT);
pinMode(greenNosePin, OUTPUT);
pinMode(greenRightPin, OUTPUT);
pinMode(redLeftPin, OUTPUT);
pinMode(redNosePin, OUTPUT);
pinMode(redRightPin, OUTPUT);
pinMode (fuelpumpLEDPin, OUTPUT);
pinMode (beaconLEDPin, OUTPUT);
pinMode (landingLEDPin, OUTPUT);
pinMode (taxiLEDPin, OUTPUT);
pinMode (navLEDPin, OUTPUT);
pinMode (strobeLEDPin, OUTPUT);
pinMode (pitotLEDPin, OUTPUT);
pinMode (com1LEDPin, OUTPUT);
//////////////////
// X-Plane data Pref
//
gearUp = XPlaneRef("sim/flight_controls/landing_gear_up");
gearDown = XPlaneRef("sim/flight_controls/landing_gear_down");
gearDeployLeft = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[1]");
gearDeployNose = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[0]");
gearDeployRight = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[2]");
gearHandleDown = XPlaneRef("sim/cockpit2/controls/gear_handle_down");
supplyVolts = XPlaneRef("sim/cockpit2/electrical/bus_volts[0]");
avionicsPowerSwitch = XPlaneRef("sim/cockpit2/switches/avionics_power_on");
avionicsPowerOff = XPlaneRef("sim/systems/avionics_off");
avionicsPowerOn = XPlaneRef("sim/systems/avionics_on");
flapsHandle = XPlaneRef("sim/flightmodel2/controls/flap_handle_deploy_ratio");
flapsDown = XPlaneRef("sim/flight_controls/flaps_down");
flapsUp = XPlaneRef("sim/flight_controls/flaps_up");
alternatorPowerSwitch = XPlaneRef("sim/cockpit2/electrical/generator_on[0]");
altPowerOn = XPlaneRef("sim/electrical/generator_1_on");
altPowerOff = XPlaneRef("sim/electrical/generator_1_off");
fuelpumpPowerSwitch = XPlaneRef("sim/cockpit2/engine/actuators/fuel_pump_on");
fuelPumpOff = XPlaneRef("sim/fuel/fuel_pump_1_off");
fuelPumpOn = XPlaneRef("sim/fuel/fuel_pump_1_on");
batteryPowerSwitch = XPlaneRef("sim/cockpit2/electrical/battery_on");
batteryOn = XPlaneRef("sim/electrical/battery_1_on");
batteryOff = XPlaneRef("sim/electrical/battery_1_off");
beaconPowerSwitch = XPlaneRef("sim/cockpit/electrical/beacon_lights_on");
beaconOn = XPlaneRef("sim/lights/beacon_lights_on");
beaconOff = XPlaneRef("sim/lights/beacon_lights_off");
landingPowerSwitch = XPlaneRef ("sim/cockpit/electrical/landing_lights_on");
landingOn = XPlaneRef ("sim/lights/landing_lights_on");
landingOff = XPlaneRef("sim/lights/landing_lights_off");
taxiPowerSwitch = XPlaneRef ("sim/cockpit/electrical/taxi_light_on");
taxiOn = XPlaneRef ("sim/lights/taxi_lights_on");
taxiOff = XPlaneRef ("sim/lights/taxi_lights_off");
navlightPowerSwitch = XPlaneRef("sim/cockpit/electrical/nav_lights_on");
navlightsOn = XPlaneRef ("sim/lights/nav_lights_on");
navlightsOff = XPlaneRef ("sim/lights/nav_lights_off");
strobePowerSwitch = XPlaneRef("sim/cockpit/electrical/strobe_lights_on");
strobeOn = XPlaneRef("sim/lights/strobe_lights_on");
strobeOff = XPlaneRef ("sim/lights/strobe_lights_off");
pitotPowerSwitch = XPlaneRef("sim/cockpit/switches/pitot_heat_on");
pitotOn = XPlaneRef("sim/ice/pitot_heat0_on");
pitotOff = XPlaneRef("sim/ice/pitot_heat0_off");
com1Audio = XPlaneRef("sim/cockpit2/actuators/audio_selection_com1");
com1Toggle = XPlaneRef("sim/audio_panel/monitor_audio_com1");
}
void loop() {
FlightSim.update();
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short vor1Clicks = (vor1Encoder.read() - vor1EncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (vor1Clicks != 0) {
// change in degrees from current stored value (can be <0 )
float vor1Change = 0;
// Threshold between FINE and FAST control: 30ms per click
if (vor1EncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
vor1Change = vor1Clicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
vor1Change = vor1Clicks * 5.0;
}
// new value = current value plus changes
float vor1NewValue = vor1Dataref + vor1Change;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (vor1NewValue < 0.0) vor1NewValue += 360.0;
while (vor1NewValue >= 360.0) vor1NewValue -= 360.0;
// write validated new heading back to dataref
vor1Dataref = vor1NewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
vor1EncoderPrevious = 0;
vor1Encoder.write(0);
vor1EncoderClickInterval = 0;
}
// vortwo code
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short vortwoClicks = (vortwoEncoder.read() - vortwoEncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (vortwoClicks != 0) {
// change in degrees from current stored value (can be <0 )
float vortwoChange = 0;
// Threshold between FINE and FAST control: 30ms per click
if (vortwoEncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
vortwoChange = vortwoClicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
vortwoChange = vortwoClicks * 5.0;
}
// new value = current value plus changes
float vortwoNewValue = vortwoDataref + vortwoChange;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (vortwoNewValue < 0.0) vortwoNewValue += 360.0;
while (vortwoNewValue >= 360.0) vortwoNewValue -= 360.0;
// write validated new heading back to dataref
vortwoDataref = vortwoNewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
vortwoEncoderPrevious = 0;
vortwoEncoder.write(0);
vortwoEncoderClickInterval = 0;
}
// adf code
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short adfClicks = (adfEncoder.read() - adfEncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (adfClicks != 0) {
// change in degrees from current stored value (can be <0 )
float adfChange = 0;
// Threshold between FINE and FAST control: 30ms per click
if (adfEncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
adfChange = adfClicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
adfChange = adfClicks * 5.0;
}
// new value = current value plus changes
float adfNewValue = adfDataref + adfChange;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (adfNewValue < 0.0) adfNewValue += 360.0;
while (adfNewValue >= 360.0) adfNewValue -= 360.0;
// write validated new heading back to dataref
adfDataref = adfNewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
adfEncoderPrevious = 0;
adfEncoder.write(0);
adfEncoderClickInterval = 0;
}
//////////////////////
// Landing Gear //////
//////////////////
// Process input
//
gearSwitch.update();
// blocking input on gear handle position
if(gearSwitch.read() == LOW) { // if the switch is closed
if (gearHandleDown == 0) { // if gear handle is up
gearDown.once(); // move it down
}
} else {
if (gearHandleDown == 1) { // if gear handle is down
gearUp.once(); // move it up
}
} // gearSwitch
//////////////////
// Process output
//
// avionics switches /////////////////////////////////////////////
avionicsSwitch.update();
if(avionicsSwitch.read() == HIGH) { // if FUEL PUMP switch is closed
if (avionicsPowerSwitch == 0) { // if avionics power is up
avionicsPowerOn.once(); // move it down
}
} else {
if (avionicsPowerSwitch == 1) { // if handle is down
avionicsPowerOff.once(); // move it up
}
}
// fuel pump switches ////////////////////////////////////////////
fuelpumpSwitch.update();
if(fuelpumpSwitch.read() == HIGH) { // if FUEL PUMP switch is closed
if (fuelpumpPowerSwitch == 0) { // if avionics power is up
fuelPumpOn.once(); // move it down
digitalWrite (fuelpumpLEDPin, 1);
}
} else {
if (fuelpumpPowerSwitch == 1) { // if handle is down
fuelPumpOff.once(); // move it up
digitalWrite (fuelpumpLEDPin, 0);
}
}
// alternator switches /////////////////////////////////////////////////
altSwitch.update();
if(altSwitch.read() == LOW) { // if ALTERNATOR switch is closed
digitalWrite (alternatorPowerSwitch, 0); { // if avionics power is up
altPowerOn.once(); // move it down
}
} else {
digitalWrite (alternatorPowerSwitch, 1); { // if handle is down
altPowerOff.once(); // move it up
}
// battery switches /////////////////////////////////////////////////////
batterySwitch.update();
if(batterySwitch.read() == LOW) { // if BATTERY switch is closed
digitalWrite (batteryPowerSwitch, 0); { // if avionics power is up
batteryOn.once(); // move it down
}
} else {
digitalWrite (batteryPowerSwitch, 1); { // if handle is down
batteryOff.once(); // move it up
}
// beacon light switches /////////////////////////////////////////////////////////
beaconSwitch.update();
if (beaconSwitch.read() == HIGH) { // if beason switch is off
digitalWrite (beaconPowerSwitch, 1);
digitalWrite(beaconLEDPin, 1);
beaconOn.once();
} else {
digitalWrite (beaconPowerSwitch, 0); {
digitalWrite(beaconLEDPin, 0);
beaconOff.once();
}
// landing light switches //////////////////////////////////////////////////
landingSwitch.update();
if (landingSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (landingPowerSwitch, 1);
digitalWrite (landingLEDPin, 1);
landingOn.once();
} else {
digitalWrite (landingPowerSwitch, 0);
digitalWrite (landingLEDPin, 0);
landingOff.once();
}
// taxi light switches ////////////////////////////////////////////////////////
taxiSwitch.update();
if (taxiSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (taxiPowerSwitch, 1);
digitalWrite (taxiLEDPin, 1);
taxiOn.once();
} else {
digitalWrite (taxiPowerSwitch, 0);
digitalWrite (taxiLEDPin, 0);
taxiOff.once();
}
// flaps switches //////////////////////////////////////////////////////
flapsupSwitch.update(); flapsdownSwitch.update();
if (flapsupSwitch.fallingEdge()) {
flapsDown.once();
}else if (flapsdownSwitch.fallingEdge()) {
flapsUp.once();
}
// nav light switches /////////////////////////////////////////////////////////
navSwitch.update();
if (navSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (navlightPowerSwitch, 1);
digitalWrite (navLEDPin, 1);
navlightsOn.once();
} else {
digitalWrite (navlightPowerSwitch, 0);
digitalWrite (navLEDPin, 0);
navlightsOff.once();
}
// pitot heat switches ////////////////////////////////////////////////////////
pitotSwitch.update();
if (pitotSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (pitotPowerSwitch, 1);
digitalWrite (pitotLEDPin, 1);
pitotOn.once();
} else {
digitalWrite (pitotPowerSwitch, 0);
digitalWrite (pitotLEDPin, 0);
pitotOff.once();
// strobe light switch ////////////////////////////////////////////////////
strobeSwitch.update();
if (strobeSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (strobePowerSwitch, 1);
digitalWrite (strobeLEDPin, 1);
strobeOn.once();
} else {
digitalWrite (strobePowerSwitch, 0);
digitalWrite (strobeLEDPin, 0);
strobeOff.once();
}
}
}
}
}
}
I thank you in advance for any help you can give me on this project.
At the moment the Com and Nav buttons haven't been programmed. For the rest, they all function individually, until the Battery and Alt switches are on, then only the fuel pump, Avionics, gear lever and encoders work. There seems to be something locking out the code for the other switches once they are on.
I have gone back bit by bit but come up with the same issue each time.
I am really new to this coding and have a Teensy 3.6 to run everything. I'm hoping someone will be able to read my code and see the problem.
Thanks again for any and all input.
#include <Bounce.h>
#include <Encoder.h>
// Input from hardware
Encoder vor1Encoder(16, 15); // vor1 encoder on pins 15 and 16
short vor1EncoderPrevious = 0;
Encoder vortwoEncoder(14, 13); // vor2 encoder on pins 13 and 14
short vortwoEncoderPrevious = 0;
Encoder adfEncoder(39, 38); // adf encoder on pins 38 and 39
short adfEncoderPrevious = 0;
elapsedMillis vor1EncoderClickInterval = 0;
elapsedMillis vortwoEncoderClickInterval = 0;
elapsedMillis adfEncoderClickInterval = 0;
// OUTPUT //////////////////////////////
const int greenLeftPin = 25;
const int greenNosePin = 24;
const int greenRightPin = 26;
const int gearSwitchPin = 12;
const int redLeftPin = 28;
const int redNosePin = 27;
const int redRightPin = 29;
const int fuelpumpLEDPin = 23;
const int beaconLEDPin = 22;
const int landingLEDPin = 21;
const int taxiLEDPin = 20;
const int navLEDPin = 19;
const int strobeLEDPin = 18;
const int pitotLEDPin = 17;
const int com1LEDPin = 32;
const int avionicsSwitchPin = 2;
const int flapsupSwitchPin = 11;
const int flapsdownSwitchPin = 10;
const int alternatorSwitchPin = 0;
const int fuelpumpSwitchPin = 3;
const int batterySwitchPin = 1;
const int beaconSwitchPin = 4;
const int landingSwitchPin = 5;
const int taxiSwitchPin = 6;
const int navSwitchPin = 7;
const int strobeSwitchPin = 8;
const int pitotSwitchPin = 9;
Bounce gearSwitch(gearSwitchPin, 5);
Bounce avionicsSwitch(avionicsSwitchPin, 5);
Bounce flapsupSwitch(flapsupSwitchPin, 5);
Bounce flapsdownSwitch(flapsdownSwitchPin, 5);
Bounce altSwitch(alternatorSwitchPin, 5);
Bounce fuelpumpSwitch(fuelpumpSwitchPin, 5);
Bounce batterySwitch(batterySwitchPin, 5);
Bounce beaconSwitch(beaconSwitchPin, 5);
Bounce landingSwitch(landingSwitchPin, 5);
Bounce taxiSwitch(taxiSwitchPin, 5);
Bounce navSwitch(navSwitchPin, 5);
Bounce strobeSwitch(strobeSwitchPin, 5);
Bounce pitotSwitch(pitotSwitchPin, 5);
int com1SwitchPin = 37;
int com1State = HIGH; // current state of com1 pin
int com1Reading; // current reading from com1 input
int com1Previous = LOW; // previous reading from com1 input
int com2SwitchPin = 36;
long time = 0;
long debounce = 200;
// Input/Output with simulator
FlightSimFloat vor1Dataref;
FlightSimFloat vortwoDataref;
FlightSimFloat adfDataref;
// Landing gear handle commands
FlightSimCommand gearUp;
FlightSimCommand gearDown;
// Landing gear actual position
FlightSimFloat gearDeployLeft;
FlightSimFloat gearDeployNose;
FlightSimFloat gearDeployRight;
// Landing gear handle position
FlightSimInteger gearHandleDown;
// Aircraft essential bus voltage
FlightSimFloat supplyVolts;
// Avionics switch position
FlightSimInteger avionicsPowerSwitch;
FlightSimCommand avionicsPowerOn;
FlightSimCommand avionicsPowerOff;
//Flaps switch position
FlightSimInteger flapsHandle;
FlightSimCommand flapsUp;
FlightSimCommand flapsDown;
// alternator master switch position
FlightSimInteger alternatorPowerSwitch;
FlightSimCommand altPowerOn;
FlightSimCommand altPowerOff;
// battery master switch position
FlightSimInteger batteryPowerSwitch;
FlightSimCommand batteryOn;
FlightSimCommand batteryOff;
// fuel pump switch position
FlightSimInteger fuelpumpPowerSwitch;
FlightSimCommand fuelPumpOff;
FlightSimCommand fuelPumpOn;
// beacon light switch position
FlightSimInteger beaconPowerSwitch;
FlightSimCommand beaconOn;
FlightSimCommand beaconOff;
// landing light switch position
FlightSimInteger landingPowerSwitch;
FlightSimCommand landingOn;
FlightSimCommand landingOff;
//taxi light switch position
FlightSimInteger taxiPowerSwitch;
FlightSimCommand taxiOn;
FlightSimCommand taxiOff;
//nav lights switch position
FlightSimInteger navlightPowerSwitch;
FlightSimCommand navlightsOn;
FlightSimCommand navlightsOff;
// strobe switch position /////
FlightSimInteger strobePowerSwitch;
FlightSimCommand strobeOn;
FlightSimCommand strobeOff;
// pitot heat position ////////////
FlightSimInteger pitotPowerSwitch;
FlightSimCommand pitotOn;
FlightSimCommand pitotOff;
// com 1 audio on/off/////////
FlightSimInteger com1Audio;
FlightSimCommand com1Toggle;
void setup() {
vor1Dataref = XPlaneRef("sim/cockpit2/radios/actuators/nav1_obs_deg_mag_pilot");
vortwoDataref = XPlaneRef("sim/cockpit2/radios/actuators/nav2_obs_deg_mag_pilot");
adfDataref = XPlaneRef("sim/cockpit/radios/adf1_cardinal_dir");
//////////////////
// Input
//
pinMode (gearSwitchPin, INPUT_PULLUP);
pinMode (avionicsSwitchPin, INPUT_PULLUP);
pinMode (flapsupSwitchPin, INPUT_PULLUP);
pinMode (flapsdownSwitchPin, INPUT_PULLUP);
pinMode (alternatorSwitchPin, INPUT_PULLUP);
pinMode (batterySwitchPin, INPUT_PULLUP);
pinMode (fuelpumpSwitchPin, INPUT_PULLUP);
pinMode (beaconSwitchPin, INPUT_PULLUP);
pinMode (landingSwitchPin, INPUT_PULLUP);
pinMode (taxiSwitchPin, INPUT_PULLUP);
pinMode (navSwitchPin, INPUT_PULLUP);
pinMode (strobeSwitchPin, INPUT_PULLUP);
pinMode (pitotSwitchPin, INPUT_PULLUP);
pinMode (com1SwitchPin, INPUT_PULLUP);
//////////////////
// Output
//
pinMode(greenLeftPin, OUTPUT);
pinMode(greenNosePin, OUTPUT);
pinMode(greenRightPin, OUTPUT);
pinMode(redLeftPin, OUTPUT);
pinMode(redNosePin, OUTPUT);
pinMode(redRightPin, OUTPUT);
pinMode (fuelpumpLEDPin, OUTPUT);
pinMode (beaconLEDPin, OUTPUT);
pinMode (landingLEDPin, OUTPUT);
pinMode (taxiLEDPin, OUTPUT);
pinMode (navLEDPin, OUTPUT);
pinMode (strobeLEDPin, OUTPUT);
pinMode (pitotLEDPin, OUTPUT);
pinMode (com1LEDPin, OUTPUT);
//////////////////
// X-Plane data Pref
//
gearUp = XPlaneRef("sim/flight_controls/landing_gear_up");
gearDown = XPlaneRef("sim/flight_controls/landing_gear_down");
gearDeployLeft = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[1]");
gearDeployNose = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[0]");
gearDeployRight = XPlaneRef("sim/flightmodel2/gear/deploy_ratio[2]");
gearHandleDown = XPlaneRef("sim/cockpit2/controls/gear_handle_down");
supplyVolts = XPlaneRef("sim/cockpit2/electrical/bus_volts[0]");
avionicsPowerSwitch = XPlaneRef("sim/cockpit2/switches/avionics_power_on");
avionicsPowerOff = XPlaneRef("sim/systems/avionics_off");
avionicsPowerOn = XPlaneRef("sim/systems/avionics_on");
flapsHandle = XPlaneRef("sim/flightmodel2/controls/flap_handle_deploy_ratio");
flapsDown = XPlaneRef("sim/flight_controls/flaps_down");
flapsUp = XPlaneRef("sim/flight_controls/flaps_up");
alternatorPowerSwitch = XPlaneRef("sim/cockpit2/electrical/generator_on[0]");
altPowerOn = XPlaneRef("sim/electrical/generator_1_on");
altPowerOff = XPlaneRef("sim/electrical/generator_1_off");
fuelpumpPowerSwitch = XPlaneRef("sim/cockpit2/engine/actuators/fuel_pump_on");
fuelPumpOff = XPlaneRef("sim/fuel/fuel_pump_1_off");
fuelPumpOn = XPlaneRef("sim/fuel/fuel_pump_1_on");
batteryPowerSwitch = XPlaneRef("sim/cockpit2/electrical/battery_on");
batteryOn = XPlaneRef("sim/electrical/battery_1_on");
batteryOff = XPlaneRef("sim/electrical/battery_1_off");
beaconPowerSwitch = XPlaneRef("sim/cockpit/electrical/beacon_lights_on");
beaconOn = XPlaneRef("sim/lights/beacon_lights_on");
beaconOff = XPlaneRef("sim/lights/beacon_lights_off");
landingPowerSwitch = XPlaneRef ("sim/cockpit/electrical/landing_lights_on");
landingOn = XPlaneRef ("sim/lights/landing_lights_on");
landingOff = XPlaneRef("sim/lights/landing_lights_off");
taxiPowerSwitch = XPlaneRef ("sim/cockpit/electrical/taxi_light_on");
taxiOn = XPlaneRef ("sim/lights/taxi_lights_on");
taxiOff = XPlaneRef ("sim/lights/taxi_lights_off");
navlightPowerSwitch = XPlaneRef("sim/cockpit/electrical/nav_lights_on");
navlightsOn = XPlaneRef ("sim/lights/nav_lights_on");
navlightsOff = XPlaneRef ("sim/lights/nav_lights_off");
strobePowerSwitch = XPlaneRef("sim/cockpit/electrical/strobe_lights_on");
strobeOn = XPlaneRef("sim/lights/strobe_lights_on");
strobeOff = XPlaneRef ("sim/lights/strobe_lights_off");
pitotPowerSwitch = XPlaneRef("sim/cockpit/switches/pitot_heat_on");
pitotOn = XPlaneRef("sim/ice/pitot_heat0_on");
pitotOff = XPlaneRef("sim/ice/pitot_heat0_off");
com1Audio = XPlaneRef("sim/cockpit2/actuators/audio_selection_com1");
com1Toggle = XPlaneRef("sim/audio_panel/monitor_audio_com1");
}
void loop() {
FlightSim.update();
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short vor1Clicks = (vor1Encoder.read() - vor1EncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (vor1Clicks != 0) {
// change in degrees from current stored value (can be <0 )
float vor1Change = 0;
// Threshold between FINE and FAST control: 30ms per click
if (vor1EncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
vor1Change = vor1Clicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
vor1Change = vor1Clicks * 5.0;
}
// new value = current value plus changes
float vor1NewValue = vor1Dataref + vor1Change;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (vor1NewValue < 0.0) vor1NewValue += 360.0;
while (vor1NewValue >= 360.0) vor1NewValue -= 360.0;
// write validated new heading back to dataref
vor1Dataref = vor1NewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
vor1EncoderPrevious = 0;
vor1Encoder.write(0);
vor1EncoderClickInterval = 0;
}
// vortwo code
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short vortwoClicks = (vortwoEncoder.read() - vortwoEncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (vortwoClicks != 0) {
// change in degrees from current stored value (can be <0 )
float vortwoChange = 0;
// Threshold between FINE and FAST control: 30ms per click
if (vortwoEncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
vortwoChange = vortwoClicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
vortwoChange = vortwoClicks * 5.0;
}
// new value = current value plus changes
float vortwoNewValue = vortwoDataref + vortwoChange;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (vortwoNewValue < 0.0) vortwoNewValue += 360.0;
while (vortwoNewValue >= 360.0) vortwoNewValue -= 360.0;
// write validated new heading back to dataref
vortwoDataref = vortwoNewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
vortwoEncoderPrevious = 0;
vortwoEncoder.write(0);
vortwoEncoderClickInterval = 0;
}
// adf code
// divide by 4 to find how many clicks the encoder's been turned
// (+ve clockwise, -ve anticlockwise, normally)
short adfClicks = (adfEncoder.read() - adfEncoderPrevious) / 4;
// when encoder 'clicks' into a new detent:
if (adfClicks != 0) {
// change in degrees from current stored value (can be <0 )
float adfChange = 0;
// Threshold between FINE and FAST control: 30ms per click
if (adfEncoderClickInterval > 30) {
// FINE/slow mode. Change by 0.25 degrees per click.
adfChange = adfClicks * 0.25;
} else {
// FAST/coarse mode. Change by 5 degrees per click.
adfChange = adfClicks * 5.0;
}
// new value = current value plus changes
float adfNewValue = adfDataref + adfChange;
// make sure new value is valid (i.e. when moving across 0deg/360deg)
while (adfNewValue < 0.0) adfNewValue += 360.0;
while (adfNewValue >= 360.0) adfNewValue -= 360.0;
// write validated new heading back to dataref
adfDataref = adfNewValue;
// reset encoder state and interval timer
// (only when encoder has 'clicked' into a new detent!)
adfEncoderPrevious = 0;
adfEncoder.write(0);
adfEncoderClickInterval = 0;
}
//////////////////////
// Landing Gear //////
//////////////////
// Process input
//
gearSwitch.update();
// blocking input on gear handle position
if(gearSwitch.read() == LOW) { // if the switch is closed
if (gearHandleDown == 0) { // if gear handle is up
gearDown.once(); // move it down
}
} else {
if (gearHandleDown == 1) { // if gear handle is down
gearUp.once(); // move it up
}
} // gearSwitch
//////////////////
// Process output
//
// avionics switches /////////////////////////////////////////////
avionicsSwitch.update();
if(avionicsSwitch.read() == HIGH) { // if FUEL PUMP switch is closed
if (avionicsPowerSwitch == 0) { // if avionics power is up
avionicsPowerOn.once(); // move it down
}
} else {
if (avionicsPowerSwitch == 1) { // if handle is down
avionicsPowerOff.once(); // move it up
}
}
// fuel pump switches ////////////////////////////////////////////
fuelpumpSwitch.update();
if(fuelpumpSwitch.read() == HIGH) { // if FUEL PUMP switch is closed
if (fuelpumpPowerSwitch == 0) { // if avionics power is up
fuelPumpOn.once(); // move it down
digitalWrite (fuelpumpLEDPin, 1);
}
} else {
if (fuelpumpPowerSwitch == 1) { // if handle is down
fuelPumpOff.once(); // move it up
digitalWrite (fuelpumpLEDPin, 0);
}
}
// alternator switches /////////////////////////////////////////////////
altSwitch.update();
if(altSwitch.read() == LOW) { // if ALTERNATOR switch is closed
digitalWrite (alternatorPowerSwitch, 0); { // if avionics power is up
altPowerOn.once(); // move it down
}
} else {
digitalWrite (alternatorPowerSwitch, 1); { // if handle is down
altPowerOff.once(); // move it up
}
// battery switches /////////////////////////////////////////////////////
batterySwitch.update();
if(batterySwitch.read() == LOW) { // if BATTERY switch is closed
digitalWrite (batteryPowerSwitch, 0); { // if avionics power is up
batteryOn.once(); // move it down
}
} else {
digitalWrite (batteryPowerSwitch, 1); { // if handle is down
batteryOff.once(); // move it up
}
// beacon light switches /////////////////////////////////////////////////////////
beaconSwitch.update();
if (beaconSwitch.read() == HIGH) { // if beason switch is off
digitalWrite (beaconPowerSwitch, 1);
digitalWrite(beaconLEDPin, 1);
beaconOn.once();
} else {
digitalWrite (beaconPowerSwitch, 0); {
digitalWrite(beaconLEDPin, 0);
beaconOff.once();
}
// landing light switches //////////////////////////////////////////////////
landingSwitch.update();
if (landingSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (landingPowerSwitch, 1);
digitalWrite (landingLEDPin, 1);
landingOn.once();
} else {
digitalWrite (landingPowerSwitch, 0);
digitalWrite (landingLEDPin, 0);
landingOff.once();
}
// taxi light switches ////////////////////////////////////////////////////////
taxiSwitch.update();
if (taxiSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (taxiPowerSwitch, 1);
digitalWrite (taxiLEDPin, 1);
taxiOn.once();
} else {
digitalWrite (taxiPowerSwitch, 0);
digitalWrite (taxiLEDPin, 0);
taxiOff.once();
}
// flaps switches //////////////////////////////////////////////////////
flapsupSwitch.update(); flapsdownSwitch.update();
if (flapsupSwitch.fallingEdge()) {
flapsDown.once();
}else if (flapsdownSwitch.fallingEdge()) {
flapsUp.once();
}
// nav light switches /////////////////////////////////////////////////////////
navSwitch.update();
if (navSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (navlightPowerSwitch, 1);
digitalWrite (navLEDPin, 1);
navlightsOn.once();
} else {
digitalWrite (navlightPowerSwitch, 0);
digitalWrite (navLEDPin, 0);
navlightsOff.once();
}
// pitot heat switches ////////////////////////////////////////////////////////
pitotSwitch.update();
if (pitotSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (pitotPowerSwitch, 1);
digitalWrite (pitotLEDPin, 1);
pitotOn.once();
} else {
digitalWrite (pitotPowerSwitch, 0);
digitalWrite (pitotLEDPin, 0);
pitotOff.once();
// strobe light switch ////////////////////////////////////////////////////
strobeSwitch.update();
if (strobeSwitch.read() == HIGH) { // if landing switch is off
digitalWrite (strobePowerSwitch, 1);
digitalWrite (strobeLEDPin, 1);
strobeOn.once();
} else {
digitalWrite (strobePowerSwitch, 0);
digitalWrite (strobeLEDPin, 0);
strobeOff.once();
}
}
}
}
}
}