Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4467

General discussion • Help with my very first project

$
0
0
I recently bought a Raspberry Pi Pico WH and a Pimoroni Pico Display. I'm coding my first project with it, using the bones of one of the Pimoroni programs that they start you off with.

I'm trying to make a countdown timer that starts at different times depending on which button you press. I'm getting everything to display correctly, but when I try to introduce a while loop into my code, it breaks after I press the button. (The starting screen still loads fine.)

I'm completely inexperience with Raspberry Pi and only a little experience with python. Can someone please tell me where I'm going wrong? I've tried so many iterations, and it just seems to me like it hates the word while.

Code:

import timefrom pimoroni import Buttonfrom pimoroni import RGBLEDfrom picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)display.set_backlight(0.5)display.set_font("bitmap8")button_a = Button(12)button_b = Button(13)button_x = Button(14)button_y = Button(15)WHITE = display.create_pen(255, 255, 255)BLACK = display.create_pen(0, 0, 0)CYAN = display.create_pen(0, 255, 255)MAGENTA = display.create_pen(255, 0, 255)RED = display.create_pen(255, 0, 10)YELLOW = display.create_pen(255, 255, 0)GREEN = display.create_pen(0, 255, 0)led = RGBLED(6,7,8)# sets up a handy function we can call to clear the screendef clear():    display.set_pen(BLACK)    display.clear()    display.update()    def countdown(x):    while x > 0:        display.set_pen(RED)        display.text("T-"+"{:.0f}".format(x),50,50,240, 6)        display.update        x = x - 1        if x == 0:                display.text("BOOM",50,50,240,8)        time.sleep(1)# set upclear()led.set_rgb(0,0,0)while True:    if button_a.read():                                   # if a button press is detected then...        clear()                                           # clear to black        display.set_pen(RED)                            # change the pen colour        countdown(10)        display.update()                                  # update the display        time.sleep(1)                                     # pause for a sec        clear()                                           # clear to black again    elif button_b.read():        clear()    elif button_x.read():        clear()    elif button_y.read():        clear()    else:        display.set_pen(WHITE)                                  display.text("Countdown from what?", 25, 60, 1000, 2.5)         display.text("10",2,10,1,3)        display.text("15",2,100,1,3)        display.text("20",210,10,1,3)        display.text("25",210,100,1,3)        display.update()                                    time.sleep(0.1)  

Statistics: Posted by LHSiragusa — Wed Apr 03, 2024 11:55 pm — Replies 1 — Views 67



Viewing all articles
Browse latest Browse all 4467

Trending Articles