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():
The function can returns a random integer within the specified range (a, b).