Skip to content

Instantly share code, notes, and snippets.

@zachflower
Last active February 19, 2024 09:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
}
@ryanisayori
Copy link

Thanks dude!

@CyanoFresh
Copy link

Maybe version without delay?

@wh1ter0se
Copy link

Thanks for this! Very convenient to modify because of its readability.

@gelezniyden
Copy link

Not complicated code and working great. Thanks.

@tonton81
Copy link

tonton81 commented Dec 7, 2020

Thanks, I use this code on my morimoto XBT ESP32 controller :)

@PseudoCoding
Copy link

@tonton any chance you could do a write up of how you got that working. I hate the XBT app and it actually hasn't worked for 8 months now. Saw this and was wondering if I could replicate what you did through a Raspberry Pi

@tonton81
Copy link

tonton81 commented Jan 22, 2021

i am still adding features to it
i actually added fastled support (not for pin driving leds, but for the awesome CRGB arrays fastled can manipulate then i feed the new sets to the XBT), I don't know if i should open source it or not, they dont use any authentication and unlike the phone app which requires phone next to the XBT to pair, my code can connect to any XBT in range, and it has the ability to detect new XBT controllers as well (connect to someone else's car) 😆

currently the core is on the ESP32, but i am writing an opensource library for teensy (teensy will control ESP32 over CANBUS) in order to write XBT mac addresses, change colors, etc i also made a video of sequential leds (3 ports on one XBT), something the phone app is pretty useless for. i wipl post on car videos in summer when i install em i got winter time to bench test my code, even TRS wants to see my summer videos of the XBT controller :)

heck if you wanted to add pots to the ESP32, you can change colors as well with knobs if your into physical buttons in your car, otherwise, like me, i integrate to the car's network and depending on the car's states, RPM, doors open, speed, park or drive, floor lights with doors, any event the car can give on CANBUS, you can have the code switch to whatever colors you want, totally custom. best part is when the XBT is first powered up, it turns on white normally. the ESP32 automatically detects the XBT in less than a second and turns the lights off! thats great if you have the controller on a switched power lead.

https://youtu.be/vuRp6eEMCzU

showing sequential ports at ~500ms for visual perception

also, right now the esp32 i coded up to 4 XBT controllers, if you are crazy enough to use 4 controllers with one ESP32, you can control 12 individual RGB channels of the XBT controllers in total

@PseudoCoding
Copy link

That's really cool! I cant believe they just leave it open to connect yet their app has so many issues connecting to it. Even my SwitchPro 9100 has at least a password locking it from any random device connecting to it. I'm going to try and connect to it when I have some time and see what I can do to it. More controllers would be cool, but I first want to get this one working as its pretty much useless to me right now. I reached out to Morimoto who said they have plans to update the app early this year but I'm not holding my breath. I also reached out to other companies with similar products to swap it but they needed more custom wiring than I would like to do since everything I have in my headlights is Morimoto. If I get in and make any progress I'd be more than willing to try to help where I can.

@tonton81
Copy link

tonton81 commented Jan 26, 2021

yeah their app "security" requires a high RSSI to make sure you have physical access to surface of the XBT, however, RSSI is not important, when I detect an XBT I can just connect to it's MAC address (which is static) and it connects in less than a second and runs for several days without issues

@PseudoCoding
Copy link

PseudoCoding commented Jan 26, 2021

Oh interesting! So that's why their app says to place the phone on the XBT. Since my phone has been unable to connect (all app permissions enabled) I'm assuming the XBT is having issues with RSSI and my phones (all android). What's the range look like for connecting from the ESP32? Also does the XBT just emit a bluetooth signal anytime it has power?

@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

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