5.4 Example Standalone Programs
The following examples are provided to assist your understanding. Although they were produced with the utmost care, if you find any points that are unclear, wrong, or have inadequate descriptions, please let us know.
For more examples, refer to the Command Reference.
In this section
- Standalone Example Program 1 – Single Thread
- Standalone Example Program 2 – Single Thread with buffer
- Standalone Example Program 3 – Single Thread
- Standalone Example Program 4 – Single Thread
- Standalone Example Program 5 – Single Thread
- Standalone Example Program 6 – Single Thread with subroutine
- Standalone Example Program 7 – Single Thread with error handling
- Standalone Example Program 8 – Multi-thread
- Standalone Example Program 9 – Multi-thread with subroutine
- Standalone Example Program 10 – Multi-thread with subroutine
Example Program 1 – Single Thread
Download this program:
Summary of operation
Set high and low speed and move the X axis to 1000 and then back to 0.
Program code
PRG 0 ;Program start line, program 0
HSPD=20000 ;Set high speed to 20,000 pulses/sec
LSPD=1000 ;Set low speed to 1,000 pulses/sec
ACC=300 ;Set acceleration to 300 msec
EO=1 ;Enable the X axis
X1000 ;Move X axis to 1,000
WAITX ;Wait for X axis move to complete
X0 ;Move X axis to 0
WAITX ;Wait for X axis move to complete
END ;End of program 0
Example Program 2 – Single Thread with buffer
Download this program:
Summary of operation
Create a contouring profile for the X axis using buffer move function.
Program code
PRG 1 ;Program start line, program 1
BUFON ;Enable buffer move
; Load Buffer
HSPD=100 ;Set high speed to 100 pulses/sec
X1000Y100Z100 ;I:1000:100:100:100
HSPD=200 ;Set high speed to 200 pulses/sec
X2000Y100Z100 ;I:2000:100:100:200
HSPD=300 ;Set high speed to 300 pulses/sec
X3000Y100Z100 ;I:3000:100:100:300
HSPD=400 ;Set high speed to 400 pulses/sec
X4000Y100Z100 ;I:4000:100:100:400
HSPD=500 ;Set high speed to 500 pulses/sec
X5000Y100Z100 ;I:5000:100:100:500
HSPD=400 ;Set high speed to 400 pulses/sec
X3000Y100Z100 ;I:3000:100:100:400
HSPD=300 ;Set high speed to 300 pulses/sec
X0Y100Z100 ;I:0:100:100:300
; Start buffered move
ISTART ;BSTART
DELAY=100 ;Delay 0.1 seconds
WAITX ;Wait until X-axis motion has stopped
BUFOFF ;Disable buffer move
END ;End of program 1
Example Program 3 – Single Thread
Download this program:
Summary of operation
Move the X axis back and forth indefinitely between position 1000 and 0.
Program code
PRG 1 ;Program start line, program 1
HSPD=20000 ;Set the high speed to 20,000 pulses/sec
LSPD=1000 ;Set the low speed to 1000 pulses/sec
ACC=300 ;Set the acceleration to 300 msec
EO=1 ;Enable the motor power
WHILE 1=1 ;Forever loop
X0 ;Move to zero
WAITX ;Wait for X-axis move to complete
X1000 ;Move to 1000
WAITX ;Wait for X-axis move to complete
ENDWHILE ;Go back to WHILE statement
END ;End of program 1
Example Program 4 – Single Thread
Download this program:
Summary of operation
Move the X axis back and forth 10 times between position 1000 and 0.
Program code
PRG 1 ;Program start line, program 1
HSPD=20000 ;Set high speed to 20,000 pulses/sec
LSPD=1000 ;Set low speed to 1,000 pulses/sec
ACC=300 ;Set acceleration to 300 msec
EO=1 ;Enable the X-axis
V1=0 ;Set variable 1 to 0
WHILE V1<10 ;Loop while variable 1 is less than 10
X0 ;Move X-axis to zero (0)
WAITX ;Wait for X-axis move to complete
X1000 ;Move X-axis to 1,000
WAITX ;Wait for X-axis move to complete
V1=V1+1 ;Increment variable 1 by 1
ENDWHILE ;Go back to WHILE statement
END ;End of program 1
Example Program 5 – Single Thread
Download this program:
Summary of operation
Move the X axis back and forth between position 1000 and 0 only if the digital input 1 is turned on.
Program code
PRG 2 ;Program start line, program 2
HSPD=20000 ;Set high speed to 20,000 pulses/sec
LSPD=1000 ;Set low speed to 1000 pulses/sec
ACC=300 ;Set acceleration to 300 msec
EO=1 ;Enable the X-axis
WHILE 1=1 ;Forever loop
IF DI1=1 ;If digital input 1 is on, execute the following commands
X0 ;Move X-axis to zero
WAITX ;Wait for X-axis move to complete
X1000 ;Move X-axis to 1,000
WAITX ;Wait for X-axis move to complete
ENDIF ;End of the IF loop
ENDWHILE ;Go back to WHILE Statement
END ;End of program 2
Example Program 6 – Single Thread with subroutine
Download this program:
Summary of operation
Using a subroutine, increment the X axis by 1000 whenever the DI1 rising edge is detected.
Program code
PRG 0 ;Program start line, Program 0
HSPD=20000 ;Set high speed to 20,000 pulses/sec
LSPD=1000 ;Set low speed to 1000 pulses/sec
ACC=300 ;Set acceleration to 300 msec
ABS ;Set move mode to absolute mode
EO=1 ;Enable the X-axis
V1=0 ;Set variable 1 to 0
WHILE 1=1 ;Forever loop
IF DI1=1 ;If digital input 1 is on, execute the following commands
GOSUB 1 ;Execute subroutine 1
ENDIF ;End of the IF loop
ENDWHILE ;Go back to WHILE statement
END ;End of program 0
SUB 1 ;Subroutine start line, subroutine 1
XV1 ;Move X-axis to V1 target position
WAITX ;Wait for X-axis move to complete
V1=V1+1000 ;Increment V1 by 1,000
WHILE DI1=1 ;Wait until the DI1 is turned off (prevent multiple increments)
ENDWHILE ;Go back to WHILE statement
ENDSUB ;End of subroutine 1
Example Program 7 – Single Thread with error handling
Download this program:
Summary of operation
If digital input 1 is on, move to position 2000. If digital input 2 is on, move to position 4000. If digital input 3 is on, move to 6000. If digital input 4 is on, home the motor in negative direction, mode 0. Use digital output 1 to indicate that the motor is moving or not moving. Also, create an error handling subroutine utilizing SUB 31 that clears the error and then returns the motor to position zero.
Program code
PRG 2 ;Start program 2
HSPD=15000 ;Set high speed to 15,000 pulses/sec
LSPD=500 ;Set low speed to 500 pulses/sec
ACC=300 ;Set acceleration to 300 msec
EO=1 ;Enable the X-axis
WHILE 1=1 ;Forever loop
IF DI1=1 ;If digital input 1 is on
X2000 ;Move X-axis to 1000
ELSEIF DI2=1 ;If digital input 2 is on
X4000 ;Move X-axis to 4000
ELSEIF DI3=1 ;If digital input 3 is on
X6000 ;Move X-axis to 6000
ELSEIF DI4=1 ;If digital input 4 is on
HOMEX-0 ;Home X-axis with mode 0 in the negative direction
ENDIF ;End of the IF loop
V1=MSTX ;Store the X-axis status to variable 1
V2=V1&7 ;Get first 3 bits
IF V2!=0 ;If one of first 3 bits is high (X-axis moving)
DO1=1 ;Turn on digital output 1
ELSE ;Else if first 3 bits are low (X-axis idle)
DO1=0 ;Turn off digital output 1
ENDIF ;End of the IF loop
ENDWHILE ;Go back to WHILE statement
END ;End of program 2
SUB 31 ;Subroutine start line, subroutine 31 (error handling subroutine)
ECLEARX ;Clear the current error in X-axis
X0 ;Return X-axis to position 0
DO2=1 ;Turn on digital output 2
ENDSUB ;End of subroutine 31
Example Program 8 – Multi-thread
Download this program:
Summary of operation
Program 0 will continuously move the motor between positions 0 and 1000. Simultaneously, program 1 will control the status of program 0 using digital inputs.
Program code
PRG 0 ;Start program 0
HSPD=20000 ;Set speed to 20,000 pulses/sec
LSPD=500 ;Set speed to 500 pulses/sec
ACC=500 ;Set acceleration to 500 msec
WHILE 1=1 ;Forever loop
X0 ;Move X-axis to position 0
WAITX ;Wait for X-axis move to complete
X1000 ;Move X-axis to position 1,000
WAITX ;Wait for X-axis move to complete
ENDWHILE ;Go back to WHILE statement
END ;End program 0
PRG 1 ;Start program 1
WHILE 1=1 ;Forever loop
IF DI1=1 ;If digital input 1 is triggered
ABORTX ;Stop movement in X-axis
SR0=0 ;Stop program 0
ELSE ;If digital input 1 is not triggered
SR0=1 ;Run program 0
ENDIF ;End of the IF loop
ENDWHILE ;Go back to WHILE statement
END ;End program 1
Example Program 9 – Multi-thread with subroutine
Download this program:
Summary of operation
Program 0 starts program 1 and program 2, after which it calls subroutine 1, which homes axis X to the negative limit. Then Program 0 continuously moves the X-axis between positions 0 and 1000. Simultaneously, program 1 is monitoring the X-axis status. Program 1 uses digital output 1 to indicate that the motor is moving or not moving. Program 2 flashes digital output 3, on for 3 seconds, then off for 3 seconds. An error handling subroutine utilizing SUB 31 turns on digital output 2 clears the error and returns the motor to position zero, then waits 1.5 seconds, after which it turns digital output 2 off.
Program code
PRG 0 ;Start of program 0
SR1=1 ;Start program 1 running
SR2=1 ;Start program 2 running
ABS ;Set move mode to absolute mode
HSPD=1000 ;Set high speed to 1,000 pulses/sec
LSPD=100 ;Set low speed to 100 pulses/sec
ACC=200 ;Set acceleration to 200 msec
DEC=200 ;Set deceleration to 200 msec
EO=1 ;Enable X-axis
GOSUB 1 ;Go to Subroutine 1
WHILE 1=1 ;Forever loop
X0 ;Move X-axis to position 0
WAITX ;Wait for X-axis move to complete
DELAY=500 ;Delay 0.5 seconds
X1000 ;Move X-axis to position 1000
WAITX ;Wait for X-axis move to complete
DELAY=500 ;Delay 0.5 seconds
ENDWHILE ;Go back to WHILE statement
END ;End program 0
PRG 1 ;Start of program 1
WHILE 1=1 ;Forever loop
V1=MSTX ;Store the X-axis status to variable 1
V2=V1&7 ;Get first 3 bits
IF V2!=0 ;If one of first 3 bits is high (X-axis moving)
DO1=1 ;Turn on digital output 1
ELSE ;Else if first 3 bits are low (X-axis idle)
DO1=0 ;Turn off digital output 1
ENDIF ;End of the IF loop
ENDWHILE ;Go back to WHILE statement
END ;End program
PRG 2 ;Start of program 2
WHILE 1=1 ;Forever loop
DO3=0 ;Turn off digital output 3
DELAY=3000 ;Delay 3 seconds
DO3=1 ;Turn on digital output 3
DELAY=3000 ;Delay 3 seconds
ENDWHILE ;Go back to WHILE statement
END ;End program
SUB 1 ;Subroutine start line, subroutine 1
HOMEX-6 ;Home X-axis with mode 6 in the negative direction
WAITX ;Wait for X-axis move to complete
ENDSUB ;End of subroutine 1
SUB 31 ;Subroutine start line, subroutine 31 (error handling subroutine)
DO2=1 ;Turn on digital output 2
ECLEARX ;Clear the current error in X-axis
X0 ;Return X-axis to position 0
DELAY=1500 ;Delay 1.5 seconds
DO2=0 ;Turn off digital output 2
ENDSUB ;End of subroutine 31
Example Program 10 – Multi-thread with subroutine
Download this program:
Summary of operation
Program 1 will home X and Y axes, then will continuously move the X and Y axes and control a laser pointer to draw a circle, line, then an arc. Simultaneously, program 3 will control the status of general-purpose digital outputs to control LEDs. Subroutine 1 turns off a laser pointer, Subroutine 2 turns on the laser pointer, Subroutine 7 enables and homes the X and Y axes, and Subroutine 31 handles errors.
Program code
PRG 1 ;Start program 1
GOSUB 1 ;Go to subroutine 1, Turn off laser pointer
GOSUB 7 ;Go to subroutine 7, Enable and Home X/Y
WHILE 1=1 ;Forever loop
; Draw Circle
X85610Y562530 ;Move to start point for Circle
WAITX ;Wait for X-axis move to complete
GOSUB 2 ;Go to subroutine 2, Turn on laser pointer
CIRXYP487659:562431 ;Moves XY in a CW circle around points X487659 Y562431
WAITX ;Wait for X-axis move to complete
GOSUB 1 ;Go to subroutine 1, Turn off laser pointer
; Draw Line
X100000Y100000 ;Move to start point of Line
WAITX ;Wait for X-axis move to complete
GOSUB 2 ;Go to subroutine 2, Turn on laser pointer
X909000Y642390 ;Linear XY move from X100000 Y100000 to X909000 Y642390
WAITX ;Wait for X-axis move to complete
GOSUB 1 ;Go to subroutine 1, Turn off laser pointer
; Draw Arc
X430589Y20623 ;Move to start point for Arc
WAITX ;Wait for X-axis move to complete
GOSUB 2 ;Go to subroutine 2, Turn on laser pointer
ARCXYN288899:267403:95000 ;270deg arc of XY CCW around points X288899 Y267403
WAITY ;Wait for Y-axis move to complete
GOSUB 1 ;Go to subroutine 1, Turn off laser pointer
ENDWHILE ;Go back to WHILE statement
END ;End program 1
PRG 3 ;Start program 3
WHILE 1 = 1 ;Forever loop
IO=64 ;General-purpose output 7 on.
DELAY=700 ;Delay 0.7 seconds
IO=80 ;General-purpose output 5 and 7 on.
DELAY=700 ;Delay 0.7 seconds
IO=84 ;General-purpose output 3, 5, and 7 on.
DELAY=700 ;Delay 0.7 seconds
IO=85 ;General-purpose output 1, 3, 5, and 7 on.
DELAY=700 ;Delay 0.7 seconds
V30=0 ; Set variable 30 to 0.
WHILE V30 < 10 ;Loop while variable 30 is less than 10
IO = 85 ;General-purpose output 1, 3, 5, and 7 on.
DELAY=50 ;Delay 0.05 seconds
IO = 0 ;All general-purpose outputs off.
DELAY=50 ;Delay 0.05 seconds
V30=V30+1 ;Increment variable 30 by 1
ENDWHILE ;Go back to WHILE statement
ENDWHILE ;Go back to WHILE statement
END ;End Program 3
SUB 1 ;Subroutine start line, subroutine 1
DO2=0 ;DO 2 off to turn off Laser pointer
DELAY=10 ;Delay 0.01 seconds to stabilize laser
ENDSUB ;End of subroutine 1
SUB 2 ;Subroutine start line, subroutine 2
DO2=1 ;DO 2 on to turn on Laser pointer
DELAY=10 ;Delay 0.01 seconds to stabilize laser
ENDSUB ;End of subroutine 2
SUB 7 ;Subroutine start line, subroutine 7
; Enable motors
HSPD=15000 ;Set high speed to 15,000 pulses/sec
LSPD=500 ;Set low speed to 500 pulses/sec
ACC=100 ;Set acceleration to 100 msec
EO=3 ;Enable X and Y axis
; Home axis
HOMEX-6 ;Home X-axis with mode 6 in the negative direction
WAITX ;Wait for X-axis move to complete
HOMEY-6 ;Home Y-axis with mode 6 in the negative direction
WAITY ;Wait for Y-axis move to complete
; Move X/Y to Start Position
HSPD=35000 ;Set high speed to 35,000 pulses/sec
X500000Y500000 ;Move X and Y-axis to X500,000 Y500,000
WAITX ;Wait for X-axis move to complete
ENDSUB ;End of subroutine 7
SUB 31 ;Subroutine start line, subroutine 31 (error handling)
SR1=0 ;Stop program 1
SR3=0 ;Stop program 3
ABORT ;Abort motion of all axes
ECLEARX ;Clear the current error in X-axis
ECLEARY ;Clear the current error in Y-axis
IO=0 ;All general-purpose outputs off.
DO2=0 ;DO 2 off to turn off Laser pointer
ENDSUB ;End of subroutine 31