Change mode (rtl, loiter, auto........) based on certain channels pwm

I need to change flight modes based on some channels pwm. Something like when ch12 is above 1700 change flight mode to loiter.

I need to do this to have a transmitter behave like the joystick buttons do on MP. Basicly when i momentarily toggle pwm of a certain channel above some value than a mode is set and it doesn’t go back to previous mode once the channel is brought back to low and i think mode sticks to what is set regardless of channel if it is set by mavlink (like a GCS would do).

Any help or hint will be greatly appreciated.

plus vehicle:set_mode(MODE_LOITER)

Thanks!!! as a matter of fact i am already cutting and pasting it :slight_smile:

Edit: Done, works great, thanks!!!

function update()
pwm9 = rc:get_pwm(9)
pwm10 = rc:get_pwm(10)
pwm12 = rc:get_pwm(12)
pwm13 = rc:get_pwm(13)
if pwm9 > 1700 then
vehicle:set_mode(3)
elseif pwm10 > 1700 then
vehicle:set_mode(5)
elseif pwm12 > 1700 then
vehicle:set_mode(6)
elseif pwm13 > 1700 then
vehicle:set_mode(2)

end
return update, 100
end
return update, 500

@Eosbandi Do you see anything wrong with it?