Skip to content

Instantly share code, notes, and snippets.

@useafterfree
Created August 25, 2022 02:57
Show Gist options
  • Save useafterfree/75cfc1b17c918d34420c09f77c9f94be to your computer and use it in GitHub Desktop.
Save useafterfree/75cfc1b17c918d34420c09f77c9f94be to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 29
#define DATA_PIN A9 // A9 // 23
#define MAX_BRIGHTNESS 255 // 64 - 255 is acceptable
// Lightning stuff
#define FPS 50
#define FLASHES 8
#define FREQUENCY 2 // delay between strikes
// Define the array of leds
struct CRGB leds[NUM_LEDS];
const int8_t HSV_PINK = -55;
const int8_t HSV_RED = 5;
int delayTime = 50;
uint8_t hue = 0;
// PINK CYCLE
int8_t pinkInc = HSV_PINK;
int8_t direction = 1;
uint8_t rainbowSpacing = 10;
unsigned int dimmer = 1;
int sceneId = 0;
// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8(uint8_t &cur, const uint8_t target, uint8_t amount)
{
if (cur == target)
return;
if (cur < target)
{
uint8_t delta = target - cur;
delta = scale8_video(delta, amount);
cur += delta;
}
else
{
uint8_t delta = cur - target;
delta = scale8_video(delta, amount);
cur -= delta;
}
}
// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
CRGB fadeTowardColor(CRGB &cur, const CRGB &target, uint8_t amount)
{
nblendU8TowardU8(cur.red, target.red, amount);
nblendU8TowardU8(cur.green, target.green, amount);
nblendU8TowardU8(cur.blue, target.blue, amount);
return cur;
}
// Fade an entire array of CRGBs toward a given background color by a given amount
// This function modifies the pixel array in place.
void fadeTowardColor(CRGB *L, uint16_t N, const CRGB &bgColor, uint8_t fadeAmount)
{
for (uint16_t i = 0; i < N; i++)
{
fadeTowardColor(L[i], bgColor, fadeAmount);
}
}
void setup()
{
delay(2000); // pixie IC will reset around this time
FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MAX_BRIGHTNESS);
}
void hueRainbow()
{
for (int i = 0; i < NUM_LEDS; ++i)
{
leds[i] = CHSV(hue + (i * rainbowSpacing), 255, 255);
}
// You can change the pattern speed here
EVERY_N_MILLISECONDS(delayTime)
{
hue++;
}
FastLED.show();
}
void pinkToRed()
{
// 0 -> 210
// drop green
for (int i = 0; i < NUM_LEDS; ++i)
{
leds[i] = CHSV(pinkInc, 255, 255);
}
// You can change the pattern speed here
EVERY_N_MILLISECONDS(delayTime)
{
pinkInc += direction;
if (pinkInc >= HSV_RED || pinkInc <= HSV_PINK)
{
direction *= -1;
}
}
FastLED.show();
}
// stolen from https://github.com/fibonacci162
// The first "flash" in a bolt of lightning is the "leader." The leader
// is usually duller and has a longer delay until the next flash. Subsequent
// flashes, the "strokes," are brighter and happen at shorter intervals.
void lighting()
{
for (int flashCounter = 0; flashCounter < random8(3, FLASHES); flashCounter++)
{
if (flashCounter == 0)
dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
else
dimmer = random8(1, 3); // return strokes are brighter than the leader
FastLED.showColor(CHSV(255, 0, 255 / dimmer));
delay(random8(4, 10)); // each flash only lasts 4-10 milliseconds
FastLED.showColor(CHSV(255, 0, 0));
if (flashCounter == 0)
delay(150); // longer delay until next flash after the leader
delay(50 + random8(100)); // shorter delay between strikes
}
delay(random8(FREQUENCY) * 100); // delay between strikes
}
void rainbow()
{
uint8_t thisSpeed = 10;
uint8_t deltaHue = 10;
uint8_t thisHue = beat8(thisSpeed, 255);
fill_rainbow(leds, NUM_LEDS, thisHue, deltaHue);
FastLED.show();
}
void crossfad3r()
{
CRGB bgColor(0, 15, 2); // pine green ?
// fade all existing pixels toward bgColor by "5" (out of 255)
fadeTowardColor(leds, NUM_LEDS, bgColor, 5);
// periodically set random pixel to a random color, to show the fading
EVERY_N_MILLISECONDS(300)
{
uint16_t pos = random16(NUM_LEDS);
CRGB color = CHSV(random8(), 255, 255);
leds[pos] = color;
}
FastLED.show();
FastLED.delay(10);
}
// from https://forum.arduino.cc/t/array-of-functions/429541/4
void (*doScene[])(void) = {
pinkToRed,
crossfad3r,
lighting,
hueRainbow,
rainbow};
int sceneLen = sizeof(doScene);
void loop()
{
// EVERY_N_SECONDS(25) {
doScene[sceneId](); // pinkToRed
// sceneId++;
// if (sceneId > sceneLen - 1)
// {
// sceneId = 0; // start over
// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment