title | layout | meta-description | share | author | about | cats | simple-description | date |
---|---|---|---|---|---|---|---|---|
Potentiometer and the Microbit |
text-width-sidebar |
How to create pretty, colourful patterns on the microbit in python with neopixels. How to use hue and not just RGB colour space. |
true |
jez |
Pretty patterns with neopixels on python. |
component examples |
Neopixel Examples |
2016-12-23 10:20:00 UTC |
Examples of some pretty neopixel paterns on the microbit in Python.
See the component article on this website about Neopixels.
{:.ui .header .dividing}
Create a list of values that range in hue. Every 100 milliseconds, shift the list by 1 and update the neopixel display.
{% highlight python %} from microbit import * import neopixel
def shift_left(lst, n): """Shifts the lst over by n indices
>>> lst = [1, 2, 3, 4, 5]
>>> shift_left(lst, 2)
>>> lst
[3, 4, 5, 1, 2]
"""
assert (n > 0), "n should be non-negative integer"
def shift(ntimes):
if ntimes == 0:
return
else:
temp = lst[0]
for index in range(len(lst) - 1):
lst[index] = lst[index + 1]
lst[index + 1] = temp
return shift(ntimes-1)
return shift(n)
def HSV_2_RGB(HSV): ''' Converts an integer HSV tuple (value range from 0 to 255) to an RGB tuple '''
# Unpack the HSV tuple for readability
H, S, V = HSV
# Check if the color is Grayscale
if S == 0:
R = V
G = V
B = V
return (R, G, B)
# Make hue 0-5
region = H // 43;
# Find remainder part, make it from 0-255
remainder = (H - (region * 43)) * 6;
# Calculate temp vars, doing integer multiplication
P = (V * (255 - S)) >> 8;
Q = (V * (255 - ((S * remainder) >> 8))) >> 8;
T = (V * (255 - ((S * (255 - remainder)) >> 8))) >> 8;
# Assign temp vars based on color cone region
if region == 0:
R = V
G = T
B = P
elif region == 1:
R = Q;
G = V;
B = P;
elif region == 2:
R = P;
G = V;
B = T;
elif region == 3:
R = P;
G = Q;
B = V;
elif region == 4:
R = T;
G = P;
B = V;
else:
R = V;
G = P;
B = Q;
return (R, G, B)
np = neopixel.NeoPixel(pin0, 24)
hue_step = round(255 / len(np))
for pixel in range(0, len(np)): hue = pixel * hue_step hsv = HSV_2_RGB((hue, 255,255)) np[pixel] = hsv print(np[pixel])
while True: shift_left(np, 1) np.show() sleep(100) {% endhighlight %}
- Idea from PXT
shift_left()
function from StackExchangeHSV_2_RGB()
function from Literate Programs
{% highlight javascript %} let neopixels = neopixel.create(DigitalPin.P0, 24, NeoPixelMode.RGB) neopixels.showRainbow(1, 360) basic.forever(() => { neopixels.rotate(1) // shift array neopixels.show() basic.pause(100) }) {% endhighlight %}