Reading Simultaneous Button Presses

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

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

Re: Reading Simultaneous Button Presses

#21 Post by jph »

Hi Brain -
LUA is absolutely not my speciality. I am sure the seasoned programmers will easily offer an option.
There are logical operator and relational operators
Check the relational operators as in -
https://www.tutorialspoint.com/lua/lua_operators.htm

Using relational operators -
You would - I believe, need to first check flag is > 0 - as in something like IF flag A > 0 then
then compare flag a to flag b, as in if flaga == flagb then
Also, you need to clear the flags on 'button release' function otherwise they will always remain set.
but, certainly the use of logical operators and relational operators needs to be something to get your head around.

Perhaps @Sling or others may clarify the programming basic outline of what you are trying to do ? in an example- as said, I am certainly no lua aficionado - not even close. it leaves me cold in a lot of areas :) It is something I will delve deeper into only when I need to.


---------------------------------------------------------------
As for the XOR example above. forget LUA etc for the moment, it is simply a boolean example.
To help things -

When I say compare two bytes (at a bit level) the if byte a. =
bit7 bit6 bit5 bit 4 bit3 bit2 bit1 bit 0
0 1 1 0 1 0 0 1

and byte b =
bit7 bit6 bit5 bit 4 bit3 bit2 bit1 bit 0
1 1 0 0 0 1 1 1

then the use of the truth table for something like XOR is used PER bit - as in the result of BIT0 of byte a XORed with BIT0 of byte b
so here, 1 xor 1 is 0

then move to BIT1 of both bytes, then BIT2 etc etc
SO the xor of BIT1 so 0 xor 1 = 1
Leave that one for later maybe;)
Joe. CISSP, MSc.

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

Re: Reading Simultaneous Button Presses

#22 Post by Sling »

Brian,

You were close but your if comparison will not do anything where you have it placed. It needs to be a little more like this. Please when posting code to the forum use the </> code button and paste your code between the code tags to get a code box like I have here. I just makes things easier to read and you will more likely get help.

A few more general tips. Use meaningful names for your variables as flag1 as an example will not mean a lot in a few months time. Remove all those prints once you are happy the code is working as you want. They slow code execution. You don’t need the <> around the Arduino channel ID.

Code: Select all


local sel_pressed_flag = false
local ctrl_pressed_flag = false

function sel_pressed()
    print ("sel button pressed")
    sel_pressed_flag = true
    sel_ctrl_both_pressed_check()
end

function sel_released()
    sel_pressed_flag = false
    —do the single sel button action here
end

function ctrl_pressed()
    print ("ctrl button pressed")
    ctrl_pressed_flag = true
    sel_ctrl_both_pressed_check()
end

function ctrl_released()
    ctrl_pressed_flag = false
    —do the single ctrl button action here
end

function sel_ctrl_both_pressed_check()
    if sel_pressed_flag and ctrl_pressed_flag then
        print ("both buttons pressed")
    end
end

hw_button_add("ARDUINO_MEGA2560_A_D38", sel_pressed, sel_released)
hw_button_add("ARDUINO_MEGA2560_A_D39", ctrl_pressed, ctrl_released)


bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Reading Simultaneous Button Presses

#23 Post by bdcurry »

This is all great stuff -- can't thank you guys enough for the support, information and dialog. I tried the code sample and it works but will need some adjustment to replicate the function (I'm also looking to use this on the STEC autopilot for the HDG-NAV and ALT-VS functions). Looking at the logic and flag info from Joe, it's making more sense and I think I'm on the right track to combine that with the suggested code to complete the project. Again, can't thank you guys enough for the help and I'll circle back to close the loop on how it works out.

Post Reply