Not sure why I am getting this error.
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
Traceback (most recent call last):
File "<stdin>", line 61, in <module>
ValueError: create_pen failed. No matching colour or space in palette!
>>>
This is my code:
import time
import gc
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P8
from pimoroni import RGBLED
import machine
# Turn of RGB LED
led = RGBLED(6, 7, 8)
led.set_rgb(0, 0, 0)
# Setup display 2.0
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P8)
display.set_backlight(1.0)
WIDTH, HEIGHT = display.get_bounds()
# Change the font
display.set_font("bitmap8")
BLACK = display.create_pen(0,0,0)
# Set up the ADCs with a list
adcs = [] # An empty list of ADC pins
for i in range(3):
adc = machine.ADC(26 + i) # Pins GP26, GP27 & GP28
adcs.append(adc) # Add the new ADC pin to the list
def pot_adj(adc, minn, maxx): # Function to rescale a potentiometer reading
pot_min = 800 # Take lowest readings as 0
pot_range = 65535 - pot_min
req_range = maxx - minn
# Read the raw potentiometer value from specified potentiometer
pot = adcs[adc].read_u16()
# Re-scale
result = ((pot - pot_min) * req_range / pot_range) + minn
result = int(result) # Ensure it is an Integer – whole number
# Adjust end points as necessary
if result < minn: # Bottom end – set to minn if too low
result = minn
if result > maxx: # Top end – set to maxx if too big
result = maxx
return result # Output the result to main program
# A few procedures to be used later
def wait(z): # delay a while
time.sleep(z)
def clean(): # Clear the screen to Black
display.set_pen(BLACK)
display.clear()
clean()
while True:
r = pot_adj(2, 0, 255)
g = pot_adj(1, 0, 255)
b = pot_adj(0, 0, 255)
gc.collect()
colour = display.create_pen(r,g,b)
time.sleep(0.05)
display.set_pen(colour)
display.circle(120,160,50)
display.update()
I’m using a Pico 2 W and Pimoroni Display 2 on a Pico Decker with 10 K pots on the ADCs.
I’ve been mixing colours from values from potentiometer for years and not met this problem before. I use the procedure to overcome the ‘jitter’ at the low end of ADC input not providing a proper zero.
I normally calculate the colour value as an appropriate bit pattern and poke that into the framebuffer but here a colour definition appears to be appended to the palette each time we redefine a colour with the same name, rather than replacing it with the new definition of the colour. After a finite number of colour changes we run out of space.
Perhaps an extra instruction to ‘pop’ the last colour from the palette so that it could then be redefined and auto appened as usual?
Can anyone think of a way round this problem?