-
-
Notifications
You must be signed in to change notification settings - Fork 109
Using encoder to drive a value back and forth within a given range
For many things, expecially in MSFS2020, but also in X-plane, there are presets that are tailored for a specific input value, and they have a built in "wrap around" for a value, like a heading selector wraps around to 0 when you increase the value from 359, or the altitude preselect might have some reasonable lower and upper limits.
But what if you need to have a range you define yourself, or if you need to input a value in the sim that does not have an INC and DEC event, but just a "set" event that just expects an input value and just expects it to be in the correct range? Or if you need to increase or decrease the value by more than just one, but the event code in the simulated aircraft does not expect the input parameter to be something else than 1 or -1?
This is when it might be handy to use a transform modifier, but this is not possible for MSFS2020 or X-plane presets.
We are using a MobiFlight variable to store our trim setting, and safeguarding that the value stays within our desired range. MobiFlight Variable input allows for a Transform modifier in the input side, and we can make sure we are not trying to adjust the trim beyond the allowed range. We divide our problem into two halves and conquer each of them separately.
Tip
MobiFlight variables are a way to store information that is not sent to the simulator, but can be read by other configuration entries later. A bit like a postit note you might leave on your fridge door to let others know there is leftover pizza if someone is hungry. Whoever needs this information can look at the note and read what's stored in it.
Here are the requirements:
- Our trim value needs to be between -100 and 100
- Each click of the encoder should increment or decrement the value by one
So lets create an input configuration for our encoder first. We need to give our variable a name, lets call it RudderTrimSetting. This can be whatever you want to use.
Notice that under the "Value" field, it reads "Supports variable value($), input value (@) and placeholders (?,#, etc.)". This field also works like the "Transform" modifier in Output configs. So we can also write some NCalc formulas. NCalc is a bit like the formulas that you can input on a spreadsheet cells, and we are going to make a logical IF here. In these fields "$" means the current value of the config, and we will make the following formula to the "On Left" field:
if($ > -100, $-1, -100)
This basically is a if(check, then, else) function that checks IF our value ($) is greater than (>) -100 (our lowest limit), THEN decreases the value by one. ELSE it just sets the value to the lowest limit of -100. So we set it on the config:
We can then click the "Copy" button, and select the On Right tab to do the same for the other direction of rotation, but instead if decrementing the value, we increment:
if($ < 100, $+1, 100)
So we check if we are at the upper limit already, and if not, we add one. For more thorough explanation of NCalc syntax and what functions are available, please see the NCalc documentation.
To see our MobiFlight variable's value, we need to create an Output config for it. Add a new output config, and set its Sim Variable to "MobiFlight Variable" and pick our RudderTrimSetting from the dropdown list. When you press "Run" in MobiFlight, you can test the variable.
Here we can see that by turning the encoder left, to decrease the value, the lowest we can reach is -100 as we wanted. And the same works for turning right.
The first part, like we did above, to see our value is super simple, we just set the Sim Variable to point to our MobiFlight Variable:
We could show this value on a 7 segment display, but it would not make much sense in terms of flying the aircraft, so we need to do something else. And here we use a bit of a shortcut, that might feel strange at first, since we are in a Output configuration. We want, whenever the MobiFlight variable value changes, to send the value to the simulator's trim. So we select the Input Action type for our display. This basically creates an input that we can send to the simulator, and since we have already taken care of our value's range limits, we can just use a "set value" event preset, "Rudder Trim Set" in this case. I searched for "trim" and used the "Generic" aircraft to narrow down the search results, and picked this one that does expect the range in the -100 to 100 range like we set things up with.
There is just one issue - our trim valuye is already neatly contained within the -100 and 100 range, and the preset code expects wrong, as you can see in the screenshot marked with red.** So click the "Show preset code" checkbox**, and we need to edit the code to be just this:
@ (>K:RUDDER_TRIM_SET)
And everything should work. Let's test!
So it worked great, except that in the MSFS2020 Asobo Beechraft Baron the rudder trim seems to move left when the value increases, and right when it decreases, so my encoder works backwards. This can be solved by swapping around the OnLeft and OnRight events for example.
And if you want to increment faster or slower, just adjust the code in the OnLEft and OnRight events to add and subtract 2 or 4 for example instead of 1.
- MobiFlight Connector Installation
- Mobiflight Connector BETA version installation
- Modules
- MobiFlight Connector Files Structure
- MobiFlight Connector Uninstall
- Modules Reset to factory default
- Verifying the WASM module installation and locating the MSFS2020 community folder
- Verifying the WASM module installation and locating the MSFS2024 community folder
- Using a Winwing FCU with MobiFlight
- Using VKB controllers with MobiFlight
- Providing logs from MobiFlight
- MobiFlight Connector How does it work
- Mobiflight Connector Main Window
- Flash module with MobiFlight firmware
- Input and Output devices
- Joysticks
- Midi Boards
- Sim Variables (for Output)
- Input Actions
- Merging configuration files
- Disabling specific COM ports
- Examples Output LEDs
- Examples Input Switch
- Example 7 segment display
- Example Servo motor
- Controlling LEDs with an output shift register
- Adding lots of buttons with an input shift register
- Beginner's guide to input multiplexers
- Key Matrix with standard MobiFlight and Multiplexers
- Tutorial Easy Driver and x.27 or x.40 Stepper Motor
- Tutorial for Airbus VS display via 7-Segment LED Module
- Example Analog Input Potentiometer
- Baron G58 Tutorial Gear, Flaps, Mags, ELT Input Output Programming
- Using Mobiflight to control arduino-based 3rd party panels (RealSimGear GNS530)
- How to use a VNH2SP30 DC motor shield with MobiFlight
- Using 3D printer mainboards
- Playing sounds by sending keystrokes to AutoHotKey
- Using the selector knob on a Honeycomb Bravo
- Using an adjustable 12 position switch as a GA starter
- Brightness of LCD displays with I2C
- Using three-position switches
- Transponder with one Rotary
- Workflow for Creating Flight Simulation Panels ‐ Part 1
- MSFS2020 RPN Tips and Tricks
- MSFS2020 Using the Custom Input Code Box
- MSFS2020 Install WASM module and Event List
- MSFS2020 How to Create and Use User Defined Lvars
- MSFS2020 How to Create a Blinking LED configuration
- MSFS2020 User Defined WASM Module Events Best Practices
- MSFS2020 Developer Mode, Model Behavior dialog and Console window
- MSFS2020 PMDG 737‐700 List of Events that require use of FSUIPC7
- MSFS2020 PMDG 737‐700 Calibrate throttle idle and reverse thrust using interpolation (Valkyrie)
- MSFS2020 PMDG 737-700 Chrono unit functions implemented in Mobiflight
- Configuring PMDG 737 Parking Brake Lever Auto-Release with a Servo in Mobiflight
- Using encoder to drive a value back and forth within a given range
- Adding a custom board to MobiFlight
- User guide - Community Board and Custom Devices
- Developing your own custom devices/boards