It's ­čÉŹCircuitPython Monday­čÉŹ at Adafruit! Use the code ADATY to get 20% off CircuitPython and 15% off items storewide!
0

Can one prioritize a loop?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Can one prioritize a loop?

by Daft_vagabond on Sun Oct 17, 2021 11:09 am

Lets say I have a "main" while True-loop that reads an input, like a switch, and makes an led flash at a certain rate. Could I write a forever-loop that waits for a different input, like a button to be pressed, and once pressed it takes priority over the first loop and starts flashing the same led at a different rate, at least until the button was released?

Lets also say that, for this example, the "main" loop also drove some other peripherals. I wouldn't want the second loop to stop the main one, just to temporarily drive the led differently and leave the other peripherals to continue to be driven by the main loop.

Daft_vagabond
 
Posts: 43
Joined: Thu May 28, 2020 2:53 pm

Re: Can one prioritize a loop?

by dastels on Sun Oct 17, 2021 1:10 pm

If you are using CircuitPython: No. It's not a multi tasking system so you can't have two loops running at the same time. Ther are programming techniques that can achieve a similar result. See https://learn.adafruit.com/multi-tasking-with-circuitpython.

Dave

dastels
 
Posts: 7889
Joined: Tue Oct 20, 2015 3:22 pm

Re: Can one prioritize a loop?

by Daft_vagabond on Sun Oct 17, 2021 1:19 pm

So the

def on_forever():

doesn't apply here?

I'll check out that link in the meantime.

Daft_vagabond
 
Posts: 43
Joined: Thu May 28, 2020 2:53 pm

Re: Can one prioritize a loop?

by dastels on Sun Oct 17, 2021 8:33 pm

That's from Makecode. Not applicable in CircuitPython.

Dave

dastels
 
Posts: 7889
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.