# Red light, green light, blue light!

CircuitPython is Python that runs on microcontrollers, including your Gemma board! To learn more about CircuitPython, check out the Welcome to CircuitPython and CircuitPython Essentials guides. This guide uses CircuitPython and Gemma.

This project is a simple one that requires touch interaction from you! There are three touch pads on your Gemma, and it just so happens that there are three colors in the built-in RGB LED! So, we've written up a program using CircuitPython that allows you to control the red level from the first pad, the green level from the second pad, and the blue level from the third to make any color in the rainbow!

RGB LED colors are set using a combination of red, green, and blue, in the form of an (RG, B) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set the LED to red, the tuple would be (255, 0, 0), which has the maximum level of red, and no green or blue. Green would be (0, 255, 0), etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255), with equal amounts of green and blue. If you increase all values to the same level, you get white!

Let's take a look at the code!

# Code Walkthrough

```"""Touch each pad to change red, green, and blue values on the LED"""
import time

import board
import touchio

led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
touch_A0 = touchio.TouchIn(board.A0)
touch_A1 = touchio.TouchIn(board.A1)
touch_A2 = touchio.TouchIn(board.A2)

r = g = b = 0

while True:
if touch_A0.value:
r = (r + 1) % 256
if touch_A1.value:
g = (g + 1) % 256
if touch_A2.value:
b = (b + 1) % 256

led = (r, g, b)
print((r, g, b))
time.sleep(0.01)
```

First we import our libraries: `time`, `touchio`, `adafruit_dotstar`, and `board`.

Next, we create the LED and touch objects.

DotStar LEDs use SPI, but the DotStar on the Gemma has its own unique pin assignments. So we assign `led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)` to tell it which pins to use. The `1` tells the code that we are using a single LED.

If you look at your board, you'll see three pads that have A0, A1 and A2 next to them. Those are the touch pads on your Gemma. We have three touch pads, so we create three touch objects. To create a touch object, you need to provide the pin you plan to use. We start with `touch_A0 = touchio.TouchIn(board.A0)` and then use the same concept for `touch_A1` and `touch_A2`.

```led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
touch_A0 = touchio.TouchIn(board.A0)
touch_A1 = touchio.TouchIn(board.A1)
touch_A2 = touchio.TouchIn(board.A2)```

Since we're going to be using r, g, b color values, we need to initialise the variables for later use. So we set `r = g = b = 0`.

We create our main loop with `while True:`.

Within the loop, we have three `if` statements. Each are the same concept, using a touch pad to change a color value.

We start with `if touch_A0.value:`, which says, "if this touch pad is touched, do the following:". We are going to use touch pad A0 to change the red color level. So, we have `r = (r + 1) % 256`. This says every time you touch the pad, increase the red value by 1 up to a maximum value of 255, and loop once the maximum is reached. This cycles from 0 to 255 and then back to 0. However, you don't need to touch the pad 256 times to get through the cycle! The touch pads respond if you touch and hold, so you can place your finger on A0 and leave it, and it will go through the entire cycle.

```    if touch_A0.value:
r = (r + 1) % 256```

Then we do the same for A1 and A2, where A1 affects the green value and A2 affects the blue value.

```    if touch_A1.value:
g = (g + 1) % 256
if touch_A2.value:
b = (b + 1) % 256```

Then, we set `led = (r, g, b)`. This set the LED to the color that matches the current values that you've set by touching the different pads! Note that we have 1 LED, however we've set `led`. This is because Python starts counting at 0. So our first LED is number 0 according to the code.

Then we print the color values to the serial console with `print((r, g, b))`. If you are connected to the serial console, you'll see the values change live as you touch the pads!

Last, we include a ` time.sleep(0.01)` to include a tiny delay. Otherwise the colors can change so quickly you'll pass right by the color you were trying to set! You can change this if you'd like to make the values change more quickly.

You don't have to change each value separately! You can touch any number of pads at the same time to affect multiple color values at the same time.

That's all there is to it! Simple code with a little math, and you've got a way to use the touch pads on your Gemma to show off any color you'd like!

This guide was first published on May 10, 2018. It was last updated on May 10, 2018. This page (Red light, green light, blue light!) was last updated on Sep 09, 2019.