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 ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.