Can LUA code make this switch work?

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Can LUA code make this switch work?

#1 Post by Kaellis991 »

I have a 3 position sliding switch, ON-OFF-ON
It's easy to program a switch hardware script to connect the two ON positions.
However, I need a 3-way switch for those (3) modes on my X-plane DME radio.

Is there a way to write the LUA code so that it will write to the dataref (INT 1) when the switch is in the OFF position and no terminals are connected?
Perhaps with some sort of boolean operation where when the two outer terminals are not connected the code will send a signal to a pin for the FREQ mode?

If not I will I will figure out something else.


image.png
image.png

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

Re: Can LUA code make this switch work?

#2 Post by Ralph »

You don't have to write it yourself, at least only the easy part. There's a premade hardware function for this.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Can LUA code make this switch work?

#3 Post by Kaellis991 »

Ralph wrote: Thu Sep 15, 2022 8:15 pm You don't have to write it yourself, at least only the easy part. There's a premade hardware function for this.

I found a Generic ON-OFF-ON hardware switch that I downloaded, but I can't find anything with DME in the name to download

This hardware only has two pins. Dont I need (3) pins for 0, 1 or 2? 1 being the center FREQ position.
image.png
image.png
Last edited by Kaellis991 on Thu Sep 15, 2022 11:32 pm, edited 1 time in total.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Can LUA code make this switch work?

#4 Post by Kaellis991 »

Am I going in the right direction here?

If this is the correct approach, I know that I dont have the elseif not and the positions correct
image.png

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Can LUA code make this switch work?

#5 Post by Kaellis991 »

This is what makes sense to me....on a basic logic level.

if not input 1 and input 3 then write the INT 1 to the dataref
elseif read input 1 write the INT 0 to the dataref
elseif read input 3 write the INT 2 to the dataref

I am just stumbling around here.....

That third pin connection doesnt make sense. Where does it connect when there are only (2) outputs on the switch.

There should be a wire run from the arduino GND to the center terminal on the switch and then a wire from the (2) outer terminals to an input pin.
I dont know how to get a third input wire / input pin with this switch.

My guess is I have to buy a 3-way switch.

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

Re: Can LUA code make this switch work?

#6 Post by Ralph »

I'm not sure what you're doing. Why did you change the script? All you have to do is add your dataref writes to the script.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Can LUA code make this switch work?

#7 Post by jph »

The thing you downloaded will work Kirk.
The '3 position switch' has two active terminals only. One is grounded. In the case of your switch it is the centre terminal, so you only have 2 terminals left.

On a previous reply a someone vaguely pointed out what the pullup and pulldown were, well from that you must consider that the Arduino has the port pin pulled up to 5V internally (they have this ability) and so to make the input active for AM then you 'pull' it low meaning your switch connects that pin to ground.
The gnd here is attached to your centre pin.

If you had an on/off switch with 2 pins then it is a 'two way' switch. It has two states, on and off. Only one pin connects to the arduino though.
The number of pins in use would normally be switch 'number of ways' -1

All you needed to add to that example was the appropriate call to whatever you are trying to do.

Are you not actually connecting the switch up to the example and running the code ? - that is what the prints are for, to show you the code in operation. You can then change it around at will and see what happens.

If you think about the way you are trying to connect the switch - with THREE pins, then how are you possibly going to pull any of the inputs to gnd to signal to AM that the switch is active ?
Joe
Joe. CISSP, MSc.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Can LUA code make this switch work?

#8 Post by Kaellis991 »

I havent actually hooked up the code to an arduino yet. I started soldering wires to the slide switch terminals to connect it to the arduino and thought I needed to have a GND wire to the center terminal and (3) wires from the other terminals to go to (3) pins on the arduino. One pin for each of the DME modes...RMT, FREQ, and GS/T.
I'm still a little confused about the concept of "pulling up".
When I wire a light switch in my house I understand that the switch closes a circuit when switched and the electricity flows through the light bulb.
For the other on/off switches in my sim, its been my understanding that each one will close the circuit and connet GND to a pin. Same as any of the 5 way rotary switches are connected where there is a GND connected to the common leg and each of the 5 positions connect to a discrete and separate pin, as in my starter switch. Each position has its own pin.
Doesnt every switch position need to have a corresponding pin to create a closed GND circuit to a pin?
Ive been looking at this switch like any other, where each position has to connect GND to a pin.
Thus when the switch is placed in the center position its not connecting anything...no connection of GND to a pin.

Where am I failing in my understanding about making these connections? I'm visualizing these switches in the same way I understand switches in my house or on my boat. Close the circuit and the electricity flows from battery ground to battery positive through the load. So that each load needs its own pathway to and from the battery.

