Anonymous Anonymous - 2 months ago
353 0

No description

C

RGB_LED

// Modified from the Elegoo code
// Everything different is from the here to the end of setup()
// Edit -> Find, replace analogWrite with ledcWrite

// Define Pins
#define BLUE_PIN 15
#define GREEN_PIN 32
#define RED_PIN 14

// Define Channels
#define RED 1
#define GREEN 2
#define BLUE 3

void setup()
{
  int freq = 5000;
  int resolution = 8;

  // Channel, Frequency, Resolution
  ledcSetup(RED, freq, resolution);
  ledcSetup(GREEN, freq, resolution);
  ledcSetup(BLUE, freq, resolution);

  // Pin, Channel
  ledcAttachPin(RED_PIN, RED);
  ledcAttachPin(GREEN_PIN, GREEN);
  ledcAttachPin(BLUE_PIN, BLUE);
}

// define variables
int redValue;
int greenValue;
int blueValue;

// main loop
void loop()
{
#define delayTime 10 // fading time between colors

  redValue = 0; // choose a value between 1 and 255 to change the color.
  greenValue = 0;
  blueValue = 0;

  // this is unnecessary as we've either turned on RED in SETUP
  // or in the previous loop ... regardless, this turns RED off
  // ledcWrite(RED, 0);
  // delay(1000);

  for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
  {
    redValue -= 1;
    greenValue += 1;
    // The following was reversed, counting in the wrong directions
    // ledcWrite(RED, 255 - redValue);
    // ledcWrite(GREEN, 255 - greenValue);
    ledcWrite(RED, redValue);
    ledcWrite(GREEN, greenValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 255;
  blueValue = 0;

  for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
  {
    greenValue -= 1;
    blueValue += 1;
    // The following was reversed, counting in the wrong directions
    // ledcWrite(GREEN, 255 - greenValue);
    // ledcWrite(BLUE, 255 - blueValue);
    ledcWrite(GREEN, greenValue);
    ledcWrite(BLUE, blueValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 0;
  blueValue = 255;

  for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
  {
    // The following code has been rearranged to match the other two similar sections
    blueValue -= 1;
    redValue += 1;
    // The following was reversed, counting in the wrong directions
    // ledcWrite(BLUE, 255 - blueValue);
    // ledcWrite(RED, 255 - redValue);
    ledcWrite(BLUE, blueValue);
    ledcWrite(RED, redValue);
    delay(delayTime);
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download