Arduino RGB LED 随机 PWM 电平
Arduino RGB LED random PWM Level
我正在尝试创建一个程序,该程序将从给定阵列中随机选择 RGB LED 的 PWM 值。它适用于第一种颜色,蓝色。然后我嵌套在第二种颜色绿色中,我从显示中去掉蓝色,只显示绿色。
void loop() {
// put your main code here, to run repeatedly:
int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 256}; //setup Array X for brightness options
int blueVariable = 0; //Blue LED
int greenVariable = 0; //Green LED
for (int blueLed = 0; blueLed > -1; ) { //for loop to choose PWM option
analogWrite(11, x[blueVariable]); //Initilize the PWM function on pin 11 to brightness of blueVariable
// if (blueLed == 255) blueLed = 0; //
blueVariable = random(0,8); //Random function to decide on blueVariable value
delay(500);
for (int greenLed = 0; greenLed > -1; ) {
analogWrite(10, x[greenVariable]);
// if (g == 255) g = 0; // switch direction at peak
greenVariable = random(0,255);
delay(500);
}
}
}
你有两个问题:
首先,您将 "for loop" 的绿色挂钩在(!)蓝色的 for 循环中。基于循环 运行 宁无限的事实,你只循环第二个 for 循环。
第二个问题(也许不是问题,但是您看不到蓝色的原因)是您将 blueVariable 初始化为 0。
如果你第一次 运行,你将值 0 写入 PWM 引脚。之后你更改变量,但不要写入 PWM 引脚,因为你会卡在你的 "infinite green Loop".
顺便说一句,就像 Michael 在评论中所说的那样,您应该将 255 更改为 8,并且在您的数组中您应该将最后一个值 (256) 更改为 255,因为 8 位 PWM 意味着 0-255 之间的 256 个值。
示例:
int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 255}; // Changed Value
void loop() {
int blueVariable = 0; //Blue LED
int greenVariable = 0; //Green LED
while(1) { // Because it was infinite already i changed it to while(1)
blueVariable = random(0,8); //Put in front of analogWrite()
analogWrite(11, x[blueVariable]);
delay(500);
// Deleted the scond loop
greenVariable = random(0,8); // Value changed from 255 to 8; Also put in front of analogWrite
analogWrite(10, x[greenVariable]);
delay(500);
}
}
我正在尝试创建一个程序,该程序将从给定阵列中随机选择 RGB LED 的 PWM 值。它适用于第一种颜色,蓝色。然后我嵌套在第二种颜色绿色中,我从显示中去掉蓝色,只显示绿色。
void loop() {
// put your main code here, to run repeatedly:
int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 256}; //setup Array X for brightness options
int blueVariable = 0; //Blue LED
int greenVariable = 0; //Green LED
for (int blueLed = 0; blueLed > -1; ) { //for loop to choose PWM option
analogWrite(11, x[blueVariable]); //Initilize the PWM function on pin 11 to brightness of blueVariable
// if (blueLed == 255) blueLed = 0; //
blueVariable = random(0,8); //Random function to decide on blueVariable value
delay(500);
for (int greenLed = 0; greenLed > -1; ) {
analogWrite(10, x[greenVariable]);
// if (g == 255) g = 0; // switch direction at peak
greenVariable = random(0,255);
delay(500);
}
}
}
你有两个问题:
首先,您将 "for loop" 的绿色挂钩在(!)蓝色的 for 循环中。基于循环 运行 宁无限的事实,你只循环第二个 for 循环。
第二个问题(也许不是问题,但是您看不到蓝色的原因)是您将 blueVariable 初始化为 0。 如果你第一次 运行,你将值 0 写入 PWM 引脚。之后你更改变量,但不要写入 PWM 引脚,因为你会卡在你的 "infinite green Loop".
顺便说一句,就像 Michael 在评论中所说的那样,您应该将 255 更改为 8,并且在您的数组中您应该将最后一个值 (256) 更改为 255,因为 8 位 PWM 意味着 0-255 之间的 256 个值。
示例:
int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 255}; // Changed Value
void loop() {
int blueVariable = 0; //Blue LED
int greenVariable = 0; //Green LED
while(1) { // Because it was infinite already i changed it to while(1)
blueVariable = random(0,8); //Put in front of analogWrite()
analogWrite(11, x[blueVariable]);
delay(500);
// Deleted the scond loop
greenVariable = random(0,8); // Value changed from 255 to 8; Also put in front of analogWrite
analogWrite(10, x[greenVariable]);
delay(500);
}
}