5 position rotary switch

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

5 position rotary switch

#1 Post by Mikemike »

Hi everyone,

I'm Mike and I'm new. I have read the API wiki, used the search bar and also watched YT until I'm blue in the face. I'm trying to wire a 5 position rotary switch but use resistors "ladder?" so I can use a single analog pin Vs 5 digital pin's. I currently have it working flawless with digital pins, but its just chewing up too many available pins for other assignments. Since I began my project a few months ago, I have succeed in learning basic, Cura CAD design, 3d printing, updating firmware for my printer using VScode, and using the Arduino and AM for simple switch and encoder assignments for my instrument panel.....but for the life of me I can't figure out this script for my ignition switch. The API wiki, although very informative is great, but it's it doesn't seem ignorant friendly for someone like me....I'd never heard of Arduino until a few months ago, LOL. Anyway, with all that said is there a code someone has available that I can use for this to get me at least a foundation? I'm totally lost on this one. I appreciate any help.

Mikemike

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#2 Post by Mikemike »

I'm looking on Wiki and thinking it has something to do with this? "Hw adc input add"....You enter the values and a callback function creates a position change. I'm probably totally off so feel free to laugh, I'm sooo not a programmer, haha.

Mikemike

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

Re: 5 position rotary switch

#3 Post by Sling »

Hi Mikemike,

First of all welcome to the forum.

Secondly, yes you need to use the adc_input_add() function. If you can tell us the analog voltages for each switch position I can help guide you on the code. Essentially you need some if..else logic in the adc callback to action the appropriate ignition control depending on the analog voltage read.

We will need to know what sim(s) this is for.

Tony

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#4 Post by Mikemike »

Tony,

My apologies, it's for the most current version of Xplane. I have not soldered the required resistors to my switch terminals just yet. I "think" I'll be using 1K ohm resistors linked in series between each terminal with a reference of 5V from my Arduino Mega 2560. Figuring out the voltage at each position is yet another hurdle for me. I guess I can measure those values independently after I get the switch soldered up. I appreciate any help you can provide with the code and I will just edit the voltage values after my switch is constructed. In the meantime I'm reading back through the wiki =)


Thanks,
Mikemike

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#5 Post by Mikemike »

I copied and pasted some in AM and came up with this. Values "xx" would represent a number (voltage?) between 0.0- 1.0...which I'm not sure of yet. I'm trying, lol... I know this is probably very simple to most folks familiar with coding.


img_add_fullscreen("switchpanel.png")
img_key = img_add("key.png",47,88,60,65)

--Ignition Key Switch--
-- This function is called every time the ignition switch has a new position voltage value
function adc_input_change(value)
print("The ignition switch position value changed" .. value)


if value == xx then
xpl_command("sim/magnetos/magnetos_off_1")
elseif value == xx then
xpl_command("sim/magnetos/magnetos_right_1")
elseif value == xx then
xpl_command("sim/magnetos/magnetos_left_1")
elseif value == xx then
xpl_command("sim/magnetos/magnetos_both_1")
-- X-Plane requires you to send a begin command (1) to keep the starter engine activated, after that we need to turn it off (0) when going back to BOTH
xpl_command("sim/starters/engage_starter_1", 0)
elseif value == xx then

-- The starter engine will run as long as you keep it in the last position.
xpl_command("sim/starters/engage_starter_1", 1)
end
end

function ignition_callback(key)
if (key[1] == xx) then
rotate(img_key,-60)
elseif (key[1] == xx) then
rotate(img_key,-30)
elseif (key[1] == xx) then
rotate(img_key,0)
elseif (key[1] == xx) then
rotate(img_key,30)
elseif (key[1] == xx) then
rotate(img_key,60)
end
end


-- Create a ADC input
hw_adc_input_add("ARDUINO_MEGA2560_A_A2", adc_input_change)

xpl_dataref_subscribe("sim/cockpit2/engine/actuators/ignition_key", "INT[8]", ignition_callback)

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#6 Post by Mikemike »

ok I changed some stuff in my previous script and I at least have the switch recognized as changing positions and it gives a value of some sort.

INFO - The ignition switch position changed 0.0 (off)
INFO - The ignition switch position changed 0.24731183052063 (right mag)
INFO - The ignition switch position changed 0.33137831091881--------
INFO - The ignition switch position changed 0.49951124191284--------(left)
INFO - The ignition switch position changed 0.66666668653488--------
INFO - The ignition switch position changed 0.75073313713074--------(both)
INFO - The ignition switch position changed 1.0 (start)

