Page 1 of 1

Ruby: while statement

Posted: Sun Feb 26, 2012 5:23 pm
by jerry
I wanted to use a while statement to loop in ruby until a limit switch was pressed.
Ruby gives an error : "excessive processing detected"
See code below:

while @ins[0] == false do
end

Please advise how to suspend operation until an event occurs.

Re: Ruby: while statement

Posted: Sun Feb 26, 2012 7:55 pm
by Embedded
Yes you have created an infinite loop, which is not good in programming terms. Hence FlowStone catches it before it causes your system to crash.

You should use a:

def event i,v,t

end

with you code inside,not the infinite loop, but what you actually what to do.

Re: Ruby: while statement

Posted: Mon Feb 27, 2012 8:53 pm
by jerry
I tried the event method you provided and I see how it continuously loops, but is there a way to exit out of that loop? Like a while statement I want to loop until a condition is met, perform an action, then exit out of the loop.

I am converting qbasic code that uses while loops and is working fine but I want to convert it to Flowstone/Ruby.

Re: Ruby: while statement

Posted: Wed Feb 29, 2012 12:04 pm
by Embedded
Try this, this uses the FlowStone events system to add a delay to the loop and therefore avoids an infinite loop.

while loop break clip.png
while loop break clip.png (29.04 KiB) Viewed 21209 times

Re: Ruby: while statement

Posted: Thu Mar 01, 2012 3:02 pm
by jerry
Thanks for the very useful code, I see the delay is working and definitely needed, if I reduce it from .01 to .001 flowstone has an error.

Re: Ruby: while statement

Posted: Sun Mar 04, 2012 3:35 am
by jerry
I see how the repeating if statement is equivalent to a while statement but I have not found a way to break out of the event method. If I add a "break" statement in the else loop I get an invalid break error. See below.

else
output 0, "Not Looping"
@count = 0
output 1, @count
break if @ins[0] == false
end

Invalid break.

Re: Ruby: while statement

Posted: Mon Mar 05, 2012 1:00 pm
by DSP
Thanks for the very useful code, I see the delay is working and definitely needed, if I reduce it from .01 to .001 flowstone has an error.


The ruby part will happily run up to around 20Khz, ( 0.00005) but if you output this into the GUI system to display it you will overload the PC. Hence while it works ok at 100hz (0.01).
Remember FlowStone is a programming language and therefore you can do pretty much what you want, doesn't mean it always good programming though, so you have to think carefully about what it is you are doing.