//oldschool twister demo effect
//rgb led matrix demo
//Yaroslaw Turbin 03-05-2021
//https://vk.com/ldirko
//https://www.reddit.com/user/ldirko/
#include <FastLED.h>
#define DATA_PIN 3 //set your datapin
#define LED_TYPE WS2812B //leds type
#define COLOR_ORDER GRB //color order of leds
#define NUM_COLS 16 //matrix resolution
#define NUM_ROWS 16 //matrix resolution
#define NUM_LEDS NUM_COLS*NUM_ROWS
#define MAX_POWER_MILLIAMPS 800 //write here your power in milliamps. default i set 800 mA for safety
#define BRIGHTNESS 255
CRGB leds [NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
// FastLED.setMaxPowerInVoltsAndMilliamps( 5, MAX_POWER_MILLIAMPS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
void loop() {
uint16_t a = millis() / 10;
LEDS.clear();
for (uint16_t i = 0; i < NUM_ROWS; i++) {
byte sinOff = sin8(i*8/PI+cos8(a/2+i)/4+a/3);
byte x1 = sin8(sinOff+a)*(NUM_COLS)/255;
byte x2 = sin8(sinOff+a+64)*(NUM_COLS)/255;
byte x3 = sin8(sinOff+a+128)*(NUM_COLS)/255;
byte x4 = sin8(sinOff+a+192)*(NUM_COLS)/255;
x1= x1>=NUM_COLS? (NUM_COLS-1):x1;
x2= x2>=NUM_COLS? (NUM_COLS-1):x2;
x3= x3>=NUM_COLS? (NUM_COLS-1):x3;
x4= x4>=NUM_COLS? (NUM_COLS-1):x4;
byte hueColor = sin8(a/20);
if (x1<x2) mydrawLine(x1, x2, i, CHSV (hueColor,255,255), 1, 1,i,0,sinOff,a);
if (x2<x3) mydrawLine(x2, x3, i, CHSV (hueColor+64,255,255), 1, 1,i,1,sinOff,a);
if (x3<x4) mydrawLine(x3, x4, i, CHSV (hueColor+128,255,255), 1, 1,i,2,sinOff,a);
if (x4<x1) mydrawLine(x4, x1, i, CHSV (hueColor+192,255,255), 1,1,i,3,sinOff,a);
}
LEDS.show();
}
void mydrawLine(byte x, byte x1, byte y, CHSV color, bool dot, bool grad, byte numline, byte side, byte sinOff, uint16_t a) { // my ugly hori line draw function )))
byte steps = abs8(x1 - x)+1 ;
for (uint16_t i = 1; i <= steps; i++) {
byte dx = lerp8by8(x1, x, i * 255 / steps);
uint16_t index = XY(dx, y);
leds[index] = color;
if (grad) leds[index] %= (sin8(numline*8+side*64+a+sinOff)+i*255/steps)/2; //for draw gradient line
}
if (dot) { //add white point at the ends of line
leds[XY(x, y)] = CRGB::Black;
leds[XY(x1, y)] = CRGB::Black;
}
}
uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);} //simple function to find led number in led matrix,