Gcode to repeatable Macro with Time input

Hello, i am trying to create a makro out of following gcode:

G1 Y200 F75000 ; Move to Y=150
G4 P100 ; Pause for 0,1 second
G1 Y10 F75000 ; Move to Y=50
G4 P100 ; Pause for 0,1 second

I’d like add a Time input, so the gcode will repeat itself until the time runs out.
And a stop button.

Timed input is not supported AFAIK and also stopping a running macro loop not.

Basically, you could do:

[gcode_macro BLA]
gcode:
    {% set runs = params.RUNS|default(1)|int %}
    {% for i in range(runs) %}
        G1 Y200 F75000 ; Move to Y=150
        G4 P100 ; Pause for 0,1 second
        G1 Y10 F75000 ; Move to Y=50
        G4 P100 ; Pause for 0,1 second
    {% endfor %}

And call it with BLA RUNS=5 and terminate with M112

Another approach would be sdcard_loop, which could be stopped with the CANCEL command

With both options, you would have to convert the needed time into a number of runs according to how long the gcode snippet takes.

1 Like

Just out of curiosity: could you add a normally open push button to a sensor (end stop) pulled-up input and fall out of the loop when the button is pressed (and the sensor input pulled to ground)?

AFAIK, no. Jinja does not support break or continue natively. It could be added by the loopcontrols extension, but this is not available in Klipper

Otherwise it could work by doing something like:

[gcode_macro CHECK_BUTTON]
gcode:
    # Get the button press and, e.g. set the variable `button_pressed`

[gcode_macro BLA]
gcode:
    {% set runs = params.RUNS|default(1)|int %}
    {% for i in range(runs) %}
        CHECK_BUTTON
        {% if button_pressed %}
            break
        {% endif %}
        G1 Y200 F75000 ; Move to Y=150
        G4 P100 ; Pause for 0,1 second
        G1 Y10 F75000 ; Move to Y=50
        G4 P100 ; Pause for 0,1 second
    {% endfor %}

(could be rubbish, since untested, but does not matter since no break exists)

2 Likes

Nice! Thanks its working fine