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 |2 Answers
Reset to default 0You'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 !
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