Another problem with the button box

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Post Reply
Message
Author
SteveBurney
Posts: 33
Joined: Fri Mar 04, 2022 2:13 am

Another problem with the button box

#1 Post by SteveBurney »

Hi Again,
I've built a button box with 2 rotary encoders and several buttons to 'tell' the encoders what to do and LEDs so that I know what it's doing. Here is the code:

Code: Select all

--Button Box Logic
--When a button is pressed, the corresponding LED will illuminate
--and the function will be active on the rotary  
              
    local BP = 0
-------------- Buttons --------------------------    

--Button 3
    function BP3()
      if BP == 3 or BP == 7 then
         BP = 0 
         hw_led_set(led7, 0.0)
         hw_led_set(led3, 0.0)
         else
         BP = 3
         illumination(led3)
         rot1()
      end
    end
    
    --Button 4
    function BP4()
      if BP == 4 then
         BP = 0 
         hw_led_set(led4, 0.0) else
         BP = 4
         illumination(led4)
         rot1()
      end
    end
    
    --Button 6
    function BP6()
      if BP == 6 or BP == 8 then
         BP = 0 
         hw_led_set(led6, 0.0) 
         hw_led_set(led8, 0.0)
         else
         BP = 6
         illumination(led6)
         rot1()
      end
    end
    
                                                    --Top Rotary Button
    function tpress()
      if BP == 0 then
        si_command("si/g5/1/knob_button", "BEGIN")
      end
     
      if BP == 3 then
         BP = 7 
         illumination(led7)
         elseif
         BP == 7 then
         BP = 3
         illumination(led3)
         rot1()            
      end
       
      if BP == 6 then
         BP = 8 
         illumination(led8)
         elseif
         BP == 8 then
         BP = 6
         illumination(led6)
         rot1()            
      end 
      
    end

    function trelease()
      if BP == 0 then   
         si_command("si/g5/1/knob_button", "END")
      end 
 
    end

                                                   --Bottom Rotary Button
   function bpress()
     if BP == 0 then    
         si_command("si/g5/2/knob_button", "BEGIN")
     end 
     
     if BP == 3 or BP == 7 then
         fsx_event("NAV2_RADIO_SWAP")
         xpl_command("sim/radios/nav2_standy_flip")
     end 
     
     if BP == 6 or BP == 8 then     
        fsx_event("COM2_RADIO_SWAP")
        xpl_command("sim/radios/com2_standy_flip")
     end 
           
   end
   
   function brelease()
     if BP == 0 then   
        si_command("si/g5/2/knob_button", "END")       
     end
    
   end      
          
-------------- LED illumination ------------------
    function illumination(light)
      hw_led_set(led3, 0.0)
      hw_led_set(led4, 0.0)
      hw_led_set(led6, 0.0)
      hw_led_set(led7, 0.0)
      hw_led_set(led8, 0.0)
      
      hw_led_set(light, 0.5) 
    end

-------------- Rotary functions ------------------

   
   function rot1(direction)
      
      --Default behavior-control G5PFD
      if BP == 0 then
         if direction == 1 then
            si_command("si/g5/1/knob_cw")
            elseif direction == -1 then
            si_command("si/g5/1/knob_ccw")
         end
      end
   
     --Button 3, NAV2
     if BP == 3 then
       if direction == -1 then
            fsx_event("NAV2_RADIO_WHOLE_DEC")
            xpl_command("sim/radios/stby_nav2_coarse_down")
            elseif direction == 1 then
            fsx_event("NAV2_RADIO_WHOLE_INC")
            xpl_command("sim/radios/stby_nav2_coarse_up")
       end
     end   
     
     if BP == 7 then
       if direction == -1 then
            fsx_event("NAV2_RADIO_FRACT_DEC")
            xpl_command("sim/radios/stby_nav2_fine_down")
            elseif direction == 1 then
            fsx_event("NAV2_RADIO_FRACT_INC")
            xpl_command("sim/radios/stby_nav2_fine_up")
       end  
     end
       
     --Button 4, OBS2
     if BP == 4 then
       if direction == 1 then
            xpl_command("sim/radios/obs2_down")
            fsx_event("VOR2_OBI_DEC")
            elseif direction == -1 then
            xpl_command("sim/radios/obs2_up")
            fsx_event("VOR2_OBI_INC")
       end
     end   
     
     --Button 6, COM2
     if BP == 6 then
       if direction == -1 then
            fsx_event("COM2_RADIO_WHOLE_DEC")
            xpl_command("sim/radios/stby_com2_coarse_down")
            elseif direction == 1 then
            fsx_event("COM2_RADIO_WHOLE_INC")
            xpl_command("sim/radios/stby_com2_coarse_up")
       end
     end   

     if BP == 8 then
       if direction == -1 then
            fsx_event("COM2_RADIO_FRACT_DEC")
            xpl_command("sim/radios/stby_com2_fine_down")
            elseif direction == 1 then
            fsx_event("COM2_RADIO_FRACT_INC")
            xpl_command("sim/radios/stby_com2_fine_up")
       end  
     end  
       
       
   end