I read that the analog input hysteresis# should be 0.1- 1.0...how do you do a range or is that even possible? I see where it fluctuates switching between right mag to left, and then left to both. What a confusing mess, I'm still lost in the sauce, haha.

Mikemike

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

Re: 5 position rotary switch

#7 Post by Sling »

You are certainly on the right track. When posting more than a few lines of code to the forum you should put it in a code box like i have done here as it makes it easier to read.

The adc will never repeatedly return the same adc value every time so you were on the right track with using a value range for each switch position. Something like this would work. You may have to tinker with the values but you get the idea. Remember to take those prints outs when your code is working the way you want.

Code: Select all

function adc_input_change(value)

    if value <= 0.1 then
        xpl_command("sim/magnetos/magnetos_off_1")
    elseif value >= 0.2 and value <= 0.3  then
        xpl_command("sim/magnetos/magnetos_right_1")
    elseif value >= 0.45 and value <= 0.55  then
        xpl_command("sim/magnetos/magnetos_left_1")
    elseif value >= 0.7 and value <= 0.8  then
        xpl_command("sim/magnetos/magnetos_both_1")
        -- X-Plane requires you to send a begin command (1) to keep the starter engine activated, after that we need to turn it off (0) when going back to BOTH
        xpl_command("sim/starters/engage_starter_1", 0)
    elseif value >= 0.9  then
        -- The starter engine will run as long as you keep it in the last position.
        xpl_command("sim/starters/engage_starter_1", 1)
    end
end



For the subscribe callback its simpler to just do this instead of all those if..else iterations.

Code: Select all


rotate(img_key, -60 + (key[1] * 30))


Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#8 Post by Mikemike »

Sling,

Ah, very nice! Yea sorry about the copy text. I see how the way you posted the script would be easier to read and help others with code. I will work though this some more with your example. Thanks for all your suggestions! I've had so much to try and learn over the last few months building my simple Cessna panel I feel like I'm back in college...yuck, haha.


Thanks again!
Mikemike

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

Re: 5 position rotary switch

#9 Post by Sling »

Alternatively you could just use < value at each step and work your way up to the start position or > value and work your way down to the off position. There are a few ways to achieve this.

Code: Select all

if value <= 0.1 then
        xpl_command("sim/magnetos/magnetos_off_1")
    elseif value <= 0.3  then
        xpl_command("sim/magnetos/magnetos_right_1")
    elseif value <= 0.55  then
        xpl_command("sim/magnetos/magnetos_left_1")
    elseif value <= 0.8  then
        xpl_command("sim/magnetos/magnetos_both_1")
        -- X-Plane requires you to send a begin command (1) to keep the starter engine activated, after that we need to turn it off (0) when going back to BOTH
        xpl_command("sim/starters/engage_starter_1", 0)
    else
        -- The starter engine will run as long as you keep it in the last position.
        xpl_command("sim/starters/engage_starter_1", 1)
    end

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: 5 position rotary switch

#10 Post by Mikemike »

Sling wrote: Fri Feb 12, 2021 11:40 pm Alternatively you could just use < value at each step and work your way up to the start position or > value and work your way down to the off position. There are a few ways to achieve this.

Code: Select all

if value <= 0.1 then
        xpl_command("sim/magnetos/magnetos_off_1")
    elseif value <= 0.3  then
        xpl_command("sim/magnetos/magnetos_right_1")
    elseif value <= 0.55  then
        xpl_command("sim/magnetos/magnetos_left_1")
    elseif value <= 0.8  then
        xpl_command("sim/magnetos/magnetos_both_1")
        -- X-Plane requires you to send a begin command (1) to keep the starter engine activated, after that we need to turn it off (0) when going back to BOTH
        xpl_command("sim/starters/engage_starter_1", 0)
    else
        -- The starter engine will run as long as you keep it in the last position.
        xpl_command("sim/starters/engage_starter_1", 1)
    end
Sling,


Very cool!

Question, how did you know the format, for example "elseif value <= 0.55", what if it were elseif value = < 0.55 instead? Are these basics codes found in the Wiki? I could have missed it no doubt.
I can't express how much this is helping me out, I was developing vertigo.

Mikemike
Last edited by Mikemike on Sat Feb 13, 2021 1:15 am, edited 1 time in total.

Post Reply