YFROBOT创客社区
标题: 玩转 Arduino Esplora ---- RGB LED调光(滑动电位器、摇杆) [打印本页]
作者: 原始人 时间: 2013-12-11 19:28
标题: 玩转 Arduino Esplora ---- RGB LED调光(滑动电位器、摇杆)
本帖最后由 原始人 于 2013-12-8 21:24 编辑
RGB调光(滑动电位器、摇杆)
Arduino Esplora上集成了一个滑动电位器元件(Linear Potentiometer),我们可以利用它来制作一个调光的实验,我们就是用Esplora 板上的RGB LED灯,读取滑动电位器语法:Esplora.readSlider();
实验例程1(使用电位器控制一个颜色灯的亮度):- // include the Esplora library
- #include <Esplora.h>
- void setup() {
- // nothing to setup
- }
- void loop() {
- // read the sensor into a variable
- int slider = Esplora.readSlider();
- // convert the sensor readings to light levels
- byte bright = slider/4;
- // write the light levels to the Red LED
- Esplora.writeRed(bright);
- // add a small delay to keep the LED from flickering:
- delay(10);
- }
复制代码滑动电位器使用起来比起旋钮电位器更加有感觉,使用它还可以实现其他互动功能!!
Arduino Esplora上还集成了一个X、Y、 360度摇杆,相当于2个电位器,我们可以使用它来调节另外两个颜色的LED,这样就可以实现控制RGB 3色调光了~ 读取X、Y值语法:
Esplora.readJoystickX();
Esplora.readJoystickY();
[attach]679[/attach]
实验例程2(摇杆、滑块调节RGB LED 3色亮度):- /*
- Esplora LED Show
- Makes the RGB LED bright and glow as the joystick or the
- slider are moved.
-
- Created on 22 november 2012
- By Enrico Gueli <enrico.gueli@gmail.com>
- Modified 22 Dec 2012
- by Tom Igoe
- */
- #include <Esplora.h>
- void setup() {
- // initialize the serial communication:
- Serial.begin(9600);
- }
- void loop() {
- // read the sensors into variables:
- int xAxis = Esplora.readJoystickX();
- int yAxis = Esplora.readJoystickY();
- int slider = Esplora.readSlider();
-
- // convert the sensor readings to light levels:
- byte red = map(xAxis, -512, 512, 0, 255);
- byte green = map(yAxis, -512, 512, 0, 255);
- byte blue = slider/4;
-
- // print the light levels:
- Serial.print(red);
- Serial.print(' ');
- Serial.print(green);
- Serial.print(' ');
- Serial.println(blue);
- // write the light levels to the LED.
- Esplora.writeRGB(red, green, blue);
- // add a delay to keep the LED from flickering:
- delay(10);
- }
复制代码将上面的例程烧写到控制板中,调节滑块和摇杆,可以调和出成千上万中颜色,RGB LED用白纸遮挡住再看颜色效果更好!
作者: azi1974 时间: 2019-6-3 23:39
这个算法很值得一学
欢迎光临 YFROBOT创客社区 (http://yfrobot.com.cn/) |
Powered by Discuz! X3.1 |