Example Encoder with SimConnect Events - MobiFlight/MobiFlight-Connector GitHub Wiki

How to setup an encoder with SimConnect events in Mobiflight

A digital rotary encoder or simply "encoder" is used quite often in building simulator panels. For example, encoders are very often used in autopilot panels for the knobs to control the settings for altitude, heading, speed and vertical speed. They can be used in substitution of potentiometers in controlling the cabin and panel light dimmers and also in radio panels to tune the COM and NAV frequencies.

image

Photo of commonly used EC11 encoders

Connecting an encoder to your arduino board

An encoder typically has three pins marked in the picture below as A, B and G for the encoding function, plus two more pins, marked P1 and P2, for the push button function.

image

Pins A and B are connected to any two digital pins in your arduino, while pin G is connected to the common ground. For the encoder push button function, one of the pins either P1 or P2 is connected to one arduino digital pin, while the other is connected to common ground, the same as pin G. Make note of which arduino pins you have used, you will need this information later.

image

Create the encoder device configuration

We assume you already uploaded the Mobiflight firmware to your arduino board. Open Mobiflight Connector and go to Mobiflight Modules

image

Your Mobiflight module should appear in the list of boards detected. If not, you need to verify your steps and confirm your board is recognized by Mobiflight before continuing. If your board appears on the list, select this board and press the "Add Device" button at the bottom of the dialog and select "Encoder" from the dropdown list.

image

You should now see a new device on the list of devices for your board with the name "Encoder" with this configuration window

image

Enter the pin numbers where you connected the encoder to your arduino board. Pin A for the Left Pin and Pin B for the Right Pin. Don't worry too much about this assignment. For the EC11 encoders, the setting "1 detent (00)" has worked best for me. You may need to select a different setting depending on the encoder you have. If your Mobiflight later reports that your encoder is rotating to the reverse direction that you actually turn it, you just need to reverse the pin assignments in this configuration.

Create another device configuration for the encoder pushbutton. This is just a regular button and you can follow the tutorial for setting up buttons. Make sure to assign the same pin number, where you connected the P1 encoder pin, to your board.

Save your configuration in the board mfmc file and also upload the new configuration to your board by pressing the upload config button. Mobiflight will report when your board has received the new configuration successfully.

Congratulations, you now have a new encoder connected and configured in your Mobiflight board. Next, we will bind this new device to a simulator function using a SimConnect event and the Mobiflight WASM module.

Setting up the encoder to control a simulator function

We will use as example how to setup the COM Standby frequency with your encoder, using the pushbutton function of the encoder to switch the encoder configuration from controlling the whole megahertz part of the frequency to controlling the fractional part of the frequency (Khz). For this we need to setup two input configurations for the same encoder, one for the whole Mhz and one for the fractional part. We will use preconditions to activate only one of them at any given time.

First, create an input config (Inputs tab) and name it "COM frequency MHZ" for the whole megahertz part of the frequency. Select the Input tab of the config wizard. In this dialog, you select the module where you installed the encoder, select the device "Encoder" or the name you gave it. Once the encoder is selected, the encoder input settings will appear.

In the "On Left" tab, Select for Action Type, "MSFS2020 - Events". Select the group of presets you wish to use. In this case, "Microsoft/Generic/Radio" and for the Event, select "COM_RADIO_WHOLE_DEC". The DEC indicates the event decreases the frequency by one Mhz.

image

Now press the "On Right" tab. Select the same Action Type "MSFS2020 - Events", the same group "Microsoft/Generic/Radio" and now select from the dropdown list the companion event to the one used above: "COM_RADIO_WHOLE_INC". The INC indicates the event increases the frequency by one Mhz. Press OK and you are done with this step. Ok, you now have an encoder that can set the whole part of the frequency.

We now need to setup the fractional part by creating another config in a similar way. Name this new config "COM frequency Khz". The procedure is the same, so I won't repeat it here. The events to use are named also in a similar way: "COM_RADIO_FRACT_DEC" and "COM_RADIO_FRACT_INC".

image

Setting up a Mobiflight Variable to decide which encoder config is active

You now have two input configurations that use the same device connected to your arduino, but are bound to two different functions in the simulator. They cannot work both at the same time, so we will control which one is active with the encoder pushbutton. This is done by setting up a Mobiflight Variable that changes value from 0 to 1 and back with each button press. We will use this variable in preconditions to activate only one encoder config at a time.

Create a new input config for the encoder pushbutton and let's name it "COM Mhz Khz Swap". In the Input tab, select the device you created for the encoder button. In the On Press tab, select Action Type "Mobiflight - Variable". Name the variable "Mhz Khz Swap". In the value field, put the code

($+1)%2

This will alternate the value between 0 and 1 with each button press. Press OK. You now have a button that assigns an alternating value to the variable.

image

In order to use the Mobiflight Variable, we need to create an output configuration that reads this variable. Let's name it "Mhz Khz Swap" the same as the variable used. Make sure to select the Variable Type as "Mobiflight Variable". The name of the variable has to match the name used in the input config, i.e. "Mhz KHz Swap". Press OK to exit.

image

Setting up preconditions for the encoder configurations

Now let's go back to the encoder input configurations. First, open the whole Mhz config and press the "Precondition" tab. Put a checkmark on the first (only) item of the Precondition list and change the "Use type of" dropdown to "Config Item". Now choose the precondition settings we want this configuration to be active on. Select in Choose config "Mhz Khz Swap". Note this dropdown contains all of the outputs currently defined. Put in "If current value is" equal to "0". Press the "Apply" button, and OK to exit.

image

We repeat the same procedure for the fractional frequency configuration, but setting the precondition for the value of "1". Press "Apply" and OK to exit.

image

Setting up an output variable to read the COM frequency

The last step we need to complete is to configure an output so that we can see the frequency in our panel. This output configuration can be used in an LCD display or 7 segment LED display. Create an output configuration for the COM standby frequency and put this standard variable in it:

(A:COM_STANDBY_FREQUENCY:1, Khz)

image

Mobiflight will read and display the frequency as the simulator changes its value in response to you rotating the encoder.

Cheers!