Until now, we have only displayed a single color at a time on one or more of the LEDs. If you consider how the circuit is wired up, you might wonder how we can get one LED to display one color and another a different one at the same time. The simple answer is that we don't need to-we just do it quickly!
All we need to do is display one color at a time, but change it back and forth, so quickly that the color looks like a mix of the two (or even a combination of the three red/green/blue LEDs). Fortunately, this is something that computers such as the Raspberry Pi can do very easily, even allowing us to combine the RGB elements to make multiple shades of colors across all five LEDs. Perform the following steps to mix the colors:
- Add combo color definitions to the top of the rgbled.py script, after the definition of the mixed colors, using the following code:
#Combo Colors RGB_AQUA = [RGB_CYAN,RGB_GREEN] RGB_LBLUE = [RGB_CYAN,RGB_BLUE] RGB_PINK = [RGB_MAGENTA,RGB_RED] RGB_PURPLE = [RGB_MAGENTA,RGB_BLUE] RGB_ORANGE = [RGB_YELLOW,RGB_RED] RGB_LIME = [RGB_YELLOW,RGB_GREEN] RGB_COLORS = [RGB_LIME,RGB_YELLOW,RGB_ORANGE,RGB_RED, RGB_PINK,RGB_MAGENTA,RGB_PURPLE,RGB_BLUE, RGB_LBLUE,RGB_CYAN,RGB_AQUA,RGB_GREEN]
The preceding code will provide the combination of colors needed to create our shades, with RGB_COLORS providing a smooth progression through the shades.
- Next, we need to create a function called led_combo() to handle single or multiple colors. The code for the function will be as follows:
def led_combo(pins,colors,period): #determine if "colors" is a single integer or not if isinstance(colors,int): #Single integer - reference directly led_time(pins,colors,period) else: #if not, then cycle through the "colors" list for i in colors: led_time(pins,i,period)
- Now we can create a new script, rgbledrainbow.py, to make use of the new functions in our rgbled.py module. The rgbledrainbow.py script will be as follows:
#!/usr/bin/python3 #rgbledrainbow.py import time import rgbled as RGBLED def next_value(number,max): number = number % max return number def main(): print ("Setup the RGB module") RGBLED.led_setup() # Multiple LEDs with different Colors print ("Switch on Rainbow") led_num = 0 col_num = 0 for l in range(5): print ("Cycle LEDs") for k in range(100): #Set the starting point for the next set of colors col_num = next_value(col_num+1,len(RGBLED.RGB_COLORS)) for i in range(20): #cycle time for j in range(5): #led cycle led_num = next_value(j,len(RGBLED.LED)) led_color = next_value(col_num+led_num, len(RGBLED.RGB_COLORS)) RGBLED.led_combo(RGBLED.LED[led_num], RGBLED.RGB_COLORS[led_color],0.001) print ("Cycle COLORs") for k in range(100): #Set the next color col_num = next_value(col_num+1,len(RGBLED.RGB_COLORS)) for i in range(20): #cycle time for j in range(5): #led cycle led_num = next_value(j,len(RGBLED.LED)) RGBLED.led_combo(RGBLED.LED[led_num], RGBLED.RGB_COLORS[col_num],0.001) print ("Finished") if __name__=='__main__': try: main() finally: RGBLED.led_cleanup() #End
The main() function will first cycle through the LEDs, setting each color from the RGB_COLORS array on all the LEDs. Then, it will cycle through the colors, creating a rainbow effect across the LEDs: