Strange behavior when writing to instrument brightness dataref

Discuss suspected bugs with other users and Sim Innovations Staff

Moderators: russ, Ralph

Message
Author
Pagouti
Posts: 58
Joined: Tue Nov 19, 2019 8:19 pm

Strange behavior when writing to instrument brightness dataref

#1 Post by Pagouti »

Hi,

One of the last knobs on my dashboard that doesn’t work as expected, is the knob to dim the brightness of my G1000 screens.

I use an ordinary 10k linear potmeter on my dashboard. I read and apply its value with the following code:

Code: Select all

function hw_instrument_brightness_knob_change(hw_instrument_brightness_knob_state)
    hw_instrument_brightness_knob_state = math.min(64.06158264 * hw_instrument_brightness_knob_state, 1.0)
    print("hw_instrument_brightness_knob_state = " .. tostring(hw_instrument_brightness_knob_state))
    xpl_dataref_write("sim/cockpit2/electrical/instrument_brightness_ratio_manual", "FLOAT[32]", hw_instrument_brightness_knob_state)
end
hw_instrument_lighting_knob = hw_adc_input_add("ARDUINO_MEGA2560_A_A5", hw_instrument_brightness_knob_change)
Reading goes fine. According to the documentation I would expect values ranging fom 0.0 to 1.0, but in order to obtain that, I have tp multiply by 64 something.

Writing to the dataref sim/cockpit2/electrical/instrument_brightness_ratio_manual is troublesome. First, using the dataref tool, I have seen values ranging from 0.0 to 0.0015. Later on, no output at all was visible.

What could possibly be wrong with the above code? Grateful for any solution!

Han Schouten

User avatar
Keith Baxter
Posts: 4826
Joined: Wed Dec 20, 2017 11:00 am
Location: Gauteng

Re: Strange behavior when writing to instrument brightness dataref

#2 Post by Keith Baxter »

Hi,

This is a simple code to write to a dataref.

NOTE "x" in the code is the index in the array -1

Code: Select all

hw_instrument_lighting_knob = hw_adc_input_add("ARDUINO_MEGA2560_A_A5",function(val) 
   xpl_dataref_write("sim/cockpit2/electrical/instrument_brightness_ratio_manual", "FLOAT[32]",{val},X)
end)
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Ralph
Posts: 8357
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Strange behavior when writing to instrument brightness dataref

#3 Post by Ralph »

You're writing to an array in a incorrect manner.

Pagouti
Posts: 58
Joined: Tue Nov 19, 2019 8:19 pm

Re: Strange behavior when writing to instrument brightness dataref

#4 Post by Pagouti »

Hi Keith

Thanks for your code example. I have tried it out and - miraculously - the multiplication by 64.something is no longer necessary. Closer inspection of the dataref I have used, revealed that it is read-only. I found another dataref that seems to work. This is, what I have made of it:

Code: Select all

-- A5: Draaiknop instrumentverlichting -----------------------------------------------------------------

hw_instrument_brightness_knob = hw_adc_input_add("ARDUINO_MEGA2560_A_A5", function(val)
    xpl_dataref_write("sim/cockpit/electrical/instrument_brightness", "FLOAT", val)
    print("hw_instrument_brightness_knob_state = " .. tostring(val))
end)


-- A6: Draaiknop dashboardverlichting ------------------------------------------------------------------

hw_panel_brightness_knob = hw_adc_input_add("ARDUINO_MEGA2560_A_A6", function(val)
    xpl_dataref_write("sim/cockpit2/switches/panel_brightness_ratio", "FLOAT[4]", {val}, 0)
    print("hw_panel_brightness_knob_state = " .. tostring(val))
end)
There is another issue that might have something to do with old, worn potmeters. The visual result, in the 3D-cockpit as well as on my dashboard, is a extremely jittery. Even when I don’t touch the knobs on my dashboard, my Arduino is working overhours to process all the changes that the dataref picks up. The G1000 screens flicker from entirely black to the brightness that I would like to see. Another knob (same type of potmeter) for the panel illumination shows a similar behaviour. Have you ever seen something similar? Is there a way to suppress these unwanted variations of dataref values? Or should I simply buy new potmeters?

Greetings,

Han.

User avatar
Keith Baxter
Posts: 4826
Joined: Wed Dec 20, 2017 11:00 am
Location: Gauteng

Re: Strange behavior when writing to instrument brightness dataref

#5 Post by Keith Baxter »

Hi,

I think there was a solution for jumpy potmeters. I cant rem where or what it was. I might be confusing encoders.

Maybe @Ralph or @SimPassion has a pointer.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Ralph
Posts: 8357
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Strange behavior when writing to instrument brightness dataref

#6 Post by Ralph »

The Arduino doesn't write to the dataref, so it'll be okay ;)
You can increase the hysteresis value, but you'll get 'steps'.
See argument 2: https://siminnovations.com/wiki/index.p ... #Arguments

