1- # CircuitPlaygroundExpress_NeoPixel
2-
1+ # Circuit Playground NeoPixel
32import time
4-
53import board
64import neopixel
75
8- pixels = neopixel .NeoPixel (board .NEOPIXEL , 10 , brightness = .2 )
9- pixels .fill ((0 , 0 , 0 ))
10- pixels .show ()
6+ pixels = neopixel .NeoPixel (board .NEOPIXEL , 10 , brightness = 0.2 , auto_write = False )
117
128# choose which demos to play
139# 1 means play, 0 means don't!
14- simpleCircleDemo = 1
15- flashDemo = 1
16- rainbowDemo = 1
17- rainbowCycleDemo = 1
10+ color_chase_demo = 1
11+ flash_demo = 1
12+ rainbow_demo = 1
13+ rainbow_cycle_demo = 1
1814
1915
2016def wheel (pos ):
2117 # Input a value 0 to 255 to get a color value.
2218 # The colours are a transition r - g - b - back to r.
19+ if pos < 0 or pos > 255 :
20+ return (0 , 0 , 0 )
2321 if pos < 85 :
24- return (int ( pos * 3 ), int ( 255 - ( pos * 3 )) , 0 )
25- elif pos < 170 :
22+ return (255 - pos * 3 , pos * 3 , 0 )
23+ if pos < 170 :
2624 pos -= 85
27- return (int (255 - (pos * 3 )), 0 , int (pos * 3 ))
28- else :
29- pos -= 170
30- return (0 , int (pos * 3 ), int (255 - pos * 3 ))
25+ return (0 , 255 - pos * 3 , pos * 3 )
26+ pos -= 170
27+ return (pos * 3 , 0 , 255 - pos * 3 )
28+
29+
30+ def color_chase (color , wait ):
31+ for i in range (10 ):
32+ pixels [i ] = color
33+ time .sleep (wait )
34+ pixels .show ()
35+ time .sleep (0.5 )
3136
3237
3338def rainbow_cycle (wait ):
3439 for j in range (255 ):
35- for i in range (len ( pixels ) ):
36- idx = int (( i * 256 / len ( pixels )) + j * 10 )
37- pixels [i ] = wheel (idx & 255 )
40+ for i in range (10 ):
41+ rc_index = ( i * 256 // 10 ) + j * 5
42+ pixels [i ] = wheel (rc_index & 255 )
3843 pixels .show ()
3944 time .sleep (wait )
4045
@@ -48,78 +53,42 @@ def rainbow(wait):
4853 time .sleep (wait )
4954
5055
51- def simpleCircle (wait ):
52- RED = 0x100000 # (0x10, 0, 0) also works
53- YELLOW = (0x10 , 0x10 , 0 )
54- GREEN = (0 , 0x10 , 0 )
55- AQUA = (0 , 0x10 , 0x10 )
56- BLUE = (0 , 0 , 0x10 )
57- PURPLE = (0x10 , 0 , 0x10 )
58- BLACK = (0 , 0 , 0 )
59-
60- for i in range (len (pixels )):
61- pixels [i ] = RED
62- time .sleep (wait )
63- time .sleep (1 )
64-
65- for i in range (len (pixels )):
66- pixels [i ] = YELLOW
67- time .sleep (wait )
68- time .sleep (1 )
69-
70- for i in range (len (pixels )):
71- pixels [i ] = GREEN
72- time .sleep (wait )
73- time .sleep (1 )
74-
75- for i in range (len (pixels )):
76- pixels [i ] = AQUA
77- time .sleep (wait )
78- time .sleep (1 )
79-
80- for i in range (len (pixels )):
81- pixels [i ] = BLUE
82- time .sleep (wait )
83- time .sleep (1 )
84-
85- for i in range (len (pixels )):
86- pixels [i ] = PURPLE
87- time .sleep (wait )
88- time .sleep (1 )
89-
90- for i in range (len (pixels )):
91- pixels [i ] = BLACK
92- time .sleep (wait )
93- time .sleep (1 )
94-
56+ RED = (255 , 0 , 0 )
57+ YELLOW = (255 , 150 , 0 )
58+ GREEN = (0 , 255 , 0 )
59+ CYAN = (0 , 255 , 255 )
60+ BLUE = (0 , 0 , 255 )
61+ PURPLE = (180 , 0 , 255 )
62+ WHITE = (255 , 255 , 255 )
63+ OFF = (0 , 0 , 0 )
9564
9665while True :
97- if simpleCircleDemo :
98- print ('Simple Circle Demo' )
99- simpleCircle (.05 )
100-
101- if flashDemo : # this will play if flashDemo = 1 up above
102- print ('Flash Demo' )
103- pixels .fill ((255 , 0 , 0 ))
66+ if color_chase_demo :
67+ color_chase (RED , 0.1 ) # Increase the number to slow down the color chase
68+ color_chase (YELLOW , 0.1 )
69+ color_chase (GREEN , 0.1 )
70+ color_chase (CYAN , 0.1 )
71+ color_chase (BLUE , 0.1 )
72+ color_chase (PURPLE , 0.1 )
73+ color_chase (OFF , 0.1 )
74+
75+ if flash_demo :
76+ pixels .fill (RED )
10477 pixels .show ()
105- time . sleep ( .25 )
106-
107- pixels .fill (( 0 , 255 , 0 ) )
78+ # Increase or decrease to change the speed of the solid color change.
79+ time . sleep ( 1 )
80+ pixels .fill (GREEN )
10881 pixels .show ()
109- time .sleep (.25 )
110-
111- pixels .fill ((0 , 0 , 255 ))
82+ time .sleep (1 )
83+ pixels .fill (BLUE )
11284 pixels .show ()
113- time .sleep (.25 )
114-
115- pixels .fill ((255 , 255 , 255 ))
85+ time .sleep (1 )
86+ pixels .fill (WHITE )
11687 pixels .show ()
117- time .sleep (.25 )
88+ time .sleep (1 )
11889
119- if rainbowDemo :
120- print ('Rainbow Demo' )
121- rainbow (.001 )
90+ if rainbow_cycle_demo :
91+ rainbow_cycle (0.05 ) # Increase the number to slow down the rainbow.
12292
123- if rainbowCycleDemo :
124- print ('Rainbow Cycle Demo' )
125- rainbow_cycle (.001 )
93+ if rainbow_demo :
94+ rainbow (0.05 ) # Increase the number to slow down the rainbow.
0 commit comments