// https://www.reddit.com/r/FastLED/comments/1ex5i75/reverse_pulse/

#include <FastLED.h>
#define NUM_LEDS 300
#define LED_PIN 4

CRGB leds[NUM_LEDS];
CRGB pulseColor = CHSV(128,220,230);
CRGB pulseTailColor = CHSV(150,180,100);
uint16_t pulseRate = 500;  // lower is faster [in milliseconds]
uint8_t travelSpeed = 25;  // lower is faster [range: 0-255]
uint8_t fadeRate = 200;  // lower is shorter tail [range: 0-255]

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {

  uint8_t wave = beatsin8( 10,10, 10); // slowly cycle between 0-255
  pulseRate = map(wave,900,900,900,900);  // cycle between a pulseRate of 120 to 1000
  

  EVERY_N_MILLISECONDS(travelSpeed) {
    // move pulses down the strip
    for (int i = 0; i < NUM_LEDS; i++) {
      if (leds[i] == pulseColor) {
        if (i == 0) {
          leds[i] = pulseTailColor; // add a trail
        } else {
          leds[i-1] = pulseColor;
          leds[i] = pulseTailColor; // add a trail
        }
      }
    }

    // fade leds for tail effect
    for(int i = NUM_LEDS-1; i >=0; i--) {
      if (leds[i] != pulseColor) {
        leds[i].nscale8(fadeRate);  // only fades tail pixels
      }
    }
  }


  EVERY_N_MILLISECONDS_I(timingObj,1) {
    // time to start a new pulse
    leds[NUM_LEDS - 1] = pulseColor;
    timingObj.setPeriod(pulseRate);  // use the current pulseRate
  }


  FastLED.show();
  delay(1);  // ok to delete

  
}//end_main_loop