87
Chapter 5 RGBLED
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
w h ile
T r ue
:
r
=
random
.
randint
(
0
,
100
)
#get a random in (0,100)
g
=
random
.
randint
(
0
,
100
)
b
=
random
.
randint
(
0
,
100
)
setColor
(
r
,
g
,
b
)
#set random as a duty cycle value
p r int
(
'r=%d, g=%d, b=%d '
% (
r
,
g
,
b
) )
time
.
sleep
(
0.3
)
d e f
destroy
():
p_R
.
stop
()
p_G
.
stop
()
p_B
.
stop
()
GPIO
.
cleanup
()
i f
__name__
= =
'__main__'
:
# Program start from here
setup
()
t r y
:
loop
( )
e x cept
KeyboardInterrupt
:
# When 'Ctrl+C' is pressed, the subprogram destroy() will
be executed.
destroy
()
In last chapter, we have learned how to use python language to make a pin output PWM. In this project, we
let three pins output PWM, and the usage is exactly the same as last chapter. In the “while” cycle of “loop”
function, we first obtain three random numbers, and then specify these three random numbers as the PWM
value of the three pins.o that the RGBLED switching of different colors randomly.
d e f
loop
( ):
w h ile
T r ue
:
r
=
random
.
randint
(
0
,
100
)
g
=
random
.
randint
(
0
,
100
)
b
=
random
.
randint
(
0
,
100
)
setColor
(
r
,
g
,
b
)
p r int
(
'r=%d, g=%d, b=%d '
% (
r
,
g
,
b
) )
time
.
sleep
(
0.3
)
About function randint():
random.randint(a, b)
The function can returns a random integer within the specified range (a, b).
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...