2
from picozero import pico_led, Button, LED, Buzzer
from time import sleep
led16 = LED(16)
button12 = Button(12, pull_up = False)
button15 = Button(15, pull_up = False)
buzzer14 = Buzzer(14)

global cont
cont = True

global count
count = 0


def cont_false():
    global count
    global cont
    if count >= 10:
        cont == False
        print(cont)

def Timer_start():
    led16.on()
    global cont
    global count
    sleep(5)
    while cont:
        buzzer14.toggle()
        sleep(0.2)
        buzzer14.toggle()
        count += 1
        print(count)
        sleep(0.3)

button15.when_pressed = Timer_start

button12.when_pressed = cont_false

while True:
    pico_led.toggle()
    sleep(0.5)

so its meant to be so that cont_false sets cont to false and stops the while loop in Timer_start but it does nothing

The While loop in Timer_start runs once outputting 1 then it keeps beeping but never outputs again

im using micro python on a Raspberry Pi Pico

2
  • This code is very strange. You're telling MicroPython to go into an infinite loop whenever button15 is pressed, which means it would never return to the main loop at the bottom and probably be unable to process any other button presses. I suspect the when_pressed functions are run in an interrupt, and it might be an interrupt that's higher priority than the USB stack, so it might interfere with USB communication. Maybe you could say what you're actually trying to do and we can help write more sensible code that achieves the goal. Commented Feb 6 at 19:41
  • It’s meant to be a egg timer for a computer science IGCSE course I’m doing I’m on my way to ask the teacher at this very moment though Commented Feb 7 at 10:25

2 Answers 2

0

You're trying to stop the while cont loop in the Timer_start() function when cont_false is triggered. However, the condition cont == False in cont_false() is a comparison, not an assignment, so it doesn't actually change the value of cont.

Additionally, cont_false() only checks count when button12 is pressed, but count will never reach 10 because the function isn't called automatically after each increment.

Fixed code:

from time import sleep

cont = True
count = 0

def cont_false():
    global cont
    cont = False  # Use '=' for assignment instead of '=='
    print("Timer stopped:", cont)

def Timer_start():
    global cont, count
    led16.on()
    cont = True   # Ensure the timer starts fresh each time
    count = 0     # Reset count for each new start

    sleep(5)
    while cont:
        buzzer14.toggle()
        sleep(0.2)
        buzzer14.toggle()
        count += 1
        print("Count:", count)
        sleep(0.3)

        # Check if count exceeds 10 to stop automatically
        if count >= 10:
            cont_false()

button15.when_pressed = Timer_start
button12.when_pressed = cont_false  # Manual stop

while True:
    pico_led.toggle()
    sleep(0.5)

2
  • quick thing i want it to beep at least 10 times and then allow for cont_false to happen i can do this by removing the if count >= 10: cont_false() and adding an if statement in the cont_false right? Commented Feb 3 at 17:10
  • so in the end when i removed the code which would end it after 10 times and added the if statement this happened Unable to connect to COM3: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5) If you have serial connection to the device from another program, then disconnect it there first. Process ended with exit code 1. no other programs are using it Commented Feb 3 at 17:16
0

I am not sure that will help but. If it's beeping, your loop is still running for sure. However, What does not loop is your button 2 handler. So in order, to make your loop stop you need to press on button12.

Furthermore, there is a double equal sign (==) at line 19. This can also explain what happens.

I hope it helped !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.