Colors Arduino LED RGB控制并将十六进制转换为RGB
我有一个RGB LED,管脚连接到9、10、11和接地 我要做的是,控制这个LED,然后用串行监视器中选择的十六进制颜色点亮它Colors Arduino LED RGB控制并将十六进制转换为RGB,colors,arduino,hex,rgb,led,Colors,Arduino,Hex,Rgb,Led,我有一个RGB LED,管脚连接到9、10、11和接地 我要做的是,控制这个LED,然后用串行监视器中选择的十六进制颜色点亮它 int redPin = 9; // the pin that the red LED is attached to int greenPin = 10; // the pin that the green LED is attached to int bluePin = 11; // the pin that the blue LED i
int redPin = 9; // the pin that the red LED is attached to
int greenPin = 10; // the pin that the green LED is attached to
int bluePin = 11; // the pin that the blue LED is attached to
void setup(){
Serial.begin(115200);
pinMode(4,OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.println("Bonjour");
digitalWrite(4,HIGH);
}
void loop(){
while(Serial.available()){
Serial.begin(115200);
for(int i=0;i<5;i++){
hexstring[i] = Serial.read();
}
int number = (int) strtol( &hexstring, NULL, 16);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;
colorRGB(redInt,greenInt,blueInt);
}
void colorRGB(int red, int green, int blue){
analogWrite(redPin,constrain(red,0,255));
analogWrite(greenPin,constrain(green,0,255));
analogWrite(bluePin,constrain(blue,0,255));
}
我想通过拾取和使用十六进制颜色来尝试第一部分:
String hexstring = "#FFFFFF";
int number = (int) strtol( &hexstring[1], NULL, 16);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;
没有错误,但LED不亮!
谁能帮帮我?!求你了 编译代码时是否有错误?至少您的代码根本没有声明hexstring。什么错误?您缺少一个
}
<代码>while(Serial.available()){只检查串行缓冲区中是否至少有1个字节,但您仍在读取5个字节。int
只有2个字节。我看不到任何地方定义了hextstring
。您没有空值终止hextstring
。或者它是String
对象吗?strtol
甚至接受String
object?我的错,我只是在复制我执行的代码时没有犯一些错误,但是上面提到的所有注释都已经做了:我已经像这样声明了hexstring:char hexstring[10];问题是led只以一种颜色亮起(红、绿、蓝)不要改变。我认为问题在于从十六进制到RGB的转换!!
String hexstring = "#FFFFFF";
int number = (int) strtol( &hexstring[1], NULL, 16);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;