How can I check joystick button input with Lua script?

I recently got to know Lua Script through Yuri’s video and thanks to that, I was able to experiment with the 3 Position switch Demo using a USB gamepad. (I haven’t had a real drone and controller yet)

Next, I wanted to check the button input of the USB gamepad, so I tried the button_test example code, but it doesn’t work as expected.

RC6 is for RCIN test, But 1 is for button_test, USB GamePad and MissionPlanner are connected as above.
(For flight simulation, since RC1~RC4 are connected to the analog direction control stick of the joystick, RC6 and RC3 react simultaneously. RC3 is reversed)

--[[  Testing Environment
PC              Windows10
USB GamePad     Generic USB Joystick like Logitech F310
GCS             MissionPlanner 1.3.80 build 1.3.8479.20539
SITL            ArduCopter V4.5.0-dev(a47e125c)
--[[ Preparing for RCIN Testing
- Set the SCR_ENABLE parameter to 1 in MissionPlanner/CONFIG/Full Parameter List.
- Set the RC6 OPTION parameter to 300 in MP/CONFIG/Full Parameter List.
- Bind throttle stick of usb gamepad to RC6 in MP/Action/Joystick settings
- Confirm RC6 Overriding in MP/SETUP/Mandatory Hardware/Radio Calibration 
local rc_chan = rc:find_channel_for_option(300) 

function rcin_test()
  local sw_pos = rc_chan:get_aux_switch_pos() -- returns 0 or 1 or 2.  3 Position switch
  local pwm = rc:get_pwm(6)
  gcs:send_text(6, string.format('Lua: rc_switch pos %s  pwm %s ',sw_pos,pwm) )

--[[ Preparing for Button Testing
- Bind button 1 of the USB GamePad to But1 in the mission planner joystick settings
- It seems that in MP/SETUP/Mandatory Hardware/Radio Calibration,  
the RC Overriding of button 1 bound in the Joystick settings cannot be confirmed.
local button_number = 1 -- the button numbber we want to read, as deffined in AP_Button
local button_active_state = true -- the 'pressed' state of the button
local last_button_state
function button_test()
  local button_new_state = ( button:get_button_state(button_number) == button_active_state )
  gcs:send_text(6,string.format('Lua: button state: %s',button_new_state))

  -- the button has changes since the last loop
  if button_new_state ~= last_button_state then
    last_button_state = button_new_state
    if button_new_state then
      gcs:send_text(0, "Lua: Button pressed")
      gcs:send_text(0, "Lua: Button released")

function update() -- this is the loop which periodically runs
    return update,1000 -- reschedules the loop (1hz)

return update() -- run immediately before starting to reschedule

--[[  Reference
ArduPilot's Documentation
    Lua Scripts 

GitHub's Lua Script example
    RC IN     

Yuri's ArduPilot Lua Demos 16 Apr 2022  

test.lua (3.0 KB)

I wrote the script as above, The execution result is as follows.
The positions (0,1,2) and pwm values ​​of the GamePad connected to the RC6 can be read normally, but
When button 1 connected to ‘But 1’ is pressed, contrary to expectations, true is not returned, only false is always output.
(The Arm function connected to ‘But 1’ was executed normally, and the flight simulation was possible.)

Is there anything wrong or missing in my attempt to check the GamePad’s button input with Lua Script?
Hope to get some help on how to check it.

“Button” typically refers to a physical button on the vehicle itself.

Your controller button is nothing more than a 2 position RC switch. The 3-position switch demo is easily modified to suit.

Oh, the button is not on the ground (gamepad) but on the sky (drone). I was completely mistaken. Thank you for correcting my misunderstanding
To be honest, I am still confused and embarrassed.

And despite your advice I don’t know how to detect gamepad button input in Lua script.

Should I map button 1 (2-position RC switch?) on the gamepad to RC6 instead of ‘But 1’? However, none of the eight buttons on the gamepad map to RC1-16. I don’t think this is what you said.

I’d appreciate it if you could give me more documents or some hints to refer to.

The gamepad is just a surrogate RC controller, so it will behave like one with respect to the vehicle and script.

The first step is to get the gamepad button to be recognized as an RC channel.

Thereafter, your script should reference that RC channel for operations with it.

Here is the documentation page for using a GCS-connected gamepad:
Joystick/Gamepad — Copter documentation (

Ladies and gentlemen here wouldn’t say that,

I read the documentation several times, afraid that I might hear something called RTFM.

To be honest I don’t know how to do this yet.

Hopefully someone who knows how to do this, including Yuri, will give an answer.