Ruby: while statement

For general discussion related FlowStone
Post Reply
jerry
Posts: 20
Joined: Tue Nov 01, 2011 8:06 pm
Location: Schaumburg, Illinois, USA

Ruby: while statement

Post 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.
Embedded
Posts: 143
Joined: Sat Oct 30, 2010 1:42 pm

Re: Ruby: while statement

Post 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.
Attachments
def event.fsm
(329 Bytes) Downloaded 1309 times
jerry
Posts: 20
Joined: Tue Nov 01, 2011 8:06 pm
Location: Schaumburg, Illinois, USA

Re: Ruby: while statement

Post 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.
Embedded
Posts: 143
Joined: Sat Oct 30, 2010 1:42 pm

Re: Ruby: while statement

Post 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 21189 times
Attachments
while loop break.fsm
(523 Bytes) Downloaded 1257 times
jerry
Posts: 20
Joined: Tue Nov 01, 2011 8:06 pm
Location: Schaumburg, Illinois, USA

Re: Ruby: while statement

Post 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.
jerry
Posts: 20
Joined: Tue Nov 01, 2011 8:06 pm
Location: Schaumburg, Illinois, USA

Re: Ruby: while statement

Post 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.
DSP
Posts: 150
Joined: Fri May 14, 2010 10:55 pm

Re: Ruby: while statement

Post 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.
Post Reply