• Tags
  • Documentation
  • Order
  • Register
  • Login
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
645
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.
  • undefined
    EducatingSavvas
    last edited by EducatingSavvas 14 Nov 2020, 20:00

    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
      last edited by A Former User 14 Nov 2020, 20:05

      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)

      undefined 1 Reply Last reply 16 Nov 2020, 10:53 Reply Quote 0
      • undefined
        EducatingSavvas @A Former User
        last edited by 16 Nov 2020, 10:53

        @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
        • undefined
          OwenD
          last edited by 16 Nov 2020, 11:51

          @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

          undefined 1 Reply Last reply 16 Nov 2020, 12:50 Reply Quote 0
          • undefined
            EducatingSavvas @OwenD
            last edited by 16 Nov 2020, 12:50

            @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
              last edited by 16 Nov 2020, 13:03

              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.

              undefined 1 Reply Last reply 16 Nov 2020, 13:39 Reply Quote 1
              • undefined
                EducatingSavvas @A Former User
                last edited by 16 Nov 2020, 13:39

                @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
                  last edited by 16 Nov 2020, 13:44

                  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

                  undefined 1 Reply Last reply 16 Nov 2020, 18:12 Reply Quote 1
                  • undefined
                    EducatingSavvas @A Former User
                    last edited by 16 Nov 2020, 18:12

                    @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
                      last edited by 16 Nov 2020, 18:44

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

                      undefined 1 Reply Last reply 16 Nov 2020, 20:09 Reply Quote 0
                      • undefined
                        EducatingSavvas @A Former User
                        last edited by 16 Nov 2020, 20:09

                        @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
                          last edited by 17 Nov 2020, 00:10

                          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)

                          undefined 1 Reply Last reply 21 Nov 2020, 13:15 Reply Quote 0
                          • undefined
                            EducatingSavvas @A Former User
                            last edited by 21 Nov 2020, 13:15

                            @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.

                            undefined 1 Reply Last reply 21 Nov 2020, 13:19 Reply Quote 0
                            • undefined
                              BoA @EducatingSavvas
                              last edited by BoA 12 Apr 2020, 14:20 21 Nov 2020, 13:19

                              @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
                              2 out of 14
                              • First post
                                2/14
                                Last post
                              Unless otherwise noted, all forum content is licensed under CC-BY-SA