Control Flow

10. Custom Macro

Control Flow

Control Structures

Control structures allow creating programs with decisions and repetitions.

IF...THEN...ELSE

Conditional execution:

; Simple IF
IF [#1 GT 0] THEN #2 = 1

; IF with GOTO
IF [#1 EQ 0] GOTO 100

; IF...THEN...ELSE (some controllers)
IF [#1 GT 0] THEN
  #2 = 1
ELSE
  #2 = -1
ENDIF

GOTO

Unconditional jump to a sequence number:

GOTO 100        ; Always jump to N100

N100 G0 X0 Y0

WHILE...DO...END

Loop with condition at start:

; Repeat while condition true
#1 = 0
WHILE [#1 LT 10] DO1
  G1 X#1 F500
  #1 = #1 + 1
END1

; Nested loops (DO1, DO2, DO3)
WHILE [#1 LT 5] DO1
  WHILE [#2 LT 5] DO2
    ; inner code
  END2
END1

Example: Circular Hole Pattern

; Parameters
#1 = 50      ; Circle radius
#2 = 8       ; Number of holes
#3 = 0       ; Start angle
#4 = -15     ; Depth

; Calculate angular step
#5 = 360 / #2

; Hole cycle
#6 = 0                          ; Counter
WHILE [#6 LT #2] DO1
  #7 = #1 * COS[#3]             ; X
  #8 = #1 * SIN[#3]             ; Y
  G0 X#7 Y#8
  G1 Z#4 F100                   ; Drill
  G0 Z5                         ; Retract
  #3 = #3 + #5                  ; Next angle
  #6 = #6 + 1                   ; Increment counter
END1

Subprogram Call with Arguments

; Call with arguments
G65 P9100 A10 B20 C-5 F500

; In subprogram O9100:
; #1 = A = 10
; #2 = B = 20
; #3 = C = -5
; #9 = F = 500

Warning: Nested loops must use different DO numbers (DO1, DO2, DO3). Never nest the same number.