EasyManua.ls Logo

Freenove Ultimate Starter Kit - Page 87

Freenove Ultimate Starter Kit
286 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
87
Chapter 5 RGBLED
█ www.freenove.com
support@freenove.com █
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
w hile Tr 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
print ('r=%d, g=%d, b=%d ' %(r ,g, b))
time.sleep(0.3)
def destroy():
p_R. stop()
p_G. stop()
p_B. stop()
GPIO. cleanup()
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
e xcept 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.
def loop():
w hile Tr ue :
r= random.randint(0,100)
g= random.randint(0,100)
b= random.randint(0,100)
setColor(r,g,b)
print ('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).

Table of Contents