It might also be that two 'systems' are writing to the same dataref, so you're watching those fight each other.

SimPassion
Posts: 5647
Joined: Thu Jul 27, 2017 12:22 am

Re: Strange behavior when writing to instrument brightness dataref

#7 Post by SimPassion »

Hi Han @Pagouti and Keith @Keith Baxter, we got unstable behavior with jerky potentiometers on Arduino and obtained a nice result by reducing potentiometer voltage input from 5V to 3.3V

User avatar
Ralph
Posts: 8357
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Strange behavior when writing to instrument brightness dataref

#8 Post by Ralph »

That'll also lower your maximum value, so you will have to compensate for that. Unless you put 3v3 on the AREF.

SimPassion
Posts: 5647
Joined: Thu Jul 27, 2017 12:22 am

Re: Strange behavior when writing to instrument brightness dataref

#9 Post by SimPassion »

Ralph wrote: Mon Dec 18, 2023 9:39 pm That'll also lower your maximum value, so you will have to compensate for that. Unless you put 3v3 on the AREF.
Indeed Ralph, a ratio was applied
The initial issue was interference with PWM device still on 5V

All solved in similar way :

Code: Select all

-- #A0 - Analog Input - ARDUINO MEGA2560 Channel A - Mixture Axis
-- log("INFO","MIXTURE AXIS")
function pr_mixture_axis(value)
	-- log("INFO","MIXTURE AXIS: "..value*1.52)
	si_variable_write(gbl_ap_hw_status, str_fill_in.."MIXTURE AXIS: "..value*1.52)
	xpl_dataref_write("sim/cockpit2/engine/actuators/mixture_ratio_all","FLOAT",value*1.52)
end

hw_adc_input_add("Mixture/Richesse", pr_mixture_axis)
or here through interpolate feature :

Code: Select all

-- #A4 - Analog Input - ARDUINO UNO Channel A - Rudder Trim

local interp_ref_rtrim = { { 0.0, -1 },
			{ 1.0, 1 } }

-- log("INFO","UNO A4 RUDDER TRIM AXIS")
function pr_rud_trim_axis(value)
	-- log("INFO","UNO A4 RUDDER TRIM AXIS INP: "..value)
	si_variable_write(gbl_ap_hw_status, str_fill_in.."UNO A4 RUDDER TRIM AXIS: "..tostring(value))
	-- value = value * -1
	value = interpolate_linear(interp_ref_rtrim, value)
	-- log("INFO","UNO A4 RUDDER TRIM AXIS INTERP: "..tostring(value))
	-- si_variable_write(gbl_ap_hw_status, str_fill_in.."UNO A4 RUDDER TRIM AXIS: "..tostring(value))
	xpl_dataref_write("sim/cockpit2/controls/rudder_trim","FLOAT",value)
end

hw_adc_input_add("UNO A4 Rudder Trim Axis", pr_rud_trim_axis)

-- #A0 - Analog Input - ARDUINO UNO Channel A - Elevator Trim

local interp_ref_etrim = { { 0.0, -1.08 },
			{ 1.0, 1.671 } }

-- log("INFO","UNO A0 ELEVATOR TRIM AXIS")
function pr_elev_trim_axis(value)
	-- log("INFO","UNO A0 ELEVATOR TRIM AXIS INP: "..value)
	si_variable_write(gbl_ap_hw_status, str_fill_in.."UNO A0 ELEVATOR TRIM AXIS: "..tostring(value))
	-- value = value * -1
	value = interpolate_linear(interp_ref_etrim, value)
	-- log("INFO","UNO A0 ELEVATOR TRIM AXIS INTERP: "..tostring(value))
	-- si_variable_write(gbl_ap_hw_status, str_fill_in.."UNO A0 ELEVATOR TRIM AXIS: "..tostring(value))
	xpl_dataref_write("sim/cockpit2/controls/elevator_trim","FLOAT",value)
end

hw_adc_input_add("UNO A0 Elevator Trim Axis", pr_elev_trim_axis)

User avatar
Keith Baxter
Posts: 4826
Joined: Wed Dec 20, 2017 11:00 am
Location: Gauteng

Re: Strange behavior when writing to instrument brightness dataref

#10 Post by Keith Baxter »

Hi,

The first thing I would try is as Ralph says, playing around with the hysteresis and adding a 1 µf capacitor. We used to do this many decades ago when building Audio amplifiers.
If that does not work, then fiddle around with filtering.
A capacitor between the analog input and ground may help to reduce noise pickup, especially if the pot is 10K or more and/or if the leads are long and unshielded (try 0.1uF or greater). You can also try software smoothing 289.

If it's jumping up & down by 1 count, that can be normal if you are on the "hairy edge" between two numbers. That's just the nature of analog-to-digital conversion. You can handle that by ignoring changes less than 1 or 2 counts, etc.
This is also a good read.

https://www.norwegiancreations.com/2015 ... filtering/

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

Post Reply