YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4842|回复: 1
打印 上一主题 下一主题

玩转 Arduino Esplora ---- RGB LED调光(滑动电位器、摇杆)

[复制链接]

签到天数: 22 天

[LV.4]偶尔看看III

跳转到指定楼层
楼主
发表于 2013-12-11 19:28:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 原始人 于 2013-12-8 21:24 编辑

RGB调光(滑动电位器、摇杆)

Arduino Esplora上集成了一个滑动电位器元件(Linear Potentiometer),我们可以利用它来制作一个调光的实验,我们就是用Esplora 板上的RGB LED灯,读取滑动电位器语法:Esplora.readSlider();


实验例程1(使用电位器控制一个颜色灯的亮度):
  1. // include the Esplora library
  2. #include <Esplora.h>

  3. void setup() {
  4.   // nothing to setup
  5. }

  6. void loop() {
  7.   // read the sensor into a variable
  8.   int slider = Esplora.readSlider();

  9.   // convert the sensor readings to light levels
  10.   byte bright  = slider/4;

  11.   // write the light levels to the Red LED
  12.   Esplora.writeRed(bright);

  13.   // add a small delay to keep the LED from flickering:
  14.   delay(10);
  15. }
复制代码

滑动电位器使用起来比起旋钮电位器更加有感觉,使用它还可以实现其他互动功能!!










Arduino Esplora上还集成了一个X、Y、 360度摇杆,相当于2个电位器,我们可以使用它来调节另外两个颜色的LED,这样就可以实现控制RGB 3色调光了~   读取X、Y值语法:


Esplora.readJoystickX();
Esplora.readJoystickY();



实验例程2(摇杆、滑块调节RGB LED 3色亮度):
  1. /*
  2.   Esplora LED Show

  3.   Makes the RGB LED bright and glow as the joystick or the
  4.   slider are moved.
  5.   
  6.   Created on 22 november 2012
  7.   By Enrico Gueli <enrico.gueli@gmail.com>
  8.   Modified 22 Dec 2012
  9.   by Tom Igoe
  10. */
  11. #include <Esplora.h>

  12. void setup() {
  13.   // initialize the serial communication:
  14.   Serial.begin(9600);
  15. }

  16. void loop() {
  17.   // read the sensors into variables:
  18.   int xAxis = Esplora.readJoystickX();
  19.   int yAxis = Esplora.readJoystickY();
  20.   int slider = Esplora.readSlider();
  21.   
  22.   // convert the sensor readings to light levels:
  23.   byte red   = map(xAxis, -512, 512, 0, 255);
  24.   byte green = map(yAxis, -512, 512, 0, 255);
  25.   byte blue  = slider/4;

  26.   // print the light levels:
  27.   Serial.print(red);
  28.   Serial.print(' ');
  29.   Serial.print(green);
  30.   Serial.print(' ');
  31.   Serial.println(blue);

  32.   // write the light levels to the LED.
  33.   Esplora.writeRGB(red, green, blue);

  34.   // add a delay to keep the LED from flickering:  
  35.   delay(10);
  36. }
复制代码

将上面的例程烧写到控制板中,调节滑块和摇杆,可以调和出成千上万中颜色,RGB LED用白纸遮挡住再看颜色效果更好!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对

签到天数: 4 天

[LV.2]偶尔看看I

沙发
发表于 2019-6-3 23:39:48 | 只看该作者
这个算法很值得一学
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-5-17 16:27 , Processed in 0.044057 second(s), 23 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表