新手求教?!RGB彩灯
本帖最后由 jasonqiu 于 2014-9-28 22:03 编辑新手求教,谢谢,代码如下:
在《Arduino 从基础到实践》 项目8-RGB彩灯
float RGB1;
float RGB2;
float INC;
int red, green, blue;
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
randomSeed(analogRead(0));
RGB1 = 0;
RGB1 = 0;
RGB1 = 0;
RGB2 = random(256);
RGB2 = random(256);
RGB2 = random(256);
}
void loop()
{
randomSeed(analogRead(0));
for (int x=0; x<3; x++) {
INC = (RGB1 - RGB2) / 256; }//这是什么意思?是要算占空比吗?是负数啊?!
for (int x=0; x<256; x++)
{
red = int(RGB1);
green = int(RGB1);
blue = int(RGB1);
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(100);
RGB1 -= INC;
RGB1 -= INC;
RGB1 -= INC;
}
for (int x=0; x<3; x++) {//这个循环是什么意思??看不懂
RGB2 = random(556)-300;
RGB2 = constrain(RGB2, 0, 255);
delay(1000);
}
}
问题:INC = (RGB1 - RGB2) / 256; 为什么要除以256啊?除了以后不就是在0和-1之间??是要算占空比吗? 隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
:
:
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
當中最好可以明白, 例子中用了 pin 9,10,11, 如果轉成 pin 7, 8, 9 又可以嗎?為什麼?
analogWrite 跟 digitalWrite 有什麼分別.
當然, 既然是 RGB 燈, 也要明白 RGB 燈的顏色是如何決定的.
還有一個小問題, 共陽跟共陰的 RGB, 在使用時及程式上又會有什麼分別?
嘗試自己解答, 之後就會明白 RGB 怎樣用了.
Super169 发表于 2014-9-28 23:35 static/image/common/back.gif
隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.
int RedPin = 11;
分析得很透彻,很深刻!这些才是真正的学习点!重点! Super169 发表于 2014-9-28 23:35 static/image/common/back.gif
隨機顯示的例子, 內裡的值如何變化, 不值得花時間去研究, 最重要明白幾句就夠了.
int RedPin = 11;
非常感谢,拓展了知识面。:handshake 朋友 能给我讲一下这个程序是怎么运行的吗?
页:
[1]