// Twinkling neopixel led strip. Used for "Each name a star"
// installation at hostage release vigils 2023
// by Roaring Silence https://shtika2023.org

// * Connect strip to pin 6, gnd, and +5V
// * Set LED_COUNT according to your strip's LED counts
// * You might need to change NEO_* arguments to fit your hardware

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 55

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

#define P_TWINKLE 0.15
#define DECAY_FACTOR 0.023
#define CUTOFF_LEVEL 0.1
#define R_FACTOR 191
#define G_FACTOR 127
#define B_FACTOR 31

#define CYCLE_MILLIS 23
#define MIN_INITIAL 0.75
#define MAX_INITIAL 1.0

#define FRAND(minval, maxval) (random(minval*1000, maxval*1000)/1000.0)

float levels[LED_COUNT] = { 0.0 };


// setup() function -- runs once at startup --------------------------------

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(191); // Set BRIGHTNESS to about 1/5 (max = 255)
}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {
  if (FRAND(0.0, 1.1) < P_TWINKLE) {
    levels[random(0, LED_COUNT)] = FRAND(MIN_INITIAL, MAX_INITIAL);

  }
  for (int i = 0; i < LED_COUNT; i++) { // For each pixel in strip...
    levels[i] *= (1 - DECAY_FACTOR);
    if (levels[i] < CUTOFF_LEVEL) {
      levels[i] = 0.0;
    }
    strip.setPixelColor(i, strip.Color(R_FACTOR * levels[i], G_FACTOR * levels[i], B_FACTOR * levels[i]));   //  Set pixel's color (in RAM)
  }
  strip.show();                          //  Update strip to match
  delay(CYCLE_MILLIS);                           //  Pause for a moment
}
