Skip to content

03.06. FlightSimOnOffDatarefSwitch

Jorg Neves Bliesener edited this page Jan 13, 2019 · 2 revisions

If your aircraft is controlled by writing values to datarefs (check here to find out if this really is the case), then you can use FlightSimOnOffDatarefSwitch for two-position switches and FlightSimWriteDatarefSwitch for multi-position switches.

FlightSimOnOffDatarefSwitch writes the values 0 and 1. If you need different values, use the more flexible FlightSimWriteDatarefSwitch. By default, the value 0 is written when the switch is open and 1 is written when the switch is closed. Only a single dataref is needed and must be assigned in setup().

As with FlightSimOnOffCommandSwitch, the pin number and inversion can be set on declaration or during setup():

#include <FlightSimSwitches.h>

// always declare FlightSimSwitches first
FlightSimSwitches switches;

// Pin number specified in object declaration
FlightSimOnOffDatarefSwitch sw1(2); // Switch between pin 2 and GND

// Pin Number specified in setup()
FlightSimOnOffDatarefSwitch sw2;

// Inverted logic: Write 0 when switch closes and 1 when
// switch opens
FlightSimOnOffDatarefSwitch sw3(4, true); // inverted switch
                                          // between pin 4 and GND

// Set parameters in setup()
FlightSimOnOffDatarefSwitch sw4;

void setup() {
  delay(1000);
  // set pushbutton 1 command
  sw1 = XPlaneRef("dataref/1");

  // set pushbutton 2 pin and commands
  sw2.setPosition(3);
  sw2 = XPlaneRef("dataref/2");

  // set pushbutton 3 command (inverted)
  sw3 = XPlaneRef("dataref/3");

  // set pushbutton 4 pin and commands
  sw4.setPosition(5);
  sw4.setInverted(true);
  sw4 = XPlaneRef("dataref/4");

  switches.setDebug(DEBUG_SWITCHES);
  switches.begin();
}

void loop() {
  FlightSim.update();
  switches.loop();
}