I am using this macro program for a touch probe to set work coordinates for a bore hole and it works great except one quirk that i cant figure out. Macro programming is new to me so i need a little help on what im missing here.
Here is the program. Most of this i copied from a post i found on here somewhere.

What happens is it runs the program and probes perfectly and sets the work coordinate great, but if i run the program a second time it will set my work coordinate to a tiny number say X.0036 and Y .0021 or something like that. If i then run it again it will set the work coordinate right again. Next time is wrong and so on so forth.
If i write in #5221=0 and #5222=0 on the first lines of the program it will run the program but will move in the wrong direction after the first touch on Y.

(SET G54)
(POSITION PROBE IN APROXIMATE CENTER OF HOLE)
G91
G31 Y10. F10.
G0 Y-.5
G31 X-10. (X- PROBE)
#501=#5061
G0 X.25
G31 X10. (X+ PROBE)
#502=#5061
G0 X-.5
G31 Y10. (Y+ PROBE)
#503=#5062
G0 Y-.25
G31 Y-10. (Y- PROBE)
#504=#5062
G0 Y.25
Z10.
G90
#5221=[#501+#502]/2
#5222=[#503+#504]/2
#501=0
#502=0
#503=0
#504=0
M30

Anybody want to take a stab at this?