【发布时间】:2017-02-22 07:29:57
【问题描述】:
我正在尝试创建一个程序,该程序将从给定的阵列中随机选择 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);
}
}
}
【问题讨论】:
-
正确格式化您的代码将对每个人(包括您自己)都有很大帮助。
-
为什么
greenVariable = random(0,255),你只有9个亮度值。此外,您的循环是无限的,没有退出条件。 -
我也看不到 PWM 这里可能是
analogWrite函数的用途,但没有上下文我们不知道它的作用。操作数是什么?您的 LED 是如何连接到 MCU 的?你买的是哪个MCU?你用的是什么 PWM(软件、定时器/计数器、PWMA 模块)? Arduino 不是魔术词,它只是一个框架,所以你看不到/不明白你在做什么......这段代码放在哪里/调用(主线程,ISR,......)? -
迈克尔,你的权利来自以前的编码,应该已经改变了。
标签: c++ arduino embedded rgb led