Transponder with one rotary - MobiFlight/MobiFlight-Connector GitHub Wiki
This page is work in progress.
Goal
We will create a device to control a transponder using an LCD display and one single encoder. We'll have a look into
- Mobiflight variables
- References
- PWM to control the LCD background brightness for auto-shutoff
- Preconditions
- Modifiers
Our goal is to use the button of the encoder to switch the transponder digit to be modified by the rotary. When the device isn't connected to a running sim the background of the LCD will be off.
Needed hardware
You will need:
- 1 compatible device for MF, eg. Arduino Mega 2560 or similar
- 1 LCD display with I2C interface
- 1 Rotary encoder
- Cables
- (maybe) 1 Breadboard to supply GND and VCC from Arduino to encoder and LCD
Needed skills
You should be familar with the basics of MF like adding hardware to a MobiFlight Module. You should know the difference between the Output configs and the Input configs of MF.
The wiring
Wire the hardware similar to this drawing. At the moment, the wire between the I2C interface and a PWM enabled pin should be omitted, the jumper should be kept on the LED pins. This way the LCD will be lit for the moment. Later we connect the I2C interface to a PWM pin to control the brightness from MF.
Take care to use the I2C pins suitable for you micro controller.
Selecting the digit to change
To change the digit to be set when turning the encoder we will add a Input config for the encoder button. I named the configuration selectedDigit
and assinged the encoder push. The On Press action is of type MobiFlight - Variable, the variable type is Number. The name can be chosen as you like by entering into the box, I chose selectedDigit
as variable name.
The Value is set to the formula ($+1) % 4
. $ equals to the value of the variable which is initially 0. It adds 1 to this value. The result is divided by 4 but only the remainder is assigned back to the variable selectedDigit
. Sample:
- 0 -> adding 1 = 1 -> dividing by 4 is 0, remainder 1
- 1 -> adding 1 = 2 -> dividing by 4 is 0, remainder 2
- 2 -> adding 1 = 3 -> dividing by 4 is 0, remainder 3
- 3 -> adding 1 = 4 -> dividing by 4 is 1, remainder 0
Using this formula we can switch the variable from 0 to 3 and then begin at 0 again.
Preparing the output
We change to the Output configs tab. There we will create some outputs.
Providing the variable
Create a new configuration. This will only be used to output the variable. We will need this line later. Only select the created variable selectedDigit
on the Sim Variable tab. Everything else won't be changed, no Modify, no Display, no Precondition.
Changing the variable to a string
We want to change the numbers 0 to 3 to a string which will indicate the selected digit we're about to change. This will be a string like --^-
to indicate the 10th to be changed. I will show two different method to change the number into a string.
For both methods: Create a new configuration named selectedDigitString
, use a MobiFlight Variable and again select selectedDigit
and keep the type Number. Change to the Modify tab and ...
Method 1: Comparison Modifier
The first method to change the number into a string is to use 4 Comparison Modifiers. Change to the Modify tab and Add Modifier of type Compare four times.
Select each line by clicking on the Compare:
text. The value in the set it to field has to be surrounded by single quotes: ' The else, set it to field will always be empty. When empty, the evaluation will continue with the next Comparison line.
=
0
set it to'^---'
=
1
set it to'-^--'
=
2
set it to'--^-'
=
3
set it to'---^'
Method 2: Transformation Modifier
The second way is to use a Transformation Modifier and a Ncalc formula. Change to the Modify tab and Add Modifier of type Transformation. Enter this formula:
if($=0,'^---',if($=1,'-^--',if($=2,'--^-','---^')))
This is a nested condition. It will change the numbers to strings:
- 0 to
^---
- 1 to
-^--
- 2 to
--^-
- everything else to
---^
This will be used to highlight the selected digit on the display.
Interlude
Run the configuration. When pressing the encoder button you should see now the two output configs changing. One line from 0 to 3 and back to 0. The other line should have a moving ^
surrounded by some -
signs.
Displaying Transponder and digit
Now we add another line to configure the output.
Sim Variable tab
Search a configuration for the transponder code. For the MSFS this will be the preset TRANSPONDER CODE
, for the index enter a 1. The tab should look like that:
Modify tab
First we will Add Reference for the string representation selectedDigitString
. Now we can access the value of the string representation of the selected digit here. Each reference will get a symbol that can be used on the Display tab and in formulas to access the reference value. As default, the first added reference will have the symbol #
.
Next add a Modifier. This will be applied to the selected value from the Sim Variable tab. The "issue" with the transpoder code is, that a value like 0001
will be displayed as 1
on the LCD. To change this we Add Modifier of type Padding. Click on the text and set Character to 0
, Length to 4
and Direction to Left
. Using this modification, the value 1
will be displayed as 0001
like needed.
Display tab
Now we put it all together. Select your configured LcdDisplay as Display Type.
Enter this text into the Text field:
XPDR $$$$
####
The $
signs will be replaced by the padded transponder code. The #
will contain the indicator for the selected digit.
When the configuration is running you'll see the indicator changing every time you press the encoder button. When you have a running simulator you should see the transponder code too. When changing the code inside the cockpit, you should see the code changing on the LCD display too.
Changing the transponder code
Head over to the Input configs tab and create a new configuration. Select the Encoder on the Input tab.
Change over to the Config References tab. Add the selectedDigit as a reference like done for the Input. This will give us access to the selected digit her on the input.
Change back to the Input tab. We'll start on the On Left tab. For the MSFS we don't need a preset. Just tick the Show Preset Code box. Enter this code into the box:
# 0 == if{ (>K:XPNDR_1000_DEC) }
# 1 == if{ (>K:XPNDR_100_DEC) }
# 2 == if{ (>K:XPNDR_10_DEC) }
# 3 == if{ (>K:XPNDR_1_DEC) }
The result should look like that:
What does this code do? The #
is the reference to our selectedDigit
. A value of 0 indicates that we want to change the thousands digit. The code does mean:
# 0 ==
compare theselectedDigit
with 0.if{ (>K:XPNDR_1000_DEC) }
If the value is 0, execute the action decrementing the thousands.
The other lines are doing similar stuff for the hundreds, tenths and ones. The result is, that only the selected digit of the transponder code will be changed.
Click on the Copy button of the On Left tab, change to the On Right tab and click Paste. This will copy the configuration to the On Right. Change the code to use increments of the transponder digits:
# 0 == if{ (>K:XPNDR_1000_INC) }
# 1 == if{ (>K:XPNDR_100_INC) }
# 2 == if{ (>K:XPNDR_10_INC) }
# 3 == if{ (>K:XPNDR_1_INC) }
The result should look like that:
Switching of the display
To have control on the brightness of the background, we have to change some cables and the MF Output configs.
Change the cables
Like shown on top of the page in the section The Wiring or in this article (Brightness of LCD displays with I2C), take of the jumper from the I2C. Connect the indicated pin to a pin of your Arduino which is capable of PWM (pulse width modulation).
Changing the module configuration
Go to the MobiFlight Modules, select your device and add an device of type LED / Output. Select the chosen PWM pin. Upload the configuration to your device. I set the name to DisplayBrightness
Adding output brightness
Change to the Output configs tab. Create a new row with name transponderOn
.
Display tab
Select the LED / Output element from your module configuration. Set the checkmark for the PWM mode.
Modify tab
To change an on/off value of 0 or 1 to a brightness value, Add Modifier of type Transform and enter the formula $*127
. If this is to dark or to bright, change the 127 by a higher or a lower number.
Sim Variable tab
As a simple sample we will add the state of the Master Battery as indicator to switch on or off the display. Select the Variable Type of SimConnect (MSFS2020) but don't choose a preset. Tick the mark for Show Preset Code and enter the code (A:ELECTRICAL MASTER BATTERY, Bool) 1 ==
.
This will read out the state of the Master Battery as 0 (off) or 1 (on). The Modifier changes this to a value of 0 (0 * 127 = 0) or 127 (1 * 127 = 127). Because the I2C background pin is connected to this PWM pin, you will change the brightness of the background.
If you stop the configuration, MF will set the value to 0 and shutting off the background light.
Fine tuning
When you have a running simulator in the background and the Master Battery is switched off you might see that the background display is off, but the LCD still shows the text. We will change that.
Old LCD display line
Open the Output config line for the LCD display. Change to the Preconditio tab. Select the topmost Precondition and enter these values:
- Use type of Config item
- Choose config transponderOn (the one created some steps above*
- If current value is != (for is unequal) and 0 meaning, this should be shown, if the transponderOn value is not zero.
New (off) LCD display line
Create a new configuration line, name it transponder-off and edit the line. The Sim Variable and Modify pages will be empty.
On the Display page select the LCD display, clear the text area and enter a single space. This will prevent MF from adding default text to the display.
Change to the Precondition page. Change the precondition as above, but with
- If current value is = (equal to) 0
This will activate this line only, when the Master Battery is off.
Finished
When you now switch off and on the Master Battery of the airplane, the display shoud be turned on and off accordingly.