Arduino RGB LED随机PWM级别
Arduino RGB LED random PWM Level
我正在尝试创建一个程序,该程序将随机选择从给定数组LED的RGB值。它与第一种颜色蓝色一起工作正常。然后,我以第二种颜色筑巢,绿色,我从显示中放开蓝色,只有绿色显示。
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"挂在(!)中的绿色中,用于蓝色。基于以下事实:循环运行无限,您只需循环循环以进行循环。
第二个问题(也许不是问题,但是您看不到蓝色的原因)是您对bluevariable的初始化为0。如果第一次运行,则将值0写入PWM PIN。之后,您更改了变量,但请勿写入PWM PIN,因为您陷入了"无限的绿色环"。
btw,就像迈克尔(Michael)的评论中所说的那样,您应该将255更改为8,并且在您的数组中您应该将最后一个值(256)更改为255>
示例:
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);
}
}
相关文章:
- 被解释为低级别const的const对象的地址
- 在Windows中查找扬声器输出的当前音量级别
- FFMPEG配置文件级别id大小无效
- 固有构造函数的字符和访问级别
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- ascii 和 unicode 在处理级别有什么区别吗?
- fastrtps:如何在发布/订阅级别使用 DDS 历史记录?
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- C++std::atomic在程序员级别保证了什么
- Cmake:为 C 设置警告级别,并为 MSVC 单独设置C++代码(Visual C++)
- 覆盖应用程序的低级别键盘挂钩问题
- 二叉树级别顺序遍历在leetcode中
- 如何循环 n 次,在 xml 文件中每个循环一个级别
- 警告级别为 3 的 int 的 std::vector push_back 处的编译器警告
- 如何查看在程序级别为我的程序创建了多少 vtable 和 vpointer
- 下步指针键入用于shared_ptr的级别
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- 返回二叉搜索树特定级别的节点数的函数
- Arduino RGB LED随机PWM级别