---------------

   function rot2(direction)
      
    --Default behavior-control G5HSI
      if BP == 0 then
        if direction == 1 then
          si_command("si/g5/2/knob_cw")
          elseif direction == -1 then
          si_command("si/g5/2/knob_ccw")
        end
      end
    end 
       
-------------- Create hardware assignments -------

hw_button_add("Button3", BP3)
hw_button_add("Button4", BP4)
hw_button_add("Button6", BP6)
led3=hw_led_add("LED3", 0.0)
led4=hw_led_add("LED4", 0.0)
led6=hw_led_add("LED6", 0.0)
led7=hw_led_add("LED7", 0.0)
led8=hw_led_add("LED8", 0.0)
hw_dial_add("TopRotary", "TYPE_1_DETENT_PER_PULSE", rot1)
hw_dial_add("BottomRotary", "TYPE_1_DETENT_PER_PULSE", rot2)
hw_button_add("TopButton", tpress, trelease)
hw_button_add("BottomButton", bpress, brelease)
The problem is that it's acting 'weird', even though I've stripped out several of the functions to try and eliminate possible sources of error. For instance, when I have button 6 pressed and I press the bottom rotary button it's supposed to swap the com2 frequency. If I push the virtual button on the hardware tab it does exactly what it's supposed to. When I push the physical button on the rotary sometimes it will swap frequencies, many times not, and once it even swapped both the nav and com frequencies simultaneously. I've checked my wiring and when I press the physical button I can see the led on the arduino flash so I know it's getting the input. Is there something wrong with my code? The only other possibility I can think of is that the button is wired to pin A5 on the Nano, but I thought it was OK to run switches on analog pins.

Thank you for looking.

Steve

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

Re: Another problem with the button box

#2 Post by Ralph »

Without going trough your whole script, I see that you are setting the same LED's on different places, which is never a good thing to do.
My head begins to swirl from this script :lol: I really recommend looking at my script, it makes it much easier. This is a bit of a spaghetti...

SteveBurney
Posts: 33
Joined: Fri Mar 04, 2022 2:13 am

Re: Another problem with the button box

#3 Post by SteveBurney »

I apologize for the spaghetti, it's my first shot at this. Here's a summary of what I'm thinking, if it will help give some insight to the script: When a button is pressed it sets a variable, BP. The LED's illuminate based on the value of the variable and the function of the rotary encoders is also based on the value of the variable. Since tuning the radios is actually 2 functions (mhz and khz) then the button on the rotary has to respond in the same manner (the button on one rotary will change from mhz to khz or khz to mhz, the button on the other rotary will swap the active and standby frequencies) for either of those values of BP.

Is there a better way to accomplish this, and what script are you referring to that I should look at? I'm happy to have another go at it.

For the most part, though, the LEDs are working properly. It's the function of the button on the rotary that's giving me a fit today.

Steve

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

Re: Another problem with the button box

#4 Post by Ralph »

I will try to have a good look this week. When the painting of our new office is done... Has to be finished before Monday.

SteveBurney
Posts: 33
Joined: Fri Mar 04, 2022 2:13 am

Re: Another problem with the button box

#5 Post by SteveBurney »

Hi Ralph,
I think I got it working, so no need for you to spend the time on it. I appreciate your help, though, and I even attempted to reduce the spaghetti by creating a function to turn out all of the led's and another function to turn on the applicable led. It's probably still a long way from ideal, but it works, it's easy to read and follow, and it's only 270 lines of commented code.

Steve

Post Reply