Fading Arduino RGB LED from one color to the other?

后端 未结 7 1884
攒了一身酷
攒了一身酷 2021-02-03 12:15

I\'ve currently managed to get my LED to cycle through eight colors that I\'ve selected. Everything is working correctly, except that I want to go for a more natural feel, and w

7条回答
  •  你的背包
    2021-02-03 12:42

    I would like to contribute with a more user friendly answer as aids understanding of how it works.

    In my example bellow I'm using common anode RGB LED.


    In my project however: To set a Color to RGB LED, I send a String via HW Serial.

    Command Example: RGB000255000

    This Command as String is divided into 4 parts of 3 chars each. Using the Command Example Above:

    • "RGB" : To filter where the Command will be executed.
    • "000" : The 2nd 3 Chars represent Red Value.
    • "255" : The 3rd 3 Chars represent Green Value.
    • "000" : The 4th 3 Chars represent Blue Value.

    Result: This will Output Green on your LED.


    See Code Bellow:

    // Set your LED Pins.
    const int rPin = 9;
    const int gPin = 10;
    const int bPin = 11;
    
    // Set the variables that will assign a value to each Color Pin.
    int rVal = 255;
    int gVal = 255;
    int bVal = 255;
    
    // Fade Red Pin (In / Out).
    void FadeRed(int red)
    {
      // When Red Value on Red Pin is Inferior to the New Value: Fade In.
      if (rVal < red)
      {
        // Fade Out.
        for (int r = rVal; r <= red; r++)
        {
          // Set the Variable and Pin values.
          rVal = r;
          analogWrite(rPin, rVal);
    
          // Delay Slighlty (Synchronously). For Asynchronous Delay; you may try using "millis".
          delay(6);
        } 
      }
    
      // When Red Value on Red Pin is Superior to the New Value: Fade Out.
      else
      { 
        for (int r = rVal; r >= red; r--)
        {
          rVal = r;
          analogWrite(rPin, rVal);
          delay(6); 
        }
      }
    }
    
    // Fade Green Pin (In / Out).
    void FadeGreen(int green)
    {
      if (gVal < green)
      {
        for (int g = gVal; g <= green; g++)
        {
          gVal = g;
          analogWrite(gPin, gVal);
          delay(6);
        }
      }
      
      else
      { 
        for (int g = gVal; g >= green; g--)
        { 
          gVal = g;
          analogWrite(gPin, gVal);
          delay(6);
        }
      }
    }
    
    // Fade Blue Pin (In / Out).
    void FadeBlue(int blue)
    {
      if (bVal < blue)
      {
        for (int b = bVal; b <= blue; b++)
        {
          bVal = b;
          delay(6);
          analogWrite(bPin, b);
        }
      }
      
      else
      {
        for (int b = bVal; b >= blue; b--)
        {
          bVal = b;
          delay(6);
          analogWrite(bPin, b);
        }
      }
    }
    
    void FadeColor(int red, int green, int blue)
    {
        // Debug Only.
        Serial.println("\n[+] Received Values");
        Serial.println(red);
        Serial.println(green);
        Serial.println(blue);
    
        // Code.
        FadeRed(red);
        FadeGreen(green);
        FadeBlue(blue);
    
        // Debug Only.
        Serial.println("\n[+] Pin Values \n");
        Serial.println(rVal);
        Serial.println(gVal);
        Serial.println(bVal);
    }
    
    /* Set RGB LED Color According to String Value. (i.e: RGB000000000) */
    void SetColor(String color)
    {  
      // Retrieve the New Color from String.
      /* Split a String : Start Position; End Position */
      String red = color.substring(3, 6);   /* Get the 1st 3 Characters Corresponding to RED   */
      String green = color.substring(6, 9); /* Get the 2nd 3 Characters Corresponding to GREEN */
      String blue = color.substring(9, 12); /* Get the Last 3 Characters Corresponding to BLUE */
    
      int r = atoi(red.c_str());
      int g = atoi(green.c_str());
      int b = atoi(blue.c_str());
    
      int redVal = 255 - r;
      int grnVal = 255 - g;
      int bluVal = 255 - b;
      
      FadeColor(redVal, grnVal, bluVal);
    }
    
    
    void setup()
    {  
      pinMode(rPin, OUTPUT);
      pinMode(gPin, OUTPUT);
      pinMode(bPin, OUTPUT);
    
      pinMode(rPin, HIGH);
      pinMode(gPin, HIGH);
      pinMode(bPin, HIGH);
    
      analogWrite(rPin, rVal);
      analogWrite(gPin, gVal);
      analogWrite(bPin, bVal);
    }
    

提交回复
热议问题