Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Toggle gpio with external trigger on/off/on

    Scheduled Pinned Locked Moved
    Gcode meta commands
    4
    14
    646
    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.
    • EducatingSavvasundefined
      EducatingSavvas
      last edited by EducatingSavvas

      Hi - I'm trying to create an external trigger that toggles (flip flop) a relay module on and off, or off and on. I want to use a single momentary button to do this so I can save space. The relay will operate a dust extractor.

      if sensors.gpIn[2].value=0
      	M42 P2 S1
      
      else sensors.gpIn[2].value=1
      	M42 P2 S0
      

      I though it would be as simple as the above but it's only performing the first if part. Any suggestions welcomed.

      1 Reply Last reply Reply Quote 0
      • A Former User?
        A Former User
        last edited by A Former User

        echo out the state of the pin before the if statement might give you some insight

        (Edit: Also you need else if or just strike the condition. Pin is 1 if it's not 0, can't be anything else)

        EducatingSavvasundefined 1 Reply Last reply Reply Quote 0
        • EducatingSavvasundefined
          EducatingSavvas @A Former User
          last edited by

          @bearer Still no luck - I'm a bit confused now.

          If I only write in the external trigger

          if sensors.gpIn[2].value=0
          	M42 P2 S1
          

          and the pin value is 0, running the external trigger turns it on

          but if I write

          if sensors.gpIn[2].value=1
          	M42 P2 S0
          

          while the pin is 1, it doesn't change.

          1 Reply Last reply Reply Quote 0
          • OwenDundefined
            OwenD
            last edited by

            @EducatingSavvas said in Toggle gpio with external trigger on/off/on:

            if sensors.gpIn[2].value=0

            I believe @bearer meant you could try

            if sensors.gpIn[2].value=0
            
            	M42 P2 S1
            else 
            
            	M42 P2 S0
            

            i.e no need for second if conditional

            EducatingSavvasundefined 1 Reply Last reply Reply Quote 0
            • EducatingSavvasundefined
              EducatingSavvas @OwenD
              last edited by

              @OwenD said in Toggle gpio with external trigger on/off/on:

              if sensors.gpIn[2].value=0 M42 P2 S1 else M42 P2 S0

              Just tried that and that doesn't work either.

              1 Reply Last reply Reply Quote 0
              • A Former User?
                A Former User
                last edited by

                Then you either found a bug, of your pin is always determined to be 0.

                Writing out the value before the if statement with echo is probably a good idea to see whats going on.

                If it always writes out 0 then you either wired something wrong, or it could be the pin has been damaged.

                EducatingSavvasundefined 1 Reply Last reply Reply Quote 1
                • EducatingSavvasundefined
                  EducatingSavvas @A Former User
                  last edited by

                  @bearer said in Toggle gpio with external trigger on/off/on:

                  ined to be 0.

                  If I write M42 P2 S1 or M42 P2 S0 into the console it changes.

                  echo sensors.gpIn[2].value=0
                  results in a false, regardless if the M42 P2 is S1 or S0

                  I don't really know what to do next...

                  1 Reply Last reply Reply Quote 0
                  • A Former User?
                    A Former User
                    last edited by

                    sensors.gpIn[2] is the same pin as you're setting with M42 P2?

                    maybe poke around with m409 to see if that should be gpOut or something?
                    https://duet3d.dozuki.com/Wiki/Gcode#Section_M409_Query_object_model

                    EducatingSavvasundefined 1 Reply Last reply Reply Quote 1
                    • EducatingSavvasundefined
                      EducatingSavvas @A Former User
                      last edited by

                      @bearer Yes - I'm checking the state of the same pin I want to change depending on the value.

                      1 Reply Last reply Reply Quote 0
                      • A Former User?
                        A Former User
                        last edited by

                        Then i suspect you should use sensors.gpOut[2] instead og sensors.gpIn[2] but you can test with m409.

                        EducatingSavvasundefined 1 Reply Last reply Reply Quote 0
                        • EducatingSavvasundefined
                          EducatingSavvas @A Former User
                          last edited by

                          @bearer said in Toggle gpio with external trigger on/off/on:

                          Then i suspect you should use sensors.gpOut[2] instead og sensors.gpIn[2] but you can test with m409.

                          Thanks - I've ran out of time today but will have a go next time I am in. I actually need to change from the e1 and e0temp pins I'm currently using as I've noticed they toggle on briefly on start up - although not enough current to turn the relay module on but that could change. I can see the GPIO pins on the duex5 but not on the Duet 2. Are those what you were referring too?

                          1 Reply Last reply Reply Quote 0
                          • A Former User?
                            A Former User
                            last edited by

                            Whatever pin you define in M950 as output Pn will need to be addressed as sensors.gpOut[n]

                            (or maybe one is index'ed from 1 and the other 0, but still need to address it as an output i think)

                            EducatingSavvasundefined 1 Reply Last reply Reply Quote 0
                            • EducatingSavvasundefined
                              EducatingSavvas @A Former User
                              last edited by

                              @bearer I think my head is a bit clearer today. I don’t know what I was doing last weekend… I wrote

                              M42 P3 S0

                              to turn the pin low / off and

                              M409 K"state.gpOut[3]"

                              to check the pin state getting this back.

                              {"key":"state.gpOut[3]","flags":"","result":{"pwm":0}}

                              I then wrote M42 P3 S1 so the pin was high / on and queried the state again:

                              M409 K"state.gpOut[3]"

                              {"key":"state.gpOut[3]","flags":"","result":{"pwm":1.00}}

                              I then changed my trigger to

                              If state.gpOut[3].pwm==0
                              	M42 P3 S1
                              else state.gpOut[3].pwm==1
                              	M42 P3 S0
                              

                              Thanks again for pointing me in the right direction. I’m going to place the relevant M42 commands into M10.g and M11.g and simplify the trigger file to refer to those.

                              BoAundefined 1 Reply Last reply Reply Quote 0
                              • BoAundefined
                                BoA @EducatingSavvas
                                last edited by BoA

                                @EducatingSavvas

                                If state.gpOut[3].pwm==0
                                	M42 P3 S1
                                else state.gpOut[3].pwm==1
                                	M42 P3 S0
                                

                                condition in else is obsolete, and more "elegant" way is

                                If state.gpOut[3].pwm != 0
                                	M42 P3 S0
                                else
                                	M42 P3 S1
                                
                                1 Reply Last reply Reply Quote 3
                                • First post
                                  Last post
                                Unless otherwise noted, all forum content is licensed under CC-BY-SA