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

    Wiring and programming a switch for a conditional statement

    Scheduled Pinned Locked Moved
    Gcode meta commands
    4
    6
    532
    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.
    • Surgikillundefined
      Surgikill
      last edited by

      Hi all,

      I want to wire a switch into my printer and use it in my homing macro. It's very simple, it will just be an if statement. If the switch is triggered, I want the Z axis to move up by 15, if the switch is not triggered, it will do nothing. So:

      if switch=true
      G1 Z15

      I was looking at M581 but it seems that it would only work for a state change. I'm guessing I would wire this to an endstop input and then map it to something.

      1 Reply Last reply Reply Quote 0
      • rjenkinsgbundefined
        rjenkinsgb
        last edited by

        You can use a simple conditional statement, something like this:

        if sensors.gpIn[3].value = 0
                G91
                G1 Z15
                G90
        

        Use whichever input pin and level is appropriate for the switch.
        Any following indented lines will only be executed if the conditional statement is true. Normal execution continues with the first non-indented line.

        Robert J.

        Printers: Overlord pro, Kossel XL+ with Duet 6HC and "Frankentron", TronXY X5SA Pro converted to E3D toolchange with Duet 6HC and 1LC toolboards.

        heathharperundefined 1 Reply Last reply Reply Quote 2
        • Phaedruxundefined Phaedrux moved this topic from Duet Hardware and wiring
        • heathharperundefined
          heathharper @rjenkinsgb
          last edited by

          @rjenkinsgb I know this is an older post, but do you think I could use this same code to continuously run some gcode lines over and over when a button is pressed by using "while" instead of if?

          If so, what would the code look like if I just want to continuously move in .5mm while the button is pressed on io2?

          Thanks in advance!

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

            @heathharper
            Yes you can, but there are a couple of things to note.
            When you press the button, the move commands will begin to queue. So when you release the button, the movement may continue much more than you thought.
            You can add M400 in the movement loop to ensure each move is completed before the next one takes place, but it will add some jerkiness.
            You might also have to add a check to account for button bouncing.

            G91
            G4 P10 ; delay 10ms to debounce
            if sensors.gpIn[2].value=0
               M99 ; break out if sensor value is zero again (bouncing)
            while sensors.gpIn[2].value=1
               G1 X0.5 F1800
               M400
            G90
            
            heathharperundefined 2 Replies Last reply Reply Quote 0
            • heathharperundefined
              heathharper @OwenD
              last edited by

              @OwenD Thanks! I will try it tomorrow when I'm back in the office!

              1 Reply Last reply Reply Quote 0
              • heathharperundefined
                heathharper @OwenD
                last edited by

                @OwenD That worked! I just had to swap the 1 and 0, true false since my switch is normally closed. THANKS!

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Unless otherwise noted, all forum content is licensed under CC-BY-SA