Arduino LED RGB控制和转换十六进制到RGB

时间:2017-06-05 03:14:46

标签: colors arduino hex rgb led

我有一个RGB LED,其引脚分别为9,10,11和一个接地引脚。

我必须做的是控制此LED并使用串行监视器中选择的HEXADECIMAL颜色点亮它。

    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));

        }

这一行中的第一个错误:

int number = (int) strtol( &hexstring, NULL, 16)

我想通过拾取和HEX颜色尝试第一部分:

 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不亮! 谁能帮帮我?请!!

0 个答案:

没有答案
相关问题