# RGB LED Button

## Goals

{% hint style="success" %}

* You know how to get a handle on the RGB LED button connected to your computer
* You can set the color of the LED, turn it on and off, and make it blink, and use the functionality of the button itself.
  {% endhint %}

## Get the LED

We can make our application more interactive and get input from a user with the [RGB LED Button](https://www.tinkerforge.com/en/doc/Hardware/Bricklets/RGB_LED_Button.html). In this article, we assume you have successfully initialized the devices with the [Tinkerforge Device Manager](https://iot.datalit.de/2-internet-of-things/iot-in-our-apps/connect-to-devices/the-tinkerforge-device-manager), and you stored all connected devices on a global variable `devices`. We also declared a global variable `button`.‌

Next, we need to know the device identifier of the RGB LED Button, which is **282**:

```
// Get a reference to the button and store in on the global variablebutton = devices.getDeviceByIdentifier(282);
```

Now that we have a reference to the button on the variable `button`, we can use that variable to call the button's LED functions. These are the same as for the [RGB LED](https://app.gitbook.com/@winf-hsos/s/home/~/drafts/-MVLyIOY_-DoC3YPzYDy/programming-with-internet-of-things/program-devices/rgb-led).‌

## Set the color of the button's LED <a href="#set-the-color-of-the-buttons-led" id="set-the-color-of-the-buttons-led"></a>

‌With `setColor()` we can turn the button's LED on and specify the color of the light. We define the color using the RGB notation:

```
// Set the button's LED to redbutton.setColor(255, 0, 0);​// Set the button's LED to greenbutton.setcolor(0, 255, 0);
```

## Turning the button's LED off <a href="#turning-the-buttons-led-off" id="turning-the-buttons-led-off"></a>

‌We can turn the button's LED off (black) at any time with the shortcut function `off()`:

```
// Turn the LED offbutton.off();
```

## Let the button's LED blink in a color <a href="#let-the-buttons-led-blink-in-a-color" id="let-the-buttons-led-blink-in-a-color"></a>

Similar to setting the color, we can use `blink()` to make the button's LED blink at a given frequency:

```
// Make the button's LED blink in green at a 500 ms intervalbutton.blink(0, 255, 0, 500);
```

## Listen to button presses <a href="#listen-to-button-presses" id="listen-to-button-presses"></a>

The more important functionality of the button is to react to the event that someone pressed the button. We can achieve this using the listener button and a callback function, as we do with sensors:

```
// Register a callbacl function for button pressesbutton.registerListener(buttonChanged);
```

‌Within the callback function, we can distinguish the two events "PRESSED" or "RELEASED":

```
function buttonChanged(val) {    // Get the state of the button    var state = val.getValue();        if(state == "PRESSED") {        // Do something    }    else if (state == "RELEASED") {        // Do something else    }}
```
