Displaying random patterns

We can add new functions to our library to produce different effects, such as generating random colors. The following function uses randint() to get a value between 1 and the number of colors. We ignore any values that are over the number of the available colors so that we can control how often the LEDs are switched off. Perform the following steps to add the required functions:

  1. Add the randint() function from the random module to the rgbled.py script using the following code:
from random import randint
  1. Now add led_rgbrandom() using the following code:
def led_rgbrandom(led,period,colors): 
   ''' Light up the selected led, for period in seconds, 
   in one of the possible colors. The colors can be 
   1 to 3 for RGB, or 1-6 for RGB plus combinations, 
   1-7 includes white. Anything over 7 will be set as 
   OFF (larger the number more chance of OFF).'''  
  value = randint(1,colors) 
  if value < len(RGB_LIST): 
    led_time(led,RGB_LIST[value-1],period) 
  1. Use the following commands in the main() function to create a series of
    flashing LEDs:
for i in range(20): 
  for j in LED: 
    #Select from all, plus OFF 
    led_rgbrandom(j,0.1,20) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.117.214