Using Python to Control WS2812 RGB LED Flexi-Strip with Micro:bit

undefined
 
Using Python to Control
WS2812 RGB LED Flexi-Strip
 
MicroPython
undefined
 
WS2812 RGB LED Flexi-Strip
 
Neopixel strip 
is made up of a series of special LEDs
that can be controlled using only one IO.
WS2812 has an internal control chip which can control
the red, green, and blue LED. Every LED can show 
2
8
 =
256 kinds of color. With three colors combined, it can
show 256 x 256 x 256 = 16777216 kinds of color.
It’s very easy to control strip with micro:bit.
undefined
 
Experiment Equipment
 
micro:bit x 1
From 
grove inventor kit:
Base Shield
 x 1
WS2812 strip x 1
Micro USB cable(Android data cable)
Network-enabled computer
undefined
 
Hardware Connection
 
First, plug micro:bit into
 
Grove Shield for
micro:bit v2.0
Then, connect the strip to the port labeled
with P1/P15(It’s OK to use other port, only
if you modify the control port later)
Finally, connect micro:bit to PC via micro
USB cable.
undefined
 
Software
 
Chrome kernel web browser(version
greater than 67)
PythonEditor community
https://micropython.top/
Update DAPLink firmware of micro:bit to
greater than 0247 which support webusb.
undefined
 
Software
 
Using 
Chrome 
web browser
G
oto 
https://micropython.top
Click Blockly to change to 
Blocks Editor
undefined
 
D
ownload the program
 
After you finish writing the program, click
Flash and you can download the program
by webusb.
 
 
 
In the pop-up selection box, the program
will be downloaded and run automatically
after you select the device.
undefined
 
Download the program in
traditional way
 
If you haven’t update the firmware, you
have to download the HEX file and paste
it into microbit to run.
undefined
 
Blocks of Neopixel
 
From navbar on the leftside, you can find
a category named Neopixel, it has four
function to control the Neopixel.
undefined
 
Function Description of Blocks
 
These blocks will be used to set which and how
many pins controling the neopixels, turn off
lights, refresh color setting, and set colors of
neopixels.
You can use any pin to control neopixel, and
the quantity could be from 1 to 256.It’s OK if you
have more. However, micro:bit has small
memory, too large a number will make program
easier to go wrong and lights may go dim for
the power of Grove Shield is not large enogh.
undefined
 
Blocks Editing
 
Program below shows the basic way to control
neopixels. The case shows the pattern of red light
from dim to bright.
Use pin1 to control, the number of lights of
neopixels in the kit is 30.
undefined
 
Blocks & Code
 
When you are using blocks to program, the
corresponding Python code will be shown on the right
side of the page.
undefined
 
Program with Code
 
The picture below shows the corresponding code of the
blocks. Campare to using blocks to make program,
code is more concise and efficient.
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
d
i
s
p
l
a
y
.
s
h
o
w
(
I
m
a
g
e
.
H
E
A
R
T
)
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
f
o
r
 
i
 
i
n
 
r
a
n
g
e
(
3
0
)
:
 
 
n
p
[
i
]
 
=
 
(
i
,
 
0
,
 
0
)
n
p
.
s
h
o
w
(
)
undefined
 
Controlling Neopixels
 
From the previos program we can tell that the basic way
to control neopixels is:
Define the number of neopixels, set the controlling
pin and the number of neopixels(The number of
neopixlels from Grove kit is 30)
Set the color of light (one LED at a time)
Using function show() to light up the LED
We can see that it’s quite easy to control neopixels. Using
basic control and different algorithms to change the color
regularly, we can make different patterns. Now we’ll see
how it works.
undefined
 
Neopixels Program 1
Stars
Shining
 
P
a
t
t
e
r
n
:
 
L
E
D
s
 
o
f
 
t
h
e
 
n
e
o
p
i
x
e
l
s
 
l
i
g
h
t
 
u
p
r
a
n
d
o
m
l
y
,
 
m
i
m
i
c
 
t
h
e
 
w
a
y
 
o
f
 
s
t
a
r
s
 
s
h
i
n
i
n
g
a
t
 
n
i
g
h
t
.
H
o
w
 
