Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // included libraries
- #include <FastLED.h>
- #define LED_PIN 6
- #define NUM_LEDS 144
- #define BRIGHTNESS 255
- #define UPDATES_PER_SECOND 60 //setting anything lower will result in horrible flickering, no idea why.
- CRGB leds[NUM_LEDS];
- uint8_t Reverse = 0;
- uint8_t RandomReverse = 0;
- //freshly added settings
- uint8_t BrightnessAnim = 0; //should we animate brightness?
- uint8_t BeatSineMax = 10;
- uint8_t BeatSineBPM = 4;
- uint8_t SpeedMethod = 0; //0 static, (1), 1 is beatsine.
- const uint8_t StaticScale = (256 + (NUM_LEDS - 1)) / NUM_LEDS;
- // POST PROCESS
- // PaletteBrightness
- uint8_t PaletteBrightness = 125;
- // Fill settings
- uint8_t StartIndex = 0; //starting position of the gradient between 0-255, incrase it by 255 / LED_NUM to step one led.
- uint8_t SpeedIndex = 0; //Speed starting value. Eeach animation method works with different speed. Not linear, 0 means stop, 1-120 slowly incrase above 120 its speed up rapidly.
- uint8_t FillScale = StaticScale;
- // Fill methods
- uint8_t FillMethod = 1; /* 0 = same color fill, all leds are same color selected from palette, Startindex will be color of the leds!
- 1 = Gradient fill, maps the gradient on the leds.
- 2 = Sine wave fill.
- 3 = Random Noise based fill.
- */
- uint8_t AnimationMethod = 0; /* 0 = no animation
- 1 = Noise anim
- 2 = sine wave anim
- */
- // noise settings
- static uint16_t NoiseDistanceJump = random16(12345); // A random number for our noise generator.
- uint8_t AnimScale = 30;
- //Sine settings this settings needs to be reviewed!
- uint8_t SineOnePhase = 0;
- uint8_t SineOneWaveLenght = 20;
- uint8_t SineOneAmplitude = 10;
- uint8_t SineTwoPhase = 0;
- uint8_t SineTwoWaveLenght = 30;
- uint8_t SineTwoAmplitude = 10;
- DEFINE_GRADIENT_PALETTE( test_pl_bw ) {
- 0, 0, 0, 0,
- 255, 255, 255 ,255};
- DEFINE_GRADIENT_PALETTE( test_pl_r ) {
- 0, 255, 0,0,
- 255, 0, 0,255};
- DEFINE_GRADIENT_PALETTE( test_pl_g ) {
- 0,0,255,0,
- 255,255,0,0};
- DEFINE_GRADIENT_PALETTE( test_pl_b ) {
- 0,0, 0,255,
- 255,0, 255,0};
- DEFINE_GRADIENT_PALETTE( test_pl_y) {
- 0, 255, 255,0,
- 255, 0,255,255};
- DEFINE_GRADIENT_PALETTE( test_pl_c ) {
- 0,0,255,255,
- 255,255,0,255};
- DEFINE_GRADIENT_PALETTE( test_pl_m ) {
- 0,255, 0,255,
- 255,255,255,0};
- //defining starting palettes
- CRGBPalette16 currentPalette( test_pl_r ); // starting palette, fade from black
- CRGBPalette16 BwPalette ( test_pl_bw ); // starting palette, fade from black
- // fill palettes.
- void AnimateAndFill() {
- static uint8_t FillIndex;
- static uint8_t ReversTimer = 8;
- uint8_t AnimationIndex;
- uint8_t AnimationAndSpeed;
- // Effect speed global, nudges everything from left to right!
- static int effectSpeed = 0; //animation speed, This is not the speed you want to set. use SpeedIndex!
- if (Reverse) {
- effectSpeed = +effectSpeed; }
- else {
- effectSpeed = -effectSpeed; //making it go backwards.
- }
- for( int i = 0; i < NUM_LEDS; i++) {
- switch (FillMethod) {
- case 0: FillIndex = StartIndex; break;
- case 1: FillIndex = i * FillScale + StartIndex; break;
- case 2: FillIndex = sin8(i*FillScale) + StartIndex; break;
- case 3: FillIndex = inoise8(i* FillScale) + StartIndex; break;
- // case 3: FillIndex = inoise8(i*FillScale, NoiseDistanceJump+i*FillScale) % 255; break;
- }
- switch (AnimationMethod) {
- case 0: AnimationIndex = 0; break;
- case 1: AnimationIndex = inoise8(i*AnimScale, NoiseDistanceJump+i*AnimScale); break;
- case 2: AnimationIndex = sin8((i+SineOnePhase)*SineOneWaveLenght)*SineOneAmplitude; break;
- }
- if (BrightnessAnim) {
- leds[i] = ColorFromPalette(currentPalette, FillIndex + AnimationIndex + effectSpeed, PaletteBrightness);
- leds[i] -= ColorFromPalette(BwPalette, i*2 + AnimationIndex + effectSpeed, PaletteBrightness); }
- /* This part, is inteded to separately animate brightness, by substracting a full black and white palette from the original palette.
- It works, but i'm not entirely happy with this solution. However, any way I tried to animate PaletteBrightness the animation comes
- out very blocky.
- you might want to change the i*2 to 256 / NUM_LEDS or: uint16_t StaticScale = (256 + (NUM_LEDS - 1)) / NUM_LEDS;
- */
- else {
- leds[i] = ColorFromPalette(currentPalette, FillIndex + AnimationIndex + effectSpeed, PaletteBrightness); break;
- }
- }
- //Speed is set by waiting a bit after every frame.
- if (SpeedIndex) {
- uint8_t BeatSine = beatsin8(BeatSineBPM,1,BeatSineMax);
- effectSpeed = abs(effectSpeed); //get the absolute value of effectspeed, in case its negative, cause we going backwards.
- switch (SpeedMethod) {
- case 0: effectSpeed += 1; break;
- case 1: effectSpeed += BeatSine; break;
- }
- NoiseDistanceJump += BeatSine;
- uint8_t Delay = 100 - SpeedIndex;
- unsigned long Speed = millis();
- while ( Speed + Delay > millis() ) {
- yield(); //pondering existence.
- }
- }
- }
- void Control()
- {
- String inData;
- while (Serial.available() > 0)
- {
- char recieved = Serial.read();
- inData += recieved;
- // Process message when new line character is recieved
- if (recieved == '\n')
- {
- Serial.println("");
- Serial.print("Arduino Received: ");
- Serial.print(inData);
- // whole word commands
- if(inData == "banim\n"){ // DON'T forget to add "\n" at the end of the string.
- if (BrightnessAnim) {
- BrightnessAnim = 0;}
- else {
- BrightnessAnim = 1;
- }
- Serial.print("BrightnessAnim: " );
- Serial.println( BrightnessAnim );
- }
- if(inData == "rand\n"){ // DON'T forget to add "\n" at the end of the string.
- if (RandomReverse) {
- RandomReverse = 0;}
- else {
- RandomReverse = 1;
- }
- Serial.print("RandomReverse: " );
- Serial.println( RandomReverse );
- }
- // commands with numbers
- if(inData.startsWith("br")) {
- inData.remove(0,2);
- PaletteBrightness = inData.toInt();
- Serial.print("PaletteBrightness: ");
- Serial.println( PaletteBrightness );
- }
- if(inData.startsWith("sp")) {
- inData.remove(0,2);
- SpeedIndex = inData.toInt();
- Serial.print("SpeedIndex : ");
- Serial.println( SpeedIndex );
- }
- if(inData.startsWith("fs")) {
- inData.remove(0,2);
- FillScale = inData.toInt();
- Serial.print("FillScale : ");
- Serial.println( FillScale );
- }
- if(inData.startsWith("fm")) {
- inData.remove(0,2);
- FillMethod = inData.toInt();
- Serial.print("FillMethod : ");
- Serial.println( FillMethod );
- }
- if(inData.startsWith("am")) {
- inData.remove(0,2);
- AnimationMethod = inData.toInt();
- Serial.print("AnimationMethod : ");
- Serial.println( AnimationMethod );
- }
- if(inData.startsWith("as")) {
- inData.remove(0,2);
- AnimScale = inData.toInt();
- Serial.print("AnimScale : ");
- Serial.println( AnimScale );
- }
- if(inData.startsWith("bm")) {
- inData.remove(0,2);
- BeatSineMax = inData.toInt();
- Serial.print("BeatSineMax : ");
- Serial.println( BeatSineMax );
- }
- if(inData.startsWith("bp")) {
- inData.remove(0,2);
- BeatSineBPM = inData.toInt();
- Serial.print("BeatSineBPM : ");
- Serial.println( BeatSineBPM );
- }
- if(inData.startsWith("sm")) {
- inData.remove(0,2);
- SpeedMethod = inData.toInt();
- Serial.print("SpeedMethod : ");
- Serial.println( SpeedMethod );
- }
- if(inData.startsWith("rv")) {
- inData.remove(0,2);
- Reverse = inData.toInt();
- Serial.print("Reverse : ");
- Serial.println( Reverse );
- }
- if(inData.startsWith("np")) {
- inData.remove(0,2);
- uint8_t NextPalette = inData.toInt();
- Serial.print("Next Palette : ");
- Serial.println( NextPalette );
- switch (NextPalette) {
- case 0: currentPalette = test_pl_r;
- case 1: currentPalette = test_pl_g;
- case 2: currentPalette = test_pl_b;
- case 3: currentPalette = test_pl_c;
- case 4: currentPalette = test_pl_m;
- case 5: currentPalette = test_pl_y;
- case 6: currentPalette = test_pl_bw;
- }
- }
- inData = ""; // Clear recieved buffer
- }
- }
- }
- void setup() {
- Serial.begin(9600);
- FastLED.addLeds<WS2811, LED_PIN, BRG>(leds, NUM_LEDS);
- FastLED.setCorrection( TypicalSMD5050 );
- FastLED.setBrightness( 255 );
- delay( 3000 ); // power-up safety delay
- } //end of setup
- void loop()
- {
- Control();
- AnimateAndFill();
- // FastLED.show(); fastled.delay already calling show.
- FastLED.delay(1000 / UPDATES_PER_SECOND);
- }
Advertisement
Add Comment
Please, Sign In to add comment