EsploraLedShow
Use the Joystick and slider to create a light show with the LED.
Led Show
This example shows you how to read the values from the joystick. The output will be displayed through the serial monitor and as a color on the RGB LED.
The joystick has two axes, X and Y. Each axis controls a color of the RGB LED: red with the X-axis, and green with the Y-axis. The brightness of the blue element will be controlled by the position of the linear potentiometer.
Hardware Required
- Arduino Esplora
Circuit
Only your Arduino Esplora is needed for this example.
Code
The RGB LED is comprised of three colors that represent the three primary colors: red, green, and blue. Each of these light's brightness is individually controllable with functions in the Esplora library:
If you want to control all the colors with one instruction you can use the writeRGB() function.
Moving the joystick and changing the position of the linear potentiometer will generate different values. These values are used to produce two different outputs: one will appear by opening the serial monitor, and the other is displayed physically through the RGB led.
1/*2
3 Esplora LED Show4
5 Makes the RGB LED bright and glow as the joystick or the6
7 slider are moved.8
9 Created on 22 november 201210
11 By Enrico Gueli <enrico.gueli@gmail.com>12
13 Modified 22 Dec 201214
15 by Tom Igoe16
17*/18#include <Esplora.h>19
20void setup() {21
22 // initialize the serial communication:23
24 Serial.begin(9600);25}26
27void loop() {28
29 // read the sensors into variables:30
31 int xAxis = Esplora.readJoystickX();32
33 int yAxis = Esplora.readJoystickY();34
35 int slider = Esplora.readSlider();36
37 // convert the sensor readings to light levels:38
39 byte red = map(xAxis, -512, 512, 0, 255);40
41 byte green = map(yAxis, -512, 512, 0, 255);42
43 byte blue = slider / 4;44
45 // print the light levels:46
47 Serial.print(red);48
49 Serial.print(' ');50
51 Serial.print(green);52
53 Serial.print(' ');54
55 Serial.println(blue);56
57 // write the light levels to the LED.58
59 Esplora.writeRGB(red, green, blue);60
61 // add a delay to keep the LED from flickering:62
63 delay(10);64}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.