How to exit running "While" loop

I have a Micropython programme which includes a number of Function definitions. These are called sequentially from the main body of the programme in response to a button press within a primary “While True” loop. Some of these Functions include a “While True” loop themselves. My question is, how do I exit from these Function “While True” conditions (which ordinarily would run continuously) back to the main body of the programme using the same button press which invoked the Function in the first place? I am trying to control the LED string within a Skully set but am using a Plasma2040 as I want to make use of the buttons which this controller offers. The Function calls which only select a fixed light condition (ie different colours) work; its only Function calls which include a While loop that are the problem. Any help/comments gratefully received.

I am far from any kind of expert on this, just so you know.
On the Galactic Unicorn its does it as an import as an effect.

    if pressed() == GalacticUnicorn.SWITCH_A:
        import fire as effect
        break
    if pressed() == GalacticUnicorn.SWITCH_B:
        import supercomputer as effect 
        break
    if pressed() == GalacticUnicorn.SWITCH_C:
        import rainbow as effect        
        break
    if pressed() == GalacticUnicorn.SWITCH_D:
        import retroprompt as effect 
        break

I have no idea how it works. But it does work, and sounds like what your trying to do.
pimoroni-pico/main.py at main · pimoroni/pimoroni-pico (github.com)

Does the main while loop use a different button to call each subroutine?

[I understand a function to be a piece of code which supplies a value or values and a procedure a piece of defined code carrying out a task without supplying a value back to the main calling code. Get a temperature is a function while toggle an LED for ever is a procedure.]

# Nested while loops
# Tony Goodhew 17 Feb 2023
# Import the libraries
from machine import Pin, ADC
import time
import random # Random number generator

# === Set up section ===
# Set up button on GP15 as INPUT with PULL_DOWN
button = Pin(15, Pin.IN, Pin.PULL_DOWN)

# Set up LED on GP16 on Pi Pico
led = Pin(16, Pin.OUT)

# Set up the potentiometer on ADC pin 27, ADC0
potentiometer = ADC(Pin(26))


def proc():
    print("Procedure")
    while button.value() ==1:
        continue
    run1 = True
    led.value(1)
    while run1:
        pot = potentiometer.read_u16()
        print("Pot value: "+str(pot))
        time.sleep(0.2)
        if button.value() ==1:
            run1 = False
            led.value(0)
    while button.value() ==1:
        continue
    print("Main again")    

# === Main ===
count = 0

while True: # Start looping – infinite loop in effect
    print(count)
    count = count + 1
    time.sleep(0.2) # Wait a short moment
    if button.value() == 1:
        proc()

This program counts in the main loop but when the button is pressed it jumps to the procedure which lights the LED, to indicate that it is in the subsidiary loop, reads the potentiometer and prints the value. When the same button is pressed again control returns to the main loop.
If you want several subsidiary loops/procedures you will need a different button for each of them.

Note the two continue loops to wait until the button is released from the press.

I hope this helps. Please let me know if this solves your query.

Thank you alphanumeric and Tonygo2 for your thoughts. I hope to get back to my problem this weekend so hopefully can provide an update then. In response to your question, Tonygo2, I am hoping to use the same button for all mode selection and just step sequentially through the available set. The Plasma2040 provides three buttons for user control but I intend to use the other two for brightness control.

The basic structure would be something like this:


def one():
    #pattern A code here

def two():
    #pattern B code here

def three():
    #pattern C code here


effects = [one,two, three] 


def pincheck(): 
    global patterns
    #set patterns to a number (0 - 3) depending on button presses. 

while True:
    pincheck()
    effects[pattern]()

Hi to all who have contributed to my question about Exiting from a While Loop but I think I have solved my problem ( with some additional help from an ex work colleague). As I think should have become clear by now, I am no expert on Micropython and functionality rather than code efficiency is my primary concern. I now have reached a situation where, apart from a few timing issues which I think I can resolve, I have a programme which does what I set out to do. Once again, thank you all for your help.

This is the code segment which is the main While loop which gives sequential selection of an operating mode. Control of this programme is achieved by the use of the User Switch button (which I have renamed mode switch) on the Plasma2040:

“””
p = 0
while True:
print(p)
if mode_switch.read():
p += 1

    time.sleep(0.5)
    #print(p)

if (Operating_modes[p] == "White"):
        White()
        
if (Operating_modes[p] == "Red"):
        Red()
        
if (Operating_modes[p] == "Green"):
        Green()
        
if (Operating_modes[p] == "Blue"):
        Blue()
        
if (Operating_modes[p] == "Rainbows"):
        Rainbow()
    
if (Operating_modes[p] == "Blinkies"):
        blink(10, 135, 1)
    
if (Operating_modes[p] == "Fire"):
        fire()
        
if (Operating_modes[p] == "Thermometer"):
        Thermometer()
        
        
if p >= 7:
    p = 0

“””

and this is the code segment that implements a rainbow effect function and which allows me to exit to the calling While loop above:-

“””
def Rainbow():
SPEED = 10
offset = 0.0
UPDATES = 60
#ok = True
#while ok:

ok = True
while ok:
    
    SPEED = min(255, max(1, SPEED))
    offset += float(SPEED) / 2000.0
    for i in range(Num_Leds):
        hue = float(i) / Num_Leds
        led_strip.set_hsv(i, hue + offset, 1.0, 1.0)           
    time.sleep(1.0 / UPDATES)
    
    if mode_switch.read():
        time.sleep(1.0)
        ok = False 

“””

Nice but of code! The potential of the plasma 2040 is huge, once you know a little python you can do some fantastic work.