最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

micropython on picoboard while loop not working - Stack Overflow

programmeradmin0浏览0评论
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

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

Share Improve this question asked Feb 3 at 16:35 Lars Falc0nLars Falc0n 211 silver badge1 bronze badge 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. – David Grayson 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 – Lars Falc0n Commented Feb 7 at 10:25
Add a comment  | 

2 Answers 2

Reset to default 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)

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 !

发布评论

评论列表(0)

  1. 暂无评论