t
o
 
m
a
k
e
 
i
t
 
w
o
r
k
:
Choose 3 LEDs from neopixel randomly
Set the color of these three LEDs randomly
Light
 
up
 
LED
s
 
quickly
 
to
 
mimic
 
the way stars
shine
Turn off LEDs after delay
undefined
 
Star Shining
undefined
 
Code of Star Shining
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
i
m
p
o
r
t
 
r
a
n
d
o
m
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
w
h
i
l
e
 
T
r
u
e
:
 
 
f
o
r
 
c
o
u
n
t
 
i
n
 
r
a
n
g
e
(
i
n
t
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
1
,
 
3
)
)
)
:
 
 
 
 
n
p
[
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
2
9
)
)
]
 
=
 
(
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
,
\
 
 
 
 
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
,
\
 
 
 
 
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
)
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
2
0
0
)
 
 
n
p
.
c
l
e
a
r
(
)
undefined
 
Neopixels Prgram 2: Breathing
Light
 
P
a
t
t
e
r
n
:
 
T
h
e
 
w
h
o
l
e
 
n
e
o
p
i
x
e
l
s
 
l
i
g
h
t
 
u
p
g
r
a
d
u
a
l
l
y
 
a
n
d
 
t
h
e
n
 
d
i
m
 
g
r
a
d
u
a
l
l
y
.
 
H
o
w
 
t
o
 
m
a
k
e
 
i
t
 
w
o
r
k
Increase or Decrease the color of LED(we
take red for example later)
Increase from 0 and decrease when it
reaches apex
 
undefined
 
Breathing Light
undefined
 
Code of Breathing Light
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
r
 
=
 
0
d
r
 
=
 
1
w
h
i
l
e
 
T
r
u
e
:
 
 
f
o
r
 
i
 
i
n
 
r
a
n
g
e
(
3
0
)
:
 
 
 
 
n
p
[
i
]
 
=
 
(
r
,
 
0
,
 
0
)
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
5
0
)
 
 
r
 
=
 
r
 
+
 
d
r
 
 
i
f
 
r
 
=
=
 
0
 
o
r
 
r
 
=
=
 
3
2
:
 
 
 
 
d
r
 
=
 
0
 
-
 
d
r
undefined
 
Neopixels Program 3
: Breathing
Light + Star Shining
 
P
a
t
t
e
r
n
:
 
C
o
m
b
i
n
e
 
t
h
e
 
l
a
s
t
 
t
w
o
e
x
a
m
p
l
e
s
,
t
o
 
s
h
o
w
 
t
h
a
t
 
r
a
n
d
o
m
 
l
i
g
h
t
s
 
l
i
g
h
t
u
p
 
a
n
d
 
d
i
m
 
g
r
a
d
u
a
l
l
y
.
H
o
w
 
t
o
 
m
a
k
e
 
i
t
 
w
o
r
k
s
Combine the methods we use in making star
shining and breathing light
Use random number to produce the change
in 3 colors
undefined
 
Breathing Light + Star Shining
undefined
 
Code of Breathing Light + Star
Shining
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
i
m
p
o
r
t
 
r
a
n
d
o
m
i
m
p
o
r
t
 
m
a
t
h
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
w
h
i
l
e
 
T
r
u
e
:
 
 
p
 
=
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
2
9
)
 
 
r
 
=
 
r
a
n
d
o
m
.
r
a
n
d
o
m
(
)
 
 
g
 
=
 
r
a
n
d
o
m
.
r
a
n
d
o
m
(
)
 
 
b
 
=
 
r
a
n
d
o
m
.
r
a
n
d
o
m
(
)
 
 
n
 
=
 
0
 
 
d
 
=
 
1
 
 
f
o
r
 
i
 
i
n
 
r
a
n
g
e
(
5
1
)
:
 
 
 
 
n
p
[
p
]
 
=
 
(
(
r
o
u
n
d
(
r
 
*
 
n
)
)
,
 
(
r
o
u
n
d
(
g
 
*
 
n
)
)
,
 
(
r
o
u
n
d
(
b
 
*
 
n
)
)
)
 
 
 
 
n
p
.
s
h
o
w
(
)
 
 
 
 
s
l
e
e
p
(
1
0
)
 
 
 
 
n
p
.
c
l
e
a
r
(
)
 
 
 
 
n
 
