Skip to content

03.07. FlightSimWriteDatarefSwitch

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

The FlightSimWriteDataref switch handles multi-position switches and writes a value that corresponds to the switch position to a dataref in X-Plane. This may not be the correct approach to change a switch position in X-Plane, read more about the issue here.

Should your aircraft really be controlled by writing a value into a dataref (for example, the IXEG 737-300), here's how you use the FlightSimWriteDatarefSwitch object.

Each position on your physical matches one of the values of the dataref. These values can be integer or floating point. At each moment, at maximum one of the input pins may be closed. If none of the pins is connected, the switch writes a "default value" to the dataref.

The switch object has various configuration options that can be set on declaration or during setup():

  • The "default value". If not specified, the value 0.0 is used as default value.
  • A "tolerance" that is used to determine if a new dataref value needs to be written. This happens only if the difference between the dataref value and the value that corresponds to the current switch position is larger than this tolerance. If not specified, the value DEFAULT_TOLERANCE (0.0001) is used.
  • Please note that, in contrast to the FlightSimOnOffCommandSwitch, FlightSimOnCommandSwitch, FlightSimOffCommandSwitch, FlightSimPushbutton and FlightSimOnOffDatarefSwitch objects, the pin numbers of FlightSimWriteDatarefSwitch must be set on declaration.

Therefore, on declaration, the FlightSimWriteDatarefSwitch needs at least the following information:

  • Number of connected pins
  • List of pin numbers
  • List of values to write to the dataref (one for each pin number)

The dataref must be set in setup():

#include <FlightSimSwitches.h>

// always declare FlightSimSwitches first
FlightSimSwitches switches;

// Multi-position switch connected to pins 3, 4 and 5.
// When pin 3 is connected to GND, the value 2 will be
// written to the dataref. Connecting pin 4 to GND writes
// 1 to the dataref. Same for pin 5 and value 3.
// When no pin is connected to GND, the value 0 is written
// to the dataref (default value).
// The specific dataref to write to will be defined in setup().
FlightSimWriteDatarefSwitch sw1(
  3,                        // 3 pins connected
  SWITCH_POSITIONS(3,4,5),  // pin numbers of connected pins
  SWITCH_VALUES   (2,1,3)); // one value for each pin number

// Another multi-position switch, but this time
// the default value that the dataref should take
// when none of the pins is connected is 5.
FlightSimWriteDatarefSwitch sw2(
  3,                        // 3 pins connected
  SWITCH_POSITIONS(6,7,8),  // pin numbers of connected pins
  SWITCH_VALUES   (2,1,3),  // one value for each pin number
  5);                       // default value

// Another multi-position switch, but this time
// the default value and tolerance are defined
// in setup()
FlightSimWriteDatarefSwitch sw3(
  4,                        // 4 pins connected
  SWITCH_POSITIONS(9,10,11,12),  // pin numbers of connected pins
  SWITCH_VALUES   (2, 1, 3, 4)); // one value for each pin number


void setup() {
  delay(1000);

  Serial.begin(115200);

  // set switch 1 dataref
  sw1 = XPlaneRef("dataref/for/switch1");

  // set switch 2 dataref
  sw2 = XPlaneRef("dataref/for/switch2");

  // set switch 3 dataref, commands and options
  sw3 = XPlaneRef("dataref/for/switch3");
  sw3.setDefaultValue(5);  // Default value: 5 if no pin is active
  sw3.setTolerance(0.1);   

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

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