Neopixels (Part 6)

If you hold your finger down while the lights are on, the rainbow stops cycling. This is slightly disappointing. Let’s fix that, and at the same time prepare ourselves so we can have short presses and long presses.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import board
import time
import neopixel
from touchio import TouchIn

touch = TouchIn(board.A2)

# LED strip
pixels = neopixel.NeoPixel(board.D1, 3, brightness=1,
    auto_write=True, pixel_order=neopixel.GRBW)

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0):
        return [0, 0, 0]
    if (pos > 255):
        return [0, 0, 0]
    if (pos < 85):
        return [int(pos * 3), int(255 - (pos*3)), 0]
    elif (pos < 170):
        pos -= 85
        return [int(255 - pos*3), 0, int(pos*3)]
    else:
        pos -= 170
        return [0, int(pos*3), int(255 - pos*3)]

pressTime = 0
on = False
i = 0
while True:
    if touch.value and pressTime == 0:
        pressTime = time.monotonic()
    
    if not touch.value and pressTime != 0:
        holdTime = time.monotonic()-pressTime
        pressTime = 0
        if holdTime > 1:
            on = not on
    
    if on:
        pixels[0] = wheel(i)
        pixels[1] = (0, 255, 255, 0)
        pixels[2] = (0, 0, 0, 255)
    else:
        pixels.fill((0, 0, 0, 0))
    
    i = (i+1) % 256