=
 
n
 
+
 
d
 
 
 
 
i
f
 
n
 
>
=
 
2
5
:
 
 
 
 
 
 
d
 
=
 
-
1
undefined
 
Neopixels Program4
Flow LED
 
P
a
t
t
e
r
n
:
 
L
i
g
h
t
 
u
p
 
L
E
D
s
 
o
n
 
t
h
e
 
s
t
r
i
p
 
o
n
e
b
y
 
o
n
e
,
 
t
o
 
m
a
k
e
 
i
t
 
l
o
o
k
 
l
i
k
e
 
t
h
e
 
l
i
g
h
t
s
 
a
r
e
f
l
o
w
i
n
g
H
o
w
 
t
o
 
m
a
k
e
 
i
t
 
w
o
r
k
Set a variable r, representing the position of LED
Set the color of LED, light up the strip, turn them
off after a delay
Increase 4, when reaching its maximum number
30, it becomes 0
Only one LED light up at a time, and one by one
undefined
 
Flow LED
undefined
 
Code of Flow LED
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
r
 
=
 
0
w
h
i
l
e
 
T
r
u
e
:
 
 
n
p
[
r
]
 
=
 
(
0
,
 
3
0
,
 
0
)
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
5
0
)
 
 
n
p
.
c
l
e
a
r
(
)
 
 
r
 
=
 
r
 
+
 
1
 
 
i
f
 
r
 
=
=
 
3
0
:
 
 
 
 
r
 
=
 
0
undefined
 
Change of Flow LED
 
Use different way to control flow LED, we can make
different patterns of flow LED.
Round Trip flow LED
Color changing flow LED
Variable speed flow LED
……
undefined
 
Color Changing Flow LED
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
i
m
p
o
r
t
 
r
a
n
d
o
m
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
c
 
=
 
(
0
,
 
3
0
,
 
0
)
w
h
i
l
e
 
T
r
u
e
:
 
 
n
p
[
r
]
 
=
 
c
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
5
0
)
 
 
n
p
.
c
l
e
a
r
(
)
 
 
r
 
=
 
r
 
+
 
1
 
 
i
f
 
r
 
=
=
 
3
0
:
 
 
 
 
r
 
=
 
0
 
 
 
 
c
 
=
 
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
undefined
 
Variable Speed Flow LED
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
i
m
p
o
r
t
 
r
a
n
d
o
m
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
c
 
=
 
(
0
,
 
3
0
,
 
0
)
d
 
=
 
5
0
w
h
i
l
e
 
T
r
u
e
:
 
 
n
p
[
r
]
 
=
 
c
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
d
)
 
 
n
p
.
c
l
e
a
r
(
)
 
 
r
 
=
 
r
 
+
 
1
 
 
i
f
 
r
 
=
=
 
3
0
:
 
 
 
 
r
 
=
 
0
 
 
 
 
c
 
=
 
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
 
 
 
 
d
 
=
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
2
0
,
 
1
2
0
)
undefined
 
Round Trip Flow LED
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
i
m
p
o
r
t
 
r
a
n
d
o
m
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
,
 
3
0
)
c
 
=
 
(
0
,
 
3
0
,
 
0
)
d
r
 
=
 
1
w
h
i
l
e
 
T
r
u
e
:
 
 
n
p
[
r
]
 
=
 
c
 
 
n
p
.
s
h
o
w
(
)
 
 
s
l
e
e
p
(
5
0
)
 
 
n
p
.
c
l
e
a
r
(
)
 
 
r
 
=
 
r
 
+
 
d
r
 
 
i
f
 
r
 
=
=
 
2
9
 
o
r
 
r
 
=
=
 
0
:
 
 
 
 
d
r
 
=
 
-
d
r
 
 
 
 
c
 
=
 
(
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
,
\
 
 
 
 
 
 
 
 
 
r
a
n
d
o
m
.
r
a
n
d
i
n
t
(
0
,
 
3
2
)
)
undefined
 
Neopixels Program
Spinning
Rainbow
 
