极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10882|回复: 4

新手求教?!RGB彩灯

[复制链接]
发表于 2014-9-28 22:01:55 | 显示全部楼层 |阅读模式
本帖最后由 jasonqiu 于 2014-9-28 22:03 编辑

新手求教,谢谢,代码如下:
在《Arduino 从基础到实践》 项目8-RGB彩灯
float RGB1[3];
float RGB2[3];
float INC[3];
int red, green, blue;
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
randomSeed(analogRead(0));
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;
RGB2[0] = random(256);
RGB2[1] = random(256);
RGB2[2] = random(256);
}
void loop()
{
randomSeed(analogRead(0));
for (int x=0; x<3; x++) {
INC[x] = (RGB1[x] - RGB2[x]) / 256; }//这是什么意思?是要算占空比吗?是负数啊?!
for (int x=0; x<256; x++)
{
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(100);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}
for (int x=0; x<3; x++) {//这个循环是什么意思??看不懂
RGB2[x] = random(556)-300;
RGB2[x] = constrain(RGB2[x], 0, 255);
delay(1000);
}
}


问题:INC[x] = (RGB1[x] - RGB2[x]) / 256; 为什么要除以256啊?除了以后不就是在0和-1之间??是要算占空比吗?
回复

使用道具 举报

发表于 2014-9-28 23:35:22 | 显示全部楼层
隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.

  1. int RedPin = 11;
  2. int GreenPin = 10;
  3. int BluePin = 9;
  4. :
  5. :
  6. analogWrite (RedPin, red);
  7. analogWrite (GreenPin, green);
  8. analogWrite (BluePin, blue);
复制代码


當中最好可以明白, 例子中用了 pin 9,10,11, 如果轉成 pin 7, 8, 9 又可以嗎?  為什麼?
analogWrite 跟 digitalWrite 有什麼分別.
當然, 既然是 RGB 燈, 也要明白 RGB 燈的顏色是如何決定的.
還有一個小問題, 共陽跟共陰的 RGB, 在使用時及程式上又會有什麼分別?

嘗試自己解答, 之後就會明白 RGB 怎樣用了.
回复 支持 反对

使用道具 举报

发表于 2014-9-29 11:57:53 | 显示全部楼层
Super169 发表于 2014-9-28 23:35
隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.

int RedPin = 11;

分析得很透彻,很深刻!这些才是真正的学习点!重点!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-30 21:42:08 | 显示全部楼层
Super169 发表于 2014-9-28 23:35
隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.

int RedPin = 11;

非常感谢,拓展了知识面。
回复 支持 反对

使用道具 举报

发表于 2014-11-30 16:15:28 | 显示全部楼层
朋友   能给我讲一下这个程序是怎么运行的吗?
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 17:17 , Processed in 0.037101 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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