The original code was using analogWrite, but in order to use ESP32 the analogWrite was changed to ledcWrite, I don't know if this is the reason why my canvas slider only adjusts the red LEDs no matter how I move it and doesn't show the RGB.
Code:
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
const int Rpin = 3;
const int Gpin = 5;
const int Bpin = 6;
void setup() {
pinMode(Rpin, OUTPUT);
pinMode(Gpin, OUTPUT);
pinMode(Bpin, OUTPUT);
ledcSetup(0, 5000, 8); // 设置LED控制通道0,频率为5000 Hz,分辨率为8位
ledcAttachPin(Rpin, 0); // 将通道0绑定到Rpin
ledcSetup(1, 5000, 8); // 设置LED控制通道1,频率为5000 Hz,分辨率为8位
ledcAttachPin(Gpin, 1); // 将通道1绑定到Gpin
ledcSetup(2, 5000, 8); // 设置LED控制通道2,频率为5000 Hz,分辨率为8位
ledcAttachPin(Bpin, 2); // 将通道2绑定到Bpin
Serial.begin(9600);
SerialBT.begin("ESP32_BT"); // 设置蓝牙名称
}
void loop() {
byte cmmd[2] = {};
int insize;
int color;
if ((insize = SerialBT.available())) {
cmmd[0] = SerialBT.read();
cmmd[1] = SerialBT.read();
color = (cmmd[1] * 256) + cmmd[0];
Serial.println(color);
if (color >= 1000 && color < 1255) {
int red = color;
red = map(red, 1000, 1255, 0, 255);
ledcWrite(0, red); // 控制红色LED
delay(10);
}
if (color >= 2000 && color < 2255) {
int green = color;
green = map(green, 2000, 2255, 0, 255);
ledcWrite(1, green); // 控制绿色LED
delay(10);
}
if (color >= 3000 && color < 3255) {
int blue = color;
blue = map(blue, 3000, 3255, 0, 255);
ledcWrite(2, blue); // 控制蓝色LED
delay(10);
}
}
}
bt_color.aia (5.0 KB)