How to read the potentiometer value before it is moved for the first time

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
brodhaq
Posts: 152
Joined: Wed Jun 29, 2016 4:13 pm

How to read the potentiometer value before it is moved for the first time

#1 Post by brodhaq »

Hello,
strange issue here. I am using the following code to control my panel backlight using a potentiometer connected to Arduino which is connected to AirPlayer Raspberry.

Code: Select all

function LIGHTING_pots()
	POT_LIGHT_circuit_1 = hw_adc_input_read(LIGHT_circuit_1)
	POT_LIGHT_circuit_2 = hw_adc_input_read(LIGHT_circuit_2)
	POT_LIGHT_circuit_3 = hw_adc_input_read(LIGHT_circuit_3)
	xpl_dataref_write("whateverdataref...", "INT", var_round(POT_LIGHT_circuit_1))
	xpl_dataref_write("whateverdataref 2...", "INT", var_round(POT_LIGHT_circuit_2))
	xpl_dataref_write("whateverdataref 3...", "INT", var_round(POT_LIGHT_circuit_3))
end

LIGHT_circuit_3 = hw_adc_input_add("ARDUINO_MEGA2560_A_A4", LIGHTING_pots) --POTENTIOMETER LIGHTING CIRCUIT III
LIGHT_circuit_2 = hw_adc_input_add("ARDUINO_MEGA2560_A_A5", LIGHTING_pots) --POTENTIOMETER LIGHTING CIRCUIT II
LIGHT_circuit_1 = hw_adc_input_add("ARDUINO_MEGA2560_A_A6", LIGHTING_pots) --POTENTIOMETER LIGHTING CIRCUIT I
It works just great, but only after the potentiometer is moved for the first time after the x-plane starts. If I position the potentiometer to for example 50% and restart x-plane, the dataref is initialized at 100% value (which is a default SASL behavior in this case), and until I first touch the potentiometer, the light is shining like crazy. After I first move the potentiometer by a tiny bit, it immediately jumps to the correct value.

Is there any code I could use to force Air Player to reload the potentiometer values without me touching them?

Thank you!
Pavel
Pavel Brodský
Prague, Czech Republic

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

Re: How to read the potentiometer value before it is moved for the first time

#2 Post by Keith Baxter »

Hi

You are using "hardware" you need to right to the sim the value of hardware on initialization.

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 

JackZ
Posts: 2264
Joined: Mon Feb 22, 2016 1:02 pm

Re: How to read the potentiometer value before it is moved for the first time

#3 Post by JackZ »

Hi.

Try to add an initial call to
LIGHTING_pots()
at startup early in your code, to force a hardware read.

The following calls to the function will be done when operating the pot.

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

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

Re: How to read the potentiometer value before it is moved for the first time

#4 Post by Keith Baxter »

JackZ wrote: Wed Mar 24, 2021 4:04 pm Hi.

Try to add an initial call to
LIGHTING_pots()
at startup early in your code, to force a hardware read.

The following calls to the function will be done when operating the pot.

Jacques
Yes my point.

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
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: How to read the potentiometer value before it is moved for the first time

#5 Post by Sling »

Pavel,

The hw_adc_input_add() callbacks should be independent. They each return their own value as an argument to the callback function. You can then do as the guys have suggested and use the read function to do a single read of the pot positions and write to the datarefs during instrument start.

What you have with the start up mod added will work but why read and update all pots every time when only one changes at a time.

Tony

JackZ
Posts: 2264
Joined: Mon Feb 22, 2016 1:02 pm

Re: How to read the potentiometer value before it is moved for the first time

#6 Post by JackZ »

@Sling Good point, having three separate functions allows to operate each pot individually
@brodhaq Your code would be better as follow:

Code: Select all

function LIGHTING_pot_1()
	POT_LIGHT_circuit_1 = hw_adc_input_read(LIGHT_circuit_1)
	xpl_dataref_write("whateverdataref...", "INT", var_round(POT_LIGHT_circuit_1))
end

function LIGHTING_pot_2()
	POT_LIGHT_circuit_2 = hw_adc_input_read(LIGHT_circuit_2)
	xpl_dataref_write("whateverdataref 2...", "INT", var_round(POT_LIGHT_circuit_2))
end

function LIGHTING_pot_3()
	POT_LIGHT_circuit_3 = hw_adc_input_read(LIGHT_circuit_3)
	xpl_dataref_write("whateverdataref 3...", "INT", var_round(POT_LIGHT_circuit_3))
end

-- initialization of pot values at startup --
LIGHTING_pot_1()
LIGHTING_pot_2()
LIGHTING_pot_3()
--rest of the code...

-- Hardware declarations
LIGHT_circuit_3 = hw_adc_input_add("ARDUINO_MEGA2560_A_A4", LIGHTING_pot_1) --POTENTIOMETER LIGHTING CIRCUIT III
LIGHT_circuit_2 = hw_adc_input_add("ARDUINO_MEGA2560_A_A5", LIGHTING_pot_2) --POTENTIOMETER LIGHTING CIRCUIT II
LIGHT_circuit_1 = hw_adc_input_add("ARDUINO_MEGA2560_A_A6", LIGHTING_pot_3) --POTENTIOMETER LIGHTING CIRCUIT I
Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: How to read the potentiometer value before it is moved for the first time

#7 Post by Sling »

Yes like that other than you need to declare the hardware before trying to read from it. So swap those last couple of blocks.

Post Reply