RGB LED Button
Goals
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.
Get the LED
We can make our application more interactive and get input from a user with the RGB LED Button. In this article, we assume you have successfully initialized the devices with 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.
Set the color of the button's LED
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
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
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
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 }}
Last updated
Was this helpful?