Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    How to determine button states?

    Help
    5
    14
    306
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Tratax
      Tratax F last edited by Tratax

      The code below should perform a logical check of 3 button "states" and then perform the appropriate action per state, hope this helps!

      buttonState = 0
      
      loop
      c = controls(0)
      
      if c.b == 1 and buttonState != 2 then
      buttonState = 1
      endif
      
      if  buttonState == 1 then
      ink(red)
      print("red!")
      buttonState = 2
      endif
      
      if buttonState == 2 and c.b == 0 then
      ink(blue)
      print("blue")
      buttonState = 0
      endif
      
      update()
      repeat
      
      

      Update: Corrected syntax and print debug test output, works well now!

      M 1 Reply Last reply Reply Quote 1
      • M
        MikeDX @Tratax last edited by MikeDX

        @Tratax don't forget to check for double equals!

        = isn't the same as ==

        if c = 1 then // <- this will always return true!
        
        Tratax 1 Reply Last reply Reply Quote 2
        • N
          Nisse5 F @sys64738 last edited by

          @sys64738 To wait for the up afterwards:

          while controls(0).a loop repeat
          

          Or, this version would do the same, but would probably save much more battery:

          while controls(0).a loop update() repeat
          

          To track the previous state, something like:

          prevstate = true // To make sure that an up exist before the first down
          loop
             c = controls(0)
             if c.a and !prevstate then
                // Do something
             endif
             prevstate = c.a
             update()
          repeat
          
          1 Reply Last reply Reply Quote 0
          • sys64738
            sys64738 F last edited by

            Have made a new project and tried tratax suggestion with == and c = controls(0) sorted

            It triggers red laser every loop while button is held down. Instead of once as needed.

            Does while take all the programs focus. In my intended program it needs to still run the main loop at 60 fps

            N 1 Reply Last reply Reply Quote 0
            • N
              Nisse5 F @sys64738 last edited by

              @sys64738 said in How to determine button states?:

              Does while take all the programs focus. In my intended program it needs to still run the main loop at 60 fps

              Is there any reason why you don't check the controls in the main loop? To me, streamlining control states + drawing + frame updates in the main loop seems like the most efficient use of FUZE.

              sys64738 1 Reply Last reply Reply Quote 0
              • sys64738
                sys64738 F last edited by

                Nisse5 your prevstate example fires a single red laser per press as requested. But how do I tell when button is released

                1 Reply Last reply Reply Quote 0
                • sys64738
                  sys64738 F @Nisse5 last edited by

                  @Nisse5 I am checking in main loop

                  1 Reply Last reply Reply Quote 0
                  • sys64738
                    sys64738 F last edited by sys64738

                    Adding this to your code seems to have done it

                    Thank you tratax and nisse5

                    
                    If c.a == 0 and prevstate == 1 then
                    //blue laser
                    End if
                    
                    Prevstate = c.a
                    Update()
                    Repeat
                    1 Reply Last reply Reply Quote 1
                    • Tratax
                      Tratax F @MikeDX last edited by Tratax

                      @MikeDX Thanks Mike, this was written on the train without my switch handy.. no excuse for leaving it at home I guess haha

                      1 Reply Last reply Reply Quote 1
                      • Tratax
                        Tratax F last edited by

                        Have rewritten the above suggestion for anyone else who needs this solution

                        1 Reply Last reply Reply Quote 0
                        • Martin
                          Martin Fuze Team last edited by Martin

                          I tackle this differently (there's always more than one way to do stuff)

                          I maintain a 'debounce' struct. In the main loop I look for c.a == 1 and increment debounce.a. In the else (ie, the button is no longer pressed) I say if debounce.a > 0 then do whatever it was I wanted to do and also reset debounce.a to 0 ready for detecting the next keypress.

                          For me this solves the problem of only doing something once per press. If instead of performing an action on keyup you really do want keydown then you can say if debounce.a == 1 (or 2 or 3 or 10 or whatever you want). If you want auto repeat you can use debounce.a % 50 or something like that.

                          1 Reply Last reply Reply Quote 2
                          • First post
                            Last post