Ending A Loop Instantly


Recommended Posts

Hello,

I am trying to end a loop when an input (END) reachs a value above 2. Currently when the input (END) goes above 2 the loop ends after the OutputLED finshes it's delays. Is there a way to end the loop as soon as the END value goes above 2 instead on waiting for the delays to finish?

Here is what I came up with.

Thanks

while (END[0] < 2)

RFON = 5.0

OutputLED= 5.0

delay(10)

OutputLED= 4.5

delay(10)

OutputLED= 4.0

delay(1)

OutputLED= 3.5

delay(1)

OutputLED= 3.0

delay(1)

OutputLED= 2.5

delay(1)

OutputLED= 2.0

delay(1)

OutputLED= 1.5

delay(1)

OutputLED= 1.0

delay(1)

OutputLED= 0.5

delay(1)

OutputLED= 0.0

delay(1)

endwhile

while (1)

RFON = 0

ALG= 0

OutputLED = 0

delay(10)

endwhile

Link to comment
Share on other sites

There are three ways, one brute force, which I won't describe, one involving a second sequence, and another more elegant way. I'm going to show the last, but first you need to make your first loop a little more elegant too:

private delays = {10,10,1,1,1,1,1,1,1,1,1}

private nexttime = systime()

private i = 0

RFON = 5.0

while(1)

while (systime() < nexttime)

if (end[0] >= 2)

break

endif

delay(0.25)

endwhile

OutputLED = 5 - i * 0.5

nexttime += delays

i++

if (i > 10)

i = 0

endif

endwhile

// etc...

I made your original more elegant by using arrays and math to determine the output and delays instead of brute forcing it. Then the little while(systime() < nexttime) loop allows you to monitor for your end marker and break. That loop runs every 0.25 seconds, which is the maximum latency from when end goes above 2 and the loop exits.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.