Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The Arduino code running 4 color modes on an Arduino UNO for some WS2812B LED strip lights using the FastLED library
#include <FastLED.h>
#include <LowPower.h>
#include <EEPROM.h>
//FastLED setup
#define NUM_LEDS 4
#define PIN 3 //Data pin for LED strip
CRGB leds[NUM_LEDS];
//Twinkle setup
#define BASE_COLOR CRGB(2,2,2) //Base background color
#define PEAK_COLOR CRGB(255,255,255) //Peak color to twinkle up to
// Amount to increment the color by each loop as it gets brighter:
#define DELTA_COLOR_UP CRGB(4,4,4)
// Amount to decrement the color by each loop as it gets dimmer:
#define DELTA_COLOR_DOWN CRGB(4,4,4)
// Chance of each pixel starting to brighten up.
// 1 or 2 = a few brightening pixels at a time.
// 10 = lots of pixels brightening at a time.
#define CHANCE_OF_TWINKLE 2
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
uint8_t PixelState[NUM_LEDS];
byte runMode;
byte globalBright = 150;
byte globalDelay = 20; //Delay speed for twinkling
byte address = 35; //Address to store the run mode
void setup()
{
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_LEDS);
FastLED.setCorrection(TypicalLEDStrip);
//FastLED.setMaxPowerInVoltsAndMilliamps(5,maxMilliamps);
FastLED.setBrightness(globalBright);
//Get the mode to run
runMode = EEPROM.read(address);
//Increment the runmode by 1
EEPROM.write(address, runMode + 1);
}
void loop()
{
switch (runMode)
{
//Solid white
case 1: fill_solid(leds, NUM_LEDS, CRGB::White);
FastLED.show();
DelayForever();
break;
//Twinkle kinda slowly
case 2: FastLED.setBrightness(255);
globalDelay = 10;
TwinkleMapPixels();
break;
//Twinkle quickly
case 3: FastLED.setBrightness(150);
globalDelay = 2;
TwinkleMapPixels();
break;
//Rainbow
case 4:
RunRainbow();
break;
//Index out of range, reset it to 2 and then run mode 1.
//When the arduino restarts it will run mode 2, but for now run mode 1
default:
EEPROM.write(address, 2);
runMode = 1;
break;
}
}
void RunRainbow()
{
byte *c;
uint16_t i, j;
while (true)
{
for (j = 0; j < 256; j++) { // 1 cycle of all colors on wheel
for (i = 0; i < NUM_LEDS; i++) {
c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
setPixel(i, *c, *(c + 1), *(c + 2));
}
FastLED.show();
delay(globalDelay);
}
}
}
byte * Wheel(byte WheelPos) {
static byte c[3];
if (WheelPos < 85) {
c[0] = WheelPos * 3;
c[1] = 255 - WheelPos * 3;
c[2] = 0;
}
else if (WheelPos < 170) {
WheelPos -= 85;
c[0] = 255 - WheelPos * 3;
c[1] = 0;
c[2] = WheelPos * 3;
}
else {
WheelPos -= 170;
c[0] = 0;
c[1] = WheelPos * 3;
c[2] = 255 - WheelPos * 3;
}
return c;
}
void TwinkleMapPixels()
{
InitPixelStates();
while (true)
{
for (uint16_t i = 0; i < NUM_LEDS; i++) {
if (PixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
if (random8() < CHANCE_OF_TWINKLE) {
PixelState[i] = GettingBrighter;
}
}
else if (PixelState[i] == GettingBrighter) {
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if (leds[i] >= PEAK_COLOR) {
PixelState[i] = GettingDimmerAgain;
}
else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}
}
else { // getting dimmer again
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if (leds[i] <= BASE_COLOR) {
leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
PixelState[i] = SteadyDim;
}
else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;
}
}
}
FastLED.show();
FastLED.delay(globalDelay);
}
}
void InitPixelStates()
{
memset(PixelState, sizeof(PixelState), SteadyDim); // initialize all the pixels to SteadyDim.
fill_solid(leds, NUM_LEDS, BASE_COLOR);
}
void DelayForever()
{
while (true)
{
delay(100);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
}
void showStrip() {
FastLED.show();
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.