//********libraries********
//****LEDS****
#include <Arduino.h>
#include <FastLED.h>
//********DEFINITIONS********
//****LEDS****
#define numleds 41
#define ledpin 33
//********VARIABLES********
//****LEDS****
CRGB leds[numleds];
int brightness = 20;
void setPixel(int Pixel, byte red, byte green, byte blue)
{
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
FastLED.show();
}
void setAll(byte red, byte green, byte blue)
{
for(int i = 0; i < numleds; i++ )
{
setPixel(i, red, green, blue);
}
FastLED.show();
}
void SetLightning(int pixel, byte Bolt) // Bolt comes out in binary Example (0010 1001=41)(41/255=0.160)(0.160*191=30.70)(30.70 Rounded ↑ From.5 is 31) - (0x3F = 31 or 0001 1111)
// (0001 1111 compared to 0011 1111 = 0001 1111)(0001 1111 <<2 = 0111 1100)(0111 1100 = 124)
{
//Serial.print("Pixel: ");
//Serial.println(pixel);
//Serial.print("Binary: ");
//Serial.println(Bolt, BIN);
//Serial.println(pixel);
//Serial.print("Bolt: ");
//Serial.println(sizeof(Bolt));
//Serial.println("");
byte t192 = round((Bolt/255.0)*191);
//Serial.print("t192: ");
//Serial.println(t192, BIN);
//Serial.print("Binary: ");
//Serial.println(t192, BIN);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
// 00111111
//
heatramp <<= 2; // scale up to 0..252
//Serial.print("Heatramp: ");
//Serial.println(heatramp);
// figure out which third of the spectrum we're in:
if( t192 > 0x80) //-----------------------------128
{ // hottest
setPixel(pixel, 255, 255, heatramp + 20); // Spaks
}
else if( t192 > 0x40 ) //----------------------64
{
setPixel(pixel, 255, heatramp, 0); //Yellow
}
else
{ // coolest
setPixel(pixel, heatramp, 0, 0); // Fire
}
}
void LightningBolt(int cool, int speedDelay)
{
static byte flash[numleds];
int cooling;
for (int i = 0; i < numleds; i++)
{
cooling = random(0, ((cool * 10) / (numleds * 2) + 2));
//+cooling = random(0,((cool *12)/(numleds/2)));
if (cooling > flash[i])
{
flash[i] = 0;
}
else
{
flash[i] = flash[i] - cooling;
}
}
//cooling = random(0, ((cool * 12) / j) + 5);
for(int k = numleds - 1; k >= 2; k--)
{
flash[k] = (flash[k - 1] + flash[k - 2] + flash[k - 2]) / 3;
}
if (random(255) < numleds)
{
int y = random(7);
flash[y] = flash[y] + random(160, 255);
}
for(int j = 0; j < numleds - 1; j++)
{
SetLightning(j, flash[j]);
}
delay(speedDelay);
}
void setup() {
Serial.begin(115200);
Serial.println("Starting Serial Communications");
delay(500);
//****LEDS****
pinMode(ledpin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
//****LEDS****
FastLED.addLeds<WS2812B, ledpin, GRB>(leds, numleds);
FastLED.setBrightness(brightness);
//****LEDS****
set_max_power_indicator_LED(LED_BUILTIN);
FastLED.setMaxPowerInMilliWatts(900);
}
void loop()
{
LightningBolt(50,3);
}