Arduino和RGB LED [英] Arduino and RGB LEDs
本文介绍了Arduino和RGB LED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个数组:
int rgbcolors = {{R1,G1,B1},{R2,G2,B2},{R3,G3,B3},{Rn,Gn,Bn}}
如何通过按遥控器上的 1按钮来遍历此数组的值? 所以按一次,给出第一个{R1,G1,B1},按两次,给出{R2,G2,B2}等等...
How do I cycle through the values of this array by pressing 1 button on the remote? So pressing once, gives the first {R1,G1,B1}, pressing twice, gives {R2,G2,B2} etc...
我当前对预定义颜色的空循环:
My current void loop for predefined colors:
if (irrecv.decode(&results))
{
int i = 0;
int j = 0;
int k = 0;
unsigned int val = results.value;
irrecv.resume();
switch(val) {
case button1: //RED
analogWrite(RedPin,255);
analogWrite(GreenPin,0);
analogWrite(BluePin,0);
break;
case button2: //GREEN
analogWrite(RedPin,0);
analogWrite(GreenPin,255);
analogWrite(BluePin,0);
break;
case button3: //BLUE
analogWrite(RedPin,0);
analogWrite(GreenPin,0);
analogWrite(BluePin,255);
break;
case button4: //YEL0
analogWrite(RedPin,255);
analogWrite(GreenPin,255);
analogWrite(BluePin,0);
break;
case button5: //PURPLE
analogWrite(RedPin,255);
analogWrite(GreenPin,0);
analogWrite(BluePin,255);
break;
case button6: //TURQUOISE
analogWrite(RedPin,0);
analogWrite(GreenPin,255);
analogWrite(BluePin,255);
break;
case button7: //ORANGE
analogWrite(RedPin,255);
analogWrite(GreenPin,140);
analogWrite(BluePin,0);
break;
case button8: //LIME !!! DON'T USE // CRASHES
analogWrite(RedPin,133);
analogWrite(GreenPin,255);
analogWrite(BluePin,140);
break;
case button9: //PINK
analogWrite(RedPin,50);
analogWrite(GreenPin,50);
analogWrite(BluePin,50);
break;
case buttonOn:
analogWrite(RedPin,255);
analogWrite(GreenPin,255);
analogWrite(BluePin,255);
break;
case buttonOff:
analogWrite(RedPin,0);
analogWrite(GreenPin,0);
analogWrite(BluePin,0);
break;
}
}
推荐答案
很遗憾,我无法帮助您解决"IR接收器值突然改变"的问题,但是我认为我可以为您通过遍历此数组的值提供帮助"按下遥控器"问题上的1按钮.以下是一个代码样本,可以完成我认为您想做的事情:
I unfortunately cannot help you with your "IR receiver value suddenly changed" issue, but I think I can help with your "cycle through the values of this array by pressing 1 button on the remote" question. The following is a code sample that does what I think you want to do:
const int NumberOfModes = 4;
int CurrentMode = -1;
void loop() {
int rgbcolors[NumberOfModes][3] = {
{ 255,0,0 }, // RED
{ 0,255,0 }, // GREEN
{ 0,0,255 }, // BLUE
{ 255,255,0 } // YELLOW
};
if (irrecv.decode(&results))
{
unsigned int val = results.value;
irrecv.resume();
if (val == button1) {
// Switch to new mode
CurrentMode++;
if (CurrentMode >= NumberOfModes)
{
CurrentMode = 0;
}
// Set output values
analogWrite(RedPin, rgbcolors[CurrentMode][0]);
analogWrite(GreenPin, rgbcolors[CurrentMode][1]);
analogWrite(BluePin, rgbcolors[CurrentMode][2]);
}
}
}
这篇关于Arduino和RGB LED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文