Skip to content

Instantly share code, notes, and snippets.

@zachflower
Last active July 8, 2025 14:35
Show Gist options
  • Save zachflower/79df9ed5ca398264d3b6 to your computer and use it in GitHub Desktop.
Save zachflower/79df9ed5ca398264d3b6 to your computer and use it in GitHub Desktop.
Smooth RGB LED Color Transitions (Arduino)
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int r = 0;
int g = 0;
int b = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
setColor(255, 0, 0); // red
setColor(0, 255, 0); // green
setColor(0, 0, 255); // blue
setColor(255, 255, 0); // yellow
setColor(80, 0, 80); // purple
setColor(0, 255, 255); // aqua
}
void setColor(int red, int green, int blue) {
while ( r != red || g != green || b != blue ) {
if ( r < red ) r += 1;
if ( r > red ) r -= 1;
if ( g < green ) g += 1;
if ( g > green ) g -= 1;
if ( b < blue ) b += 1;
if ( b > blue ) b -= 1;
_setColor();
delay(10);
}
}
void _setColor() {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
@tonton81
Copy link

tonton81 commented Jan 27, 2021

it broadcasts itself if there are no connections to it. example, if i unplug the esp32, the phone will "eventually" pick up the XBT, i say eventually because it may take up to 15 seconds with the app, the esp32 connects in less than a second hahaha

the XBT range should be normal bluetooth range, considering people install the XBT in their engine bays amd use a phone app, the ESP32 should be fine

@giantpreston
Copy link

int redPin = 9;
int greenPin = 10;
int bluePin = 11;

int r = 0;
int g = 0;
int b = 0;

#define BRIGHTNESS 0.3  // Set brightness (0.0 = off, 1.0 = full brightness)

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() { 
  setColor(255, 0, 0);    // red
  setColor(0, 255, 0);    // green
  setColor(0, 0, 255);    // blue
  setColor(255, 255, 0);  // yellow
  setColor(80, 0, 80);    // purple
  setColor(0, 255, 255);  // aqua
}

void setColor(int red, int green, int blue) {
  while (r != red || g != green || b != blue) {
    if (r < red) r += 1;
    if (r > red) r -= 1;

    if (g < green) g += 1;
    if (g > green) g -= 1;

    if (b < blue) b += 1;
    if (b > blue) b -= 1;

    _setColor();
    delay(10);
  }
}

void _setColor() {
  analogWrite(redPin, r * BRIGHTNESS);
  analogWrite(greenPin, g * BRIGHTNESS);
  analogWrite(bluePin, b * BRIGHTNESS); 
}

Made this with reduceable brightness, my led was too bright to stare at and i figured some people might have the same problem. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment