Wiring and programming a switch for a conditional statement
-
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 Z15I 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.
-
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. -
-
@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!
-
@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
-
@OwenD Thanks! I will try it tomorrow when I'm back in the office!
-
@OwenD That worked! I just had to swap the 1 and 0, true false since my switch is normally closed. THANKS!