Hello Guest it is March 28, 2024, 03:04:53 PM

Author Topic: Problems with For Next loop in script  (Read 3452 times)

0 Members and 1 Guest are viewing this topic.

Problems with For Next loop in script
« on: January 29, 2015, 03:33:53 PM »
I've been testing this script using MDI, for stopping X axis with an external input from a switch, and then allowing it to be moved afterwards:-

If IsActive(INPUT 1) Then  'If switch is tripped
DoOEMButton(250)   'stop X axis
DoOEMButton(1021)  'reset twice (once doesn't work)
DoOEMButton(1021)
End If
If IsActive(INPUT 1) Then
DoOEMButton(250)   'Do again to enable X axis to run again. Simply putting this command after 1st loop doesn't work
DoOEMButton(1021)
DoOEMButton(1021)
End If

If I trigger Input1 and hold it down, then run this, it works as I wanted it to, although it looks awful as a piece of code!

I want this script to run for 10 seconds, during which time it will 'look out' for input 1 triggering; so I put it inside a For Next loop, with a sleep command to ensure that this runs 50 times every second for 10 seconds; sufficient for my needs.

For i = 1 To 500
If IsActive(INPUT 1) Then  'If switch is tripped
DoOEMButton(250)   'stop X axis
DoOEMButton(1021)  'reset twice (once doesn't work)
DoOEMButton(1021)
End If
If IsActive(INPUT 1) Then
DoOEMButton(250)   'Do again to enable X axis to run again. Simply putting this command after 1st loop doesn't work
DoOEMButton(1021)
DoOEMButton(1021)
Sleep 20
End If
Next i

Unfortunately it works exactly the same as the first one; It doesn't seem to loop and wait for input 1 to activate; like the first one, it will only stop the axis and let it restart if I trigger Input1 and hold it down, then press run.

I can't see why it won't loop, sleep, and activate on input 1. Whatever it is, I just can't see it. I can't work out how to debug this at runtime, like in visual studio, as I don't know how to simulate the input 1's active signal.

Does anyone know why it isn't working as expected?

Thanks

Offline ger21

*
  • *
  •  6,295 6,295
    • View Profile
    • The CNC Woodworker
Re: Problems with For Next loop in script
« Reply #1 on: January 29, 2015, 03:50:15 PM »
Try this:

For i = 1 To 500

If IsActive(INPUT 1) Then  'If switch is tripped

DoOEMButton(1001)   'Feedhold

WhileIsMoving()
Wend

DoOEMButton(1003)  'Stop

End If

Sleep (20)

Next i
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html
Re: Problems with For Next loop in script
« Reply #2 on: January 29, 2015, 05:20:26 PM »
Thanks Ger, that's a lot less clumsy. It stops the axis, but I get an egg timer next to my VB screen, It seems to get stuck, because I can't do anything after it feedholds. I tried putting a boolean flag in there that goes true when Input1 is active, as I only need it to stop once in each activation, but it behaved the same as before.Have to switch Mach off and try again tomorrow...

I hope this doesn't turn out to be another 'Demo version only' limitation...

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Problems with For Next loop in script
« Reply #3 on: January 29, 2015, 05:29:29 PM »
IF Isactive(input1) Then

DoOemButton(1001)

While Ismoving()
Wend

DoOemButton(1003)

Else

End IF

END
Re: Problems with For Next loop in script
« Reply #4 on: January 29, 2015, 05:36:26 PM »
Thanks I'll try that tomorrow. Will that 'look out' for the Input1 signal? I kind of expected that to involve a loop somehow. Bit bleary eyed now, I'll call it a night I think.

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Problems with For Next loop in script
« Reply #5 on: January 29, 2015, 06:47:01 PM »
You need to RUN it in the macropump which IS a loop It runs 10 times per sec 10HZ.

To try it any other way would tie up the Proccessor and would tie up Mach3 waiting on something to happen. As you have found out.

You COULD do it in Brains as well.

THen again you could just use Limits and Rehome (;-) They do work well.

(;-) TP
Re: Problems with For Next loop in script
« Reply #6 on: January 31, 2015, 06:36:17 AM »
Thanks for that, it works just as I wanted. Its response feels a bit slower that the 10Hz frequency suggests, although that might be my sense of expectation! It's only for the Z axis, which moves very slowly and can take a lot of slack, so it should work fine.