P
a
t
t
e
r
n
:
 
S
e
t
 
t
h
e
 
c
o
l
o
r
 
o
f
 
s
t
r
i
p
 
t
o
 
g
r
a
d
i
e
n
t
 
r
a
i
n
b
o
w
 
c
o
l
o
r
,
 
a
n
d
 
s
h
o
w
i
n
 
s
p
i
n
n
i
n
g
 
w
a
y
.
 
H
o
w
 
t
o
 
m
a
k
e
 
i
t
 
w
o
r
k
s
Define function 
np_rainbow(np, num, bright, offset) 
to set the
color of the strip.
num is the number of LEDs in the strip
bright is the greatest brightness
of
fset is the starting position
Divide the strip into 7 equally, set the color of each LED
according to the color of the rainbow.
By decreasing 
offset to make the effect of spinning
undefined
 
Code
 
of
 
Spinning Rainbow
f
r
o
m
 
m
i
c
r
o
b
i
t
 
i
m
p
o
r
t
 
*
i
m
p
o
r
t
 
n
e
o
p
i
x
e
l
n
p
 
=
 
n
e
o
p
i
x
e
l
.
N
e
o
P
i
x
e
l
(
p
i
n
1
 
,
3
2
)
d
e
f
 
n
p
_
r
a
i
n
b
o
w
(
n
p
,
 
n
u
m
,
 
b
r
i
g
h
t
=
3
2
,
 
o
f
f
s
e
t
 
=
 
0
)
:
 
 
 
 
r
b
 
=
 
(
(
2
5
5
,
0
,
0
)
,
 
(
2
5
5
,
1
2
7
,
0
)
,
 
(
2
5
5
,
2
5
5
,
0
)
,
 
(
0
,
2
5
5
,
0
)
,
\
 
 
 
 
 
 
 
 
 
 
(
0
,
2
5
5
,
2
5
5
)
,
 
(
0
,
0
,
2
5
5
)
,
 
(
1
3
6
,
0
,
2
5
5
)
,
 
(
2
5
5
,
0
,
0
)
)
 
 
 
 
f
o
r
 
i
 
i
n
 
r
a
n
g
e
(
n
u
m
)
:
 
 
 
 
 
 
 
 
t
 
=
 
7
*
i
/
n
u
m
 
 
 
 
 
 
 
 
t
0
 
=
 
i
n
t
(
t
)
 
 
 
 
 
 
 
 
r
 
=
 
r
o
u
n
d
(
(
r
b
[
t
0
]
[
0
]
 
+
 
(
t
-
t
0
)
*
(
r
b
[
t
0
+
1
]
[
0
]
-
r
b
[
t
0
]
[
0
]
)
)
*
b
r
i
g
h
t
)
>
>
8
 
 
 
 
 
 
 
 
g
 
=
 
r
o
u
n
d
(
(
r
b
[
t
0
]
[
1
]
 
+
 
(
t
-
t
0
)
*
(
r
b
[
t
0
+
1
]
[
1
]
-
r
b
[
t
0
]
[
1
]
)
)
*
b
r
i
g
h
t
)
>
>
8
 
 
 
 
 
 
 
 
b
 
=
 
r
o
u
n
d
(
(
r
b
[
t
0
]
[
2
]
 
+
 
(
t
-
t
0
)
*
(
r
b
[
t
0
+
1
]
[
2
]
-
r
b
[
t
0
]
[
2
]
)
)
*
b
r
i
g
h
t
)
>
>
8
 
 
 
 
 
 
 
 
n
p
[
(
i
+
o
f
f
s
e
t
)
%
n
u
m
]
 
=
 
(
r
,
 
g
,
 
b
)
n
 
=
 
0
w
h
i
l
e
 
1
:
 
 
 
 
n
p
_
r
a
i
n
b
o
w
(
n
p
,
 
3
0
,
 
o
f
f
s
e
t
 
=
 
n
)
 
 
 
 
n
p
.
s
h
o
w
(
)
 
 
 
 
n
 
+
=
 
1
 
 
 
 
s
l
e
e
p
(
3
0
)
Slide Note
Embed
Share

