Anonymous Anonymous - 6 days ago
67 0

Added and deleted comments. Corrected the RED sequence to start as red.

C

RGB_LED 2

// Modified from the Elegoo code
//
// I replaced analogWrite with ledcWrite
// Everything else that is different is from here to the end of setup()
//
// ESP32 pin number is the # in GPIO_#
// Probaby best to avoid the pins with blue-colored names (SCL, SDA, etc.) for now.
//
// Arduino Setup:
// pinMode(pin, mode); // Mode: INPUT, OUTPUT, or INPUT_PULLUP
//
// Arduino Output:
// analogWrite(pin, duty); // Duty: between 0 (always off) and 255 (always on)
// 
// ESP32 Setup:
// ledcSetup(channel, frequency, resolution); // Resolution upto 16 bits, 8 should be fine
// ledcAttachPin(pin, channel); // Channel is any value between 0 - 15
//
// ESP32 Output:
// ledcWrite(channel, duty); // Duty: between 0 (always off) and 255 (always on)

#define delayTime 10 // fading time between colors

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

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

// Identifiers
int freq = 1250;
int resolution = 8;

void setup()
{
  // 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);
}

// RGB Values
int redValue;
int greenValue;
int blueValue;

// main loop
void loop()
{
  // RED sequence
  redValue = 255;
  greenValue = 0;
  blueValue = 0;

  for(int i = 0; i < 255; i += 1)
  {
    redValue -= 1;
    greenValue += 1;
    ledcWrite(RED, redValue);
    ledcWrite(GREEN, greenValue);
    delay(delayTime);
  }

  // GREEN sequence
  redValue = 0;
  greenValue = 255;
  blueValue = 0;

  for(int i = 0; i < 255; i += 1)
  {
    greenValue -= 1;
    blueValue += 1;
    ledcWrite(GREEN, greenValue);
    ledcWrite(BLUE, blueValue);
    delay(delayTime);
  }

  // BLUE sequence
  redValue = 0;
  greenValue = 0;
  blueValue = 255;

  for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
  {
    blueValue -= 1;
    redValue += 1;
    ledcWrite(BLUE, blueValue);
    ledcWrite(RED, redValue);
    delay(delayTime);
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download