#include "FastLED.h"
// LEDs pin
#define DATA_PIN 5
#define CLOCK_PIN 13
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
// LED brightness
#define BRIGHTNESS 255
#define NUM_COLS_SEGMENT 11 // resolution of planar lookup table
#define NUM_ROWS_SEGMENT 11 // resolution of planar lookup table
#define NUM_LEDS_SEGMENT NUM_COLS_SEGMENT*NUM_ROWS_SEGMENT
#define NUM_COLS_CILINDR 55 // resolution of circular lookup table
#define NUM_ROWS_CILINDR 11 // resolution of circular lookup table
#define NUM_LEDS_CIRCULAR NUM_COLS_CILINDR*NUM_ROWS_CILINDR
#define NUM_LEDS 180
int lastSafeIndex = 180;
CRGB leds[NUM_LEDS];
#define SECONDS_PER_PALETTE 15
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette;
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
Serial.begin(115200);
next_Random_Pallete ();
}
const uint32_t indexcolors[] = { //some colors. need tune
0xF0F8FF, 0x9966CC, 0xFAEBD7, 0x00FFFF, 0x7FFFD4, 0xF0FFFF, 0xF5F5DC, 0xFFE4C4, 0x000000, 0xFFEBCD, 0x0000FF, 0x8A2BE2, 0xA52A2A, 0xDEB887, 0x5F9EA0, 0x7FFF00, 0xD2691E, 0xFF7F50, 0x6495ED, 0xFFF8DC, 0xDC143C, 0x00FFFF, 0x00008B, 0x008B8B, 0xB8860B, 0xA9A9A9, 0xA9A9A9, 0x006400, 0xBDB76B, 0x8B008B, 0x556B2F, 0xFF8C00, 0x9932CC, 0x8B0000, 0xE9967A, 0x8FBC8F, 0x483D8B, 0x2F4F4F, 0x2F4F4F, 0x00CED1, 0x9400D3, 0xFF1493, 0x00BFFF, 0x696969, 0x696969, 0x1E90FF, 0xB22222, 0xFFFAF0, 0x228B22, 0xFF00FF, 0xDCDCDC, 0xF8F8FF, 0xFFD700, 0xDAA520, 0x808080, 0x808080, 0x008000, 0xADFF2F, 0xF0FFF0, 0xFF69B4, 0xCD5C5C, 0x4B0082, 0xFFFFF0, 0xF0E68C, 0xE6E6FA, 0xFFF0F5, 0x7CFC00, 0xFFFACD, 0xADD8E6, 0xF08080, 0xE0FFFF, 0xFAFAD2, 0x90EE90, 0xD3D3D3, 0xFFB6C1, 0xFFA07A, 0x20B2AA, 0x87CEFA, 0x778899, 0x778899, 0xB0C4DE, 0xFFFFE0, 0x00FF00, 0x32CD32, 0xFAF0E6, 0xFF00FF, 0x800000, 0x66CDAA, 0x0000CD, 0xBA55D3, 0x9370DB, 0x3CB371, 0x7B68EE, 0x00FA9A, 0x48D1CC, 0xC71585, 0x191970, 0xF5FFFA, 0xFFE4E1, 0xFFE4B5, 0xFFDEAD, 0x000080, 0xFDF5E6, 0x808000, 0x6B8E23, 0xFFA500, 0xFF4500, 0xDA70D6, 0xEEE8AA, 0x98FB98, 0xAFEEEE, 0xDB7093, 0xFFEFD5, 0xFFDAB9, 0xCD853F, 0xFFC0CB, 0xCC5533, 0xDDA0DD, 0xB0E0E6, 0x800080, 0xFF0000, 0xBC8F8F, 0x4169E1, 0x8B4513, 0xFA8072, 0xF4A460, 0x2E8B57, 0xFFF5EE, 0xA0522D, 0xC0C0C0, 0x87CEEB, 0x6A5ACD, 0x708090, 0x708090, 0xFFFAFA, 0x00FF7F, 0x4682B4, 0xD2B48C, 0x008080, 0xD8BFD8, 0xFF6347, 0x40E0D0, 0xEE82EE, 0xF5DEB3, 0xFFFFFF, 0xF5F5F5, 0xFFFF00, 0x9ACD32, 0xFFE42D, 0xFF9D2A
};
const uint8_t cos_wave [256] = {
0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 6, 6, 8,
9, 10, 11, 12, 14, 15, 17, 18, 20, 22, 23, 25, 27, 29, 31,
33, 35, 38, 40, 42, 45, 47, 49, 52, 54, 57, 60, 62, 65, 68,
71, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 113,
116, 119, 122, 125, 128, 131, 135, 138, 141, 144, 147, 150, 153, 156, 159,
162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 199, 202,
204, 207, 209, 212, 214, 216, 218, 221, 223, 225, 227, 229, 231, 232, 234,
236, 238, 239, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252,
253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253,
253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 241, 239, 238,
236, 234, 232, 231, 229, 227, 225, 223, 221, 218, 216, 214, 212, 209, 207,
204, 202, 199, 197, 194, 191, 189, 186, 183, 180, 177, 174, 171, 168, 165,
162, 159, 156, 153, 150, 147, 144, 141, 138, 135, 131, 128, 125, 122, 119,
116, 113, 109, 106, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 73,
71, 68, 65, 62, 60, 57, 54, 52, 49, 47, 45, 42, 40, 38, 35,
33, 31, 29, 27, 25, 23, 22, 20, 18, 17, 15, 14, 12, 11, 10,
9, 8, 6, 6, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0
};
void testlayout(){
static byte index = 0;
fade_raw(leds, NUM_LEDS, 10);
leds [index].setHue(sin8((index = ++index%(NUM_LEDS))*255/(NUM_LEDS-1)+millis()/16));
delay (50);
}
void twincle() {
static byte offset = 0;
EVERY_N_MILLISECONDS(200) {
offset++;
}
random16_set_seed(23579);
double a = millis();
for (int i = 0; i < NUM_LEDS; i++) {
byte hue = sin8((random16() / 256) + inoise8(random16() / 256 + a / 32) + i * 4);
byte bright = sin8(random16() / 256 + inoise8(random16() / 256 + a / 32) + a / 8);
byte satur = sin8(random16() / 256 + inoise8(random16() / 256 + a / 32) + a / 32);
nblend(leds[i], CHSV(hue + offset, 0, bright), 128);
}
delay(20);
}
void fire2021_segmens () {
int a = millis();
int a1 = a / 3;
for (byte j = 0; j < NUM_ROWS_SEGMENT; j++) {
for (byte i = 0; i < NUM_COLS_SEGMENT; i++) {
CRGB color = HeatColor(qsub8 (inoise8 (i * 80, j * 80 + a, a1), abs8(j - (NUM_ROWS_SEGMENT - 1)) * 255 / (NUM_ROWS_SEGMENT + 4)));
for (int s = 0; s < 5; s++) {
int ledsindex = XY_Segment(i, j, s);
leds[ledsindex]=color;
}
}
}
}
void fire2021_circular () {
int a = millis();
int a1 = a / 3;
for (int j = 0; j < NUM_ROWS_CILINDR; j++) {
for (int i = 0; i < NUM_COLS_CILINDR; i++) {
CRGB color = HeatColor(qsub8 (inoise8 (i * 80, j * 80 + a, a1), abs8(j - (NUM_ROWS_CILINDR - 1)) * 255 / (NUM_ROWS_CILINDR + 3)));
int ledsindex = XY_Circular(i, j);
if (ledsindex == lastSafeIndex) continue;
leds[ledsindex]=color;
}
}
}
void RGB_hiphotic_segments () {
CRGB color;
int a = millis()/8;
for (int x = 0; x < NUM_COLS_SEGMENT; x++) {
for (int y = 0; y < NUM_ROWS_SEGMENT; y++) {
color.b=sin8((x-8)*cos8((y+20)*4)/4+a);
color.g=(sin8(x*16+a/3)+cos8(y*8+a/2))/2;
color.r=sin8(cos8(x*8+a/3)+sin8(y*8+a/4)+a);
for (int s = 0; s < 5; s++) {
int ledsindex = XY_Segment(x, y, s);
leds[ledsindex]=color;
}
}
}
// GammaCorrection();
}
void Distortion_Waves_plan_segments() {
byte speed = 5;
uint8_t w = 1;
uint8_t scale = 1;
uint16_t a=millis()/24;
uint16_t a2=a/2;
uint16_t a3=a/3;
uint16_t cx = beatsin8 (10-speed,0,NUM_COLS_SEGMENT)*scale;
uint16_t cy = beatsin8 (12-speed,0,NUM_ROWS_SEGMENT)*scale;
uint16_t cx1 = beatsin8 (13-speed,0,NUM_COLS_SEGMENT)*scale;
uint16_t cy1 = beatsin8 (15-speed,0,NUM_ROWS_SEGMENT)*scale;
uint16_t cx2 = beatsin8 (17-speed,0,NUM_COLS_SEGMENT)*scale;
uint16_t cy2 = beatsin8 (14-speed,0,NUM_ROWS_SEGMENT)*scale;
uint16_t xoffs=0;
for (int x = 0; x < NUM_COLS_SEGMENT; x++) {
xoffs += scale;
uint16_t yoffs = 0;
for (int y = 0; y < NUM_ROWS_SEGMENT; y++) {
yoffs += scale;
byte rdistort = cos_wave [(cos_wave[((x<<3)+a )&255]+cos_wave[((y<<3)-a2)&255]+a3 )&255]>>1;
byte gdistort = cos_wave [(cos_wave[((x<<3)-a2)&255]+cos_wave[((y<<3)+a3)&255]+a+32 )&255]>>1;
byte bdistort = cos_wave [(cos_wave[((x<<3)+a3)&255]+cos_wave[((y<<3)-a) &255]+a2+64)&255]>>1;
byte valueR = rdistort+ w* (a- ( ((xoffs - cx) * (xoffs - cx) + (yoffs - cy) * (yoffs - cy))>>7 ));
byte valueG = gdistort+ w* (a2-( ((xoffs - cx1) * (xoffs - cx1) + (yoffs - cy1) * (yoffs - cy1))>>7 ));
byte valueB = bdistort+ w* (a3-( ((xoffs - cx2) * (xoffs - cx2) + (yoffs - cy2) * (yoffs - cy2))>>7 ));
valueR = cos_wave [valueR];
valueG = cos_wave [valueG];
valueB = cos_wave [valueB];
CRGB color = CRGB (valueR,valueG,valueB);
for (int s = 0; s < 5; s++) {
int ledsindex = XY_Segment(x, y, s);
leds[ledsindex]=color;
}
}
}
// GammaCorrection();
}
//Spiral_____________________________________
void Spiral () {
uint16_t a=millis()/8;
float scale = (sin(a/32*PI/180)*14)-4; //-18 10
float scale1 =0;
for (int i = 0; i < NUM_COLS_CILINDR; i++) {
for (int j = 0; j < NUM_ROWS_CILINDR; j++) {
uint16_t index = XY_Circular(i,j);
if (index == lastSafeIndex) continue;
CRGB newcolor = ColorFromPalette( gCurrentPalette, (i*255/(NUM_COLS_CILINDR-1)+j*255/(NUM_ROWS_CILINDR-1))+(int)scale1+a+sin16(a)/16384, 255);
nblend( leds[index], newcolor, 16);
}
scale1 += scale;
}
}
uint32_t NextRandomColor (){
static int tableIndex = 1023;
tableIndex= (tableIndex+1)%(sizeof(indexcolors)/4);
return (indexcolors[tableIndex]);
}
void next_Random_Pallete () {
for (int i = 0; i < 16; i++) {
gTargetPalette[i] = CRGB(0, 0, 0);
}
for( int i = 0; i < 12; i++) {
gTargetPalette[i] = indexcolors[random16(150)];
// gTargetPalette[i*2+1] =indexcolors[randomIndex = random16(150)];
}
}
void loop() {
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) { //random change palettes
next_Random_Pallete ();
}
EVERY_N_MILLISECONDS(40) { //blend current palette to next palette
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
//twincle();
Spiral();
//fire2021_segmens();
// fire2021_circular();
// RGB_hiphotic_segments();
//Distortion_Waves_plan_segments();
FastLED.show();
delay(4);
}
#define NUM_COLS_PLANAR 17 // resolution of planar lookup table
#define NUM_ROWS_PLANAR 18
// static const uint16_t FibonPlanarTable [] PROGMEM ={ //from Butterbrott https://wokwi.com/projects/347217437609951828
// 180,180,180,180,180,180,180,180,27,180,180,180,180,180,180,180,180,
// 180,180,180,180,180,180,180,26,180,28,180,180,180,180,180,180,180,
// 180,180,180,180,180,180,180,25,77,29,180,180,180,180,180,180,180,
// 180,180,180,180,180,180,24,78,180,76,30,180,180,180,180,180,180,
// 180,180,180,180,180,180,23,79,117,75,31,180,180,180,180,180,180,
// 180,180,180,180,180,22,80,116,180,118,74,32,180,180,180,180,180,
// 16,17,18,19,20,21,81,115,147,119,73,33,34,35,36,37,38,
// 15,86,85,84,83,82,114,148,180,146,120,72,71,70,69,68,39,
// 180,14,87,110,111,112,113,149,167,145,121,122,123,124,67,40,180,
// 180,13,88,109,152,151,150,166,180,168,144,143,142,125,66,41,180,
// 180,180,12,89,108,153,164,165,177,169,170,141,126,65,42,180,180,
// 180,180,11,90,107,154,163,178,180,176,171,140,127,64,43,180,180,
// 180,180,10,91,106,155,162,179,180,175,172,139,128,63,44,180,180,
// 180,180,9,92,105,156,161,160,180,174,173,138,129,62,45,180,180,
// 180,180,8,93,104,157,158,159,180,135,136,137,130,61,46,180,180,
// 180,180,7,94,103,102,101,100,180,134,133,132,131,60,47,180,180,
// 180,180,6,95,96,97,98,99,180,55,56,57,58,59,48,180,180,
// 180,180,5,4,3,2,1,0,180,54,53,52,51,50,49,180,180
// };
static const byte GegmenstTable [] PROGMEM ={
180,180,180,180,180,27,180,180,180,180,180,
180,180,180,180,26,180,28,180,180,180,180,
180,180,180,25,180,77,180,29,180,180,180,
180,180,24,180,78,180,76,180,30,180,180,
180,23,180,79,180,117,180,75,180,31,180,
22,180,80,180,116,180,118,180,74,180,32,
180,81,180,115,180,147,180,119,180,73,180,
180,180,114,180,148,180,146,180,120,180,180,
180,180,180,149,180,167,180,145,180,180,180,
180,180,180,180,166,180,168,180,180,180,180,
180,180,180,180,180,177,180,180,180,180,180,
180,180,180,180,180,38,180,180,180,180,180,
180,180,180,180,37,180,39,180,180,180,180,
180,180,180,36,180,68,180,40,180,180,180,
180,180,35,180,69,180,67,180,41,180,180,
180,34,180,70,180,124,180,66,180,42,180,
33,180,71,180,123,180,125,180,65,180,43,
180,72,180,122,180,142,180,126,180,64,180,
180,180,121,180,143,180,141,180,127,180,180,
180,180,180,144,180,170,180,140,180,180,180,
180,180,180,180,169,180,171,180,180,180,180,
180,180,180,180,180,176,180,180,180,180,180,
180,180,180,180,180,49,180,180,180,180,180,
180,180,180,180,48,180,50,180,180,180,180,
180,180,180,47,180,59,180,51,180,180,180,
180,180,46,180,60,180,58,180,52,180,180,
180,45,180,61,180,131,180,57,180,53,180,
44,180,62,180,130,180,132,180,56,180,54,
180,63,180,129,180,137,180,133,180,55,180,
180,180,128,180,138,180,136,180,134,180,180,
180,180,180,139,180,173,180,135,180,180,180,
180,180,180,180,172,180,174,180,180,180,180,
180,180,180,180,180,175,180,180,180,180,180,
180,180,180,180,180,5,180,180,180,180,180,
180,180,180,180,4,180,6,180,180,180,180,
180,180,180,3,180,95,180,7,180,180,180,
180,180,2,180,96,180,94,180,8,180,180,
180,1,180,97,180,103,180,93,180,9,180,
0,180,98,180,102,180,104,180,92,180,10,
180,99,180,101,180,157,180,105,180,91,180,
180,180,100,180,158,180,156,180,106,180,180,
180,180,180,159,180,161,180,155,180,180,180,
180,180,180,180,160,180,162,180,180,180,180,
180,180,180,180,180,179,180,180,180,180,180,
180,180,180,180,180,16,180,180,180,180,180,
180,180,180,180,15,180,17,180,180,180,180,
180,180,180,14,180,86,180,18,180,180,180,
180,180,13,180,87,180,85,180,19,180,180,
180,12,180,88,180,110,180,84,180,20,180,
11,180,89,180,109,180,111,180,83,180,21,
180,90,180,108,180,152,180,112,180,82,180,
180,180,107,180,153,180,151,180,113,180,180,
180,180,180,154,180,164,180,150,180,180,180,
180,180,180,180,163,180,165,180,180,180,180,
180,180,180,180,180,178,180,180,180,180,180
};
static const byte StarCircularTable [] PROGMEM ={ //11x55
180,180,180,180,180,49,180,180,180,180,180,180,180,180,180,180,38,180,180,180,180,180,180,180,180,180,180,27,180,180,180,180,180,180,180,180,180,180,16,180,180,180,180,180,180,180,180,180,180,5,180,180,180,180,180,
180,180,180,180,50,180,48,180,180,180,180,180,180,180,180,39,180,37,180,180,180,180,180,180,180,180,28,180,26,180,180,180,180,180,180,180,180,17,180,15,180,180,180,180,180,180,180,180,6,180,4,180,180,180,180,
180,180,180,51,180,59,180,47,180,180,180,180,180,180,40,180,68,180,36,180,180,180,180,180,180,29,180,77,180,25,180,180,180,180,180,180,18,180,86,180,14,180,180,180,180,180,180,7,180,95,180,3,180,180,180,
180,180,52,180,58,180,60,180,46,180,180,180,180,41,180,67,180,69,180,35,180,180,180,180,30,180,76,180,78,180,24,180,180,180,180,19,180,85,180,87,180,13,180,180,180,180,8,180,94,180,96,180,2,180,180,
180,53,180,57,180,131,180,61,180,45,180,180,42,180,66,180,124,180,70,180,34,180,180,31,180,75,180,117,180,79,180,23,180,180,20,180,84,180,110,180,88,180,12,180,180,9,180,93,180,103,180,97,180,1,180,
54,56,180,180,132,180,130,180,180,62,44,43,65,180,180,125,180,123,180,180,71,33,32,74,180,180,118,180,116,180,180,80,22,21,83,180,180,111,180,109,180,180,89,11,10,92,180,180,104,180,102,180,180,98,0,
55,180,133,180,180,137,180,180,129,180,63,64,180,126,180,180,142,180,180,122,180,72,73,180,119,180,180,147,180,180,115,180,81,82,180,112,180,180,152,180,180,108,180,90,91,180,105,180,180,157,180,180,101,180,99,
134,180,180,136,180,180,180,138,180,180,128,127,180,180,141,180,180,180,143,180,180,121,120,180,180,146,180,180,180,148,180,180,114,113,180,180,151,180,180,180,153,180,180,107,106,180,180,156,180,180,180,158,180,180,100,
135,180,180,180,180,173,180,180,180,180,139,140,180,180,180,180,170,180,180,180,180,144,145,180,180,180,180,167,180,180,180,180,149,150,180,180,180,180,164,180,180,180,180,154,155,180,180,180,180,161,180,180,180,180,159,
180,174,180,180,180,180,180,180,180,172,180,180,171,180,180,180,180,180,180,180,169,180,180,168,180,180,180,180,180,180,180,166,180,180,165,180,180,180,180,180,180,180,163,180,180,162,180,180,180,180,180,180,180,160,180,
180,180,180,180,180,175,180,180,180,180,180,180,180,180,180,180,176,180,180,180,180,180,180,180,180,180,180,177,180,180,180,180,180,180,180,180,180,180,178,180,180,180,180,180,180,180,180,180,180,179,180,180,180,180,180
};
int XY_Segment(byte x, byte y, byte seg){
int index = pgm_read_byte (GegmenstTable + y * NUM_COLS_SEGMENT + x + seg*NUM_LEDS_SEGMENT);
return(index);
}
int XY_Circular(byte x, byte y){
int index = pgm_read_byte (StarCircularTable + y * NUM_COLS_CILINDR + x);
if (index>180) Serial.println(index);
return(index);
}
uint32_t random_two_shifts() { //found this in https://cdn.hackaday.io/files/10721458687264/bh-badge-animate.c
static uint32_t r_z = 362436069, r_w = 521288629;
r_z = 36969 * (r_z & 65535) + (r_z >> 16);
r_w = 18000 * (r_w & 65535) + (r_w >> 16);
return (r_z << 16) + r_w;
}