Explore how to control a WS2812 RGB LED Flexi-Strip using MicroPython with a Micro:bit and a Grove Inventor Kit. Learn about hardware connections, software setup, program download methods, and utilizing Neopixel blocks for LED control.

  • Python
  • LED Strip
  • MicroPython
  • Micro:bit
  • Neopixel

Uploaded on Sep 25, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Using Python to Control WS2812 RGB LED Flexi-Strip MicroPython

  2. WS2812 RGB LED Flexi-Strip Neopixel strip is made up of a series of special LEDs that can be controlled using only one IO. WS2812 has an internal control chip which can control the red, green, and blue LED. Every LED can show 28= 256 kinds of color. With three colors combined, it can show 256 x 256 x 256 = 16777216 kinds of color. It s very easy to control strip with micro:bit.

  3. Experiment Equipment micro:bit x 1 From grove inventor kit: Base Shield x 1 WS2812 strip x 1 Micro USB cable(Android data cable) Network-enabled computer

  4. Hardware Connection First, plug micro:bit into Grove Shield for micro:bit v2.0 Then, connect the strip to the port labeled with P1/P15(It s OK to use other port, only if you modify the control port later) Finally, connect micro:bit to PC via micro USB cable.

  5. Software Chrome kernel web browser(version greater than 67) PythonEditor community https://micropython.top/ Update DAPLink firmware of micro:bit to greater than 0247 which support webusb.

  6. Software Using Chrome web browser Goto https://micropython.top Click Blockly to change to Blocks Editor

  7. Download the program After you finish writing the program, click Flash and you can download the program by webusb. In the pop-up selection box, the program will be downloaded and run automatically after you select the device.

  8. Download the program in traditional way If you haven t update the firmware, you have to download the HEX file and paste it into microbit to run.

  9. Blocks of Neopixel From navbar on the leftside, you can find a category named Neopixel, it has four function to control the Neopixel.

  10. Function Description of Blocks These blocks will be used to set which and how many pins controling the neopixels, turn off lights, refresh color setting, and set colors of neopixels. You can use any pin to control neopixel, and the quantity could be from 1 to 256.It s OK if you have more. However, micro:bit has small memory, too large a number will make program easier to go wrong and lights may go dim for the power of Grove Shield is not large enogh.

  11. Blocks Editing Program below shows the basic way to control neopixels. The case shows the pattern of red light from dim to bright. Use pin1 to control, the number of lights of neopixels in the kit is 30.

  12. Blocks & Code When you are using blocks to program, the corresponding Python code will be shown on the right side of the page.

  13. Program with Code The picture below shows the corresponding code of the blocks. Campare to using blocks to make program, code is more concise and efficient. from microbit import * import neopixel display.show(Image.HEART) np = neopixel.NeoPixel(pin1, 30) for i in range(30): np[i] = (i, 0, 0) np.show()

  14. Controlling Neopixels From the previos program we can tell that the basic way to control neopixels is: Define the number of neopixels, set the controlling pin and the number of neopixels(The number of neopixlels from Grove kit is 30) Set the color of light (one LED at a time) Using function show() to light up the LED We can see that it s quite easy to control neopixels. Using basic control and different algorithms to change the color regularly, we can make different patterns. Now we ll see how it works.

  15. Neopixels Program 1Stars Shining Pattern: LEDs of the neopixels light up randomly, mimic the way of stars shining at night. How to make it work: Choose 3 LEDs from neopixel randomly Set the color of these three LEDs randomly Light up LEDs quickly to mimic the way stars shine Turn off LEDs after delay

  16. Star Shining

  17. Code of Star Shining import neopixel import random from microbit import * np = neopixel.NeoPixel(pin1, 30) while True: for count in range(int(random.randint(1, 3))): np[(random.randint(0, 29))] = ((random.randint(0, 32)),\ (random.randint(0, 32)),\ (random.randint(0, 32))) np.show() sleep(200) np.clear()

  18. Neopixels Prgram 2: Breathing Light Pattern: The whole neopixels light up gradually and then dim gradually. How to make it work Increase or Decrease the color of LED(we take red for example later) Increase from 0 and decrease when it reaches apex

  19. Breathing Light

  20. Code of Breathing Light import neopixel from microbit import * np = neopixel.NeoPixel(pin1, 30) r = 0 dr = 1 while True: for i in range(30): np[i] = (r, 0, 0) np.show() sleep(50) r = r + dr if r == 0 or r == 32: dr = 0 - dr

  21. Neopixels Program 3: Breathing Light + Star Shining Pattern: Combine the last two examples,to show that random lights light up and dim gradually. How to make it works Combine the methods we use in making star shining and breathing light Use random number to produce the change in 3 colors

  22. Breathing Light + Star Shining

  23. Code of Breathing Light + Star Shining import neopixel import random import math from microbit import * np = neopixel.NeoPixel(pin1, 30) while True: p = random.randint(0, 29) r = random.random() g = random.random() b = random.random() n = 0 d = 1 for i in range(51): np[p] = ((round(r * n)), (round(g * n)), (round(b * n))) np.show() sleep(10) np.clear() n = n + d if n >= 25: d = -1

  24. Neopixels Program4Flow LED Pattern: Light up LEDs on the strip one by one, to make it look like the lights are flowing How to make it work Set a variable r, representing the position of LED Set the color of LED, light up the strip, turn them off after a delay Increase 4, when reaching its maximum number 30, it becomes 0 Only one LED light up at a time, and one by one

  25. Flow LED

  26. Code of Flow LED import neopixel from microbit import * np = neopixel.NeoPixel(pin1, 30) r = 0 while True: np[r] = (0, 30, 0) np.show() sleep(50) np.clear() r = r + 1 if r == 30: r = 0

  27. Change of Flow LED Use different way to control flow LED, we can make different patterns of flow LED. Round Trip flow LED Color changing flow LED Variable speed flow LED

  28. Color Changing Flow LED import neopixel import random from microbit import * np = neopixel.NeoPixel(pin1, 30) c = (0, 30, 0) while True: np[r] = c np.show() sleep(50) np.clear() r = r + 1 if r == 30: r = 0 c = (random.randint(0, 32),\ random.randint(0, 32),\ random.randint(0, 32))

  29. Variable Speed Flow LED import neopixel import random from microbit import * np = neopixel.NeoPixel(pin1, 30) c = (0, 30, 0) d = 50 while True: np[r] = c np.show() sleep(d) np.clear() r = r + 1 if r == 30: r = 0 c = (random.randint(0, 32),\ random.randint(0, 32),\ random.randint(0, 32)) d = random.randint(20, 120)

  30. Round Trip Flow LED import neopixel import random from microbit import * np = neopixel.NeoPixel(pin1, 30) c = (0, 30, 0) dr = 1 while True: np[r] = c np.show() sleep(50) np.clear() r = r + dr if r == 29 or r == 0: dr = -dr c = (random.randint(0, 32),\ random.randint(0, 32),\ random.randint(0, 32))

  31. Neopixels ProgramSpinning Rainbow Pattern: Set the color of strip to gradient rainbow color, and show in spinning way. How to make it works Define function np_rainbow(np, num, bright, offset) to set the color of the strip. num is the number of LEDs in the strip bright is the greatest brightness offset is the starting position Divide the strip into 7 equally, set the color of each LED according to the color of the rainbow. By decreasing offset to make the effect of spinning

  32. Code of Spinning Rainbow from microbit import * import neopixel np = neopixel.NeoPixel(pin1 ,32) def np_rainbow(np, num, bright=32, offset = 0): rb = ((255,0,0), (255,127,0), (255,255,0), (0,255,0),\ (0,255,255), (0,0,255), (136,0,255), (255,0,0)) for i in range(num): t = 7*i/num t0 = int(t) r = round((rb[t0][0] + (t-t0)*(rb[t0+1][0]-rb[t0][0]))*bright)>>8 g = round((rb[t0][1] + (t-t0)*(rb[t0+1][1]-rb[t0][1]))*bright)>>8 b = round((rb[t0][2] + (t-t0)*(rb[t0+1][2]-rb[t0][2]))*bright)>>8 np[(i+offset)%num] = (r, g, b) n = 0 while 1: np_rainbow(np, 30, offset = n) np.show() n += 1 sleep(30)

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#