Its hard for me to picture how that center position connects a GND to a pin.
Obviously the picture in my mind's eye is faulty.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Can LUA code make this switch work?

#9 Post by jph »

Ah, well, first thing is forget switches on boats or houses as apart from a contact inside there is little similarity.

Think about an arduino pin set as an input.
It is waiting for a signal, either 'high' or 'low' where 'high' is the voltage level of the arduino supply, and low is GND. The arduino only understands two states on a digital input, high, or low, nothing else.

When you set it as an input and try to read it, with nothing connected, then what will the value be ? - answer - anything .. it is 'floating'. It is neither high, nor low and will vary depending on many conditions - even temperature and proximity. Computer digital inputs NEED to be either on or off (high or low)

So, we MUST set the input pin to a certain state, either high or low.
Let's presume we want the pin to be normally reading 'high' without anything connected or any action being taken. What we could do is add a resistor, let's say about 10 to 20K ohms, from the pin, to the arduino supply (vcc), then, when we now read the pin, with nothing else connected, it will ALWAYS read 'high' as we have 'pulled it up' to vcc via a reasonably high value resistor.

Now, lets connect a wire to the pin - a dupont cable maybe. to the PIN. The pin is as before, it is 'pulled up' to vcc via the 'pullup' resistor.
If we now connect the other end of the dupont cable to GND then what happens at the pin ?
Well, the pin is now 'pulled LOW' and the voltage is dropped across the resistor.
The resistor is quite high value as current is now flowing through it to GND. But due to the value it is minimal so no heat or losses to talk of.

The arduino and most microprocessors are smart though. They include the 'pull up' resistor in the design of the chip inputs and it is selectable in code if it is there or not.
We call this WPU (weak pull ups).
All you need to worry about though is that when you set an input in AM, it is usually set with WPU enabled in code that you have no access to.
This means that you (usually) do not need to add the resistor externally.

So to connect a simple toggle switch then one pin goes to an arduino pin and one pin goes to GND.
The arduino pin is 'pulled up' via its internal WPU resistor and with the switch open the pin will read high. With the switch closed the pin will be 'pulled down' to GND via the WPU and the switch contact being closed so we will read LOW when the switch circuit is made (in the normal electrical sense of the switch operation).

The concept of ON and OFF have no meaning - for a switch - in code. We only have HIGH and LOW logic levels at the pins. What we do with that / those is 100% up to you and what you do with the code.

If you want to turn on an LED with the switch in the electrical 'on' (closed) position then you simply monitor the input pin that the switch is connected to and if we want to, then when the pin is LOW (as it is normally 'pulled up' via the internal WPU) w can signal the led to turn on by making an output pin either high or low depending on how the LED is connected - as we can connect an LED in source or sink mode.
If we decide we want the led to be on when the switch is 'electrically open' then we turn on the led, in code, when the pin reading is HIGH (meaning the switch is OPEN) and we turn it OFF when the switch is electrically CLOSED - at which time the input pin will read LOW.

A 3 position switch - as in what you have, only needs two pins. The centre goes to ground. The input pins are 'pulled up' internally by the WPU in the micro. so read HIGH with nothing connected.
The two pins on the switch (the outer 2) are connected to input pins.

Now on those 2 pins you have 4 (FOUR) possible state (Simple Binary) that they can read but we are limited to 3 because of the mechanical design if the switch (hence our 3 way)

those states are
LOW LOW
LOW HIGH
HIGH LOW

You cant have HIGH HIGH ! - the switch will not allow that unless you crush it in a vice.

What you decide to do with those input in code is entirely up to you.
You have 3 logic states as above.

Use them however you want - If you want the switch to be OFF when at one end and then ON ON then you can use the logic as
HIGH LOW = OFF
LOW LOW = ON
LOW HIGH = ON 2

Do whatever you want with it.
If you look at the code, that is all it is doing, doing simple logic assessments on the input state of 2 pins.

havent checked typos - TLDR

Joe ;)
Joe. CISSP, MSc.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Can LUA code make this switch work?

#10 Post by Kaellis991 »

Joe / Ralph,

Thanks for the help.
Joe,
Your description of the HIGH LOW concept of the arduino inputs, is the best explanation I have seen yet.
I will have to read through it a number of times and work on forming an internal visual to help me fully understand the concepts, but you have helped fill in some of the puzzle pieces.
Eventually I will get the entire puzzle put together to see the whole picture.

But for now, I added those datarefs to this code, plugged in the arduino, soldered the wires to the switch and connected them to the arduino pins and surprise, it's working.
I dont fully understand it conceptually, but that is what I need to work on.

Now I have to create the simple ON/OFF switch for that DME radio.

Kirk

image.png

Post Reply