int freq = 20; // 频率
int resolution = 8; // 分辨率8位也就是0-255
int channelr = 0;
int channelg = 1;
int channelb= 2;
int pinr = 12;//R接pin-12
int ping = 13;//G接pin-13
int pinb = 15;//B接pin-15
void setup() {
Serial.begin(115200);
ledcSetup(channelr, freq, resolution);
ledcAttachPin(pinr, channelr);
ledcSetup(channelg, freq, resolution);
ledcAttachPin(ping, channelg);
ledcSetup(channelb, freq, resolution);
ledcAttachPin(pinb, channelb);
}
// 通用函数,通过传递RGB值来设置颜色
void setColor(int r, int g, int b) {
ledcWrite(channelr, r);
ledcWrite(channelg, g);
ledcWrite(channelb, b);
}
void loop() {
setColor(255, 0, 0); // 红色初始化寻找蓝牙
delay(500);
setColor(0, 0, 255); // 蓝色已连接正在执行
delay(500);
setColor(0, 255, 0); // 绿色成功
delay(500);
}
注意引脚接对就行