硬件:共阴极,RGB配置为输出模式,输入0-255的值,组合成不同效果
代码:
const int redPin = 6;
const int greenPin = 7;
const int bluePin = 8;
void setup() {
// put your setup code here, to run once:
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}
void color(unsigned char red, unsigned char green, unsigned char blue)
{
analogWrite(redPin,red);
analogWrite(greenPin,green);
analogWrite(bluePin,blue);
}
void loop() {
// put your main code here, to run repeatedly:
color(0,255,255);
delay(1000); //ms
color(0,128,0);
delay(1000);
color(128,128,128);
delay(1000);
}
效果:可以查找对应的RGB值嵌入在代码中,显示各种颜色