I am working with the display pack2. I am trying to output to the onboard LED when button A is pressed but it’s not working, I have tried a number of variations and so far this doesn’t bring up any errors but it doesn’t work. The code is below, and I have ‘#’ where there are changes .Any suggestions welcome Nick `
# This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed.
# I have modified the code to ouput to the on board LED but its not working yet.......led1 .
from machine import Pin, Timer
import time
import utime
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P4
from pimoroni import RGBLED
led1_Pin =Pin(25,Pin.OUT) #output to onboard led hopefully
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)
display.set_backlight(0.5)
display.set_font("bitmap8")
led = RGBLED(6,7,8)
led1_Pin = Pin(25,Pin.OUT) #onboard led
timer = Timer()
def blink(timer):
led1.toggle()
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)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)
# sets up a handy function we can call to clear the screen
def clear():
display.set_pen(BLACK)
display.clear()
display.update()
# set up
clear()
while True:
if button_a.read(): # if a button press is detected then...
clear() # clear to black
display.set_pen(WHITE) # change the pen colour
display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen
display.update() # update the display
led.set_rgb(255,255,255)
led1_Pin.value(1) # onboard led
led1_Pin.toggle() # onboard led
time.sleep(1) # pause for a sec
clear() # clear to black again
elif button_b.read():
clear()
display.set_pen(CYAN)
display.text("Button B pressed", 10, 10, 240, 4)
display.update()
led.set_rgb(0,255,255)
time.sleep(1)
clear()
elif button_x.read():
clear()
display.set_pen(MAGENTA)
display.text("Button X pressed", 10, 10, 240, 4)
display.update()
led.set_rgb(255,0,255)
time.sleep(1)
clear()
elif button_y.read():
clear()
display.set_pen(YELLOW)
display.text("Button Y pressed", 10, 10, 240, 4)
display.update()
led.set_rgb(255,255,0)
time.sleep(1)
clear()
else:
display.set_pen(GREEN)
display.text("Press any button!", 10, 10, 240, 4)
display.update()
led.set_rgb(0,255,0)
time.sleep(0.1) # this number is how frequently the Pico checks for button presses