/*
 * @Author: dugulingping
 * @Date: 2022-04-21 18:35:09
 * @LastEditTime: 2022-04-22 14:57:29
 * @LastEditors: dugulingping
 * @Description: colorLED
 * @FilePath: \test\colorLED\colorLED.ino
 */
#define RED 5
#define GREEN 4
#define BLUE 14

void RGBLed(unsigned char red, unsigned char green, unsigned char blue, bool type);
void setup(){
  pinMode(RED,OUTPUT);
  pinMode(GREEN,OUTPUT);
  pinMode(BLUE,OUTPUT);

  digitalWrite(RED,1);
  digitalWrite(GREEN,1);
  digitalWrite(BLUE,1);

}
void loop(){
    int rgbColour[3];
    rgbColour[0] = 255;
    rgbColour[1] = 0;
    rgbColour[2] = 0; 
    for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
        for(int i = 0; i < 255; i += 1) {
            rgbColour[decColour] -= 1;
            rgbColour[incColour] += 1;
            RGBLed(rgbColour[0], rgbColour[1], rgbColour[2],true);
            delay(10);
        }
    }
}

void RGBLed(unsigned char red, unsigned char green, unsigned char blue, bool type = false){
    //处理共阳RGB LED
    if(type){
        red = 255 - red;
        green = 255 - green;
        blue = 255 - blue;
    }
    analogWrite(RED, red);
    analogWrite(GREEN, green);
    analogWrite(BLUE, blue);
}