// Use an array of independent millis() timers to track independent events
// Circuit based on https://wokwi.com/projects/287302452979433992
// for https://forum.arduino.cc/t/doing-1000-independent-things-at-the-same-time-with-millis/1084860/
// DaveX 2023-01-31 Apache 2.0
// uses millis() per https://www.gammon.com.au/millis
// and comparison of future timestamps per https://arduino.stackexchange.com/a/12588/6628
// For 1600 timers/lEDs , try swappin in the code from
// https://forum.arduino.cc/t/doing-1000-independent-things-at-the-same-time-with-millis/1084860/26?u=davex
#include <FastLED.h>
#define TIMING 0
#define LED_PIN 3
#define NUM_LEDS 1000 //1120
#define NUM_SPARKLE NUM_LEDS // 800
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define MIN_OFF_MS 5000L
#define MAX_OFF_MS 10000L
#define MIN_ON_MS 1000L
#define MAX_ON_MS 1000L
CRGB leds[NUM_LEDS];
#define NUM_RINGS (sizeof(led_count) / sizeof(led_count[0]))
#define FIRE_WIDTH 64
#define FIRE_HEIGHT NUM_RINGS
const int debug = 0;
unsigned long next_change_time[NUM_LEDS]; // memory for timers
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
for (int i = 0 ; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
next_change_time[i] = random(MAX_OFF_MS);
}
Serial.print("LEDs On:{");
Serial.print(MIN_ON_MS);
Serial.print(',');
Serial.print(MAX_ON_MS);
Serial.print("}ms Off: {");
Serial.print(MIN_OFF_MS);
Serial.print(',');
Serial.print(MAX_OFF_MS);
Serial.println("}ms");
}
void loop() {
unsigned long t1 = millis();
int changes = 0;
CRGB *led = leds;
static unsigned long last = 0;
const unsigned long interval = 50 ; //ms
if ( t1 - last >= interval) { // limit rate
last += interval;
for (int i = 0; i < NUM_SPARKLE; ) {
if ((signed long)(t1 - next_change_time[i]) > 0) {
// time for a change
if (leds[i]) { // lit?
leds[i] = CRGB::Black;
next_change_time[i] += random(MIN_OFF_MS, MAX_OFF_MS);
} else { // light it up
// https://github.com/FastLED/FastLED/wiki/Pixel-reference
leds[i] = CHSV(random(256), 255, 255);
next_change_time[i] += random(MIN_ON_MS, MAX_ON_MS);;
}
changes++;
}
i++;
}
}
if (changes) { // update LEDS
FastLED.show();
if(debug){Serial.print('*');Serial.print(changes);}
} else {
if(debug){Serial.print('.');}
}
report();
}
void report(){
const int interval = 1000;
static unsigned long last = -interval;
static long count = 0;
++count;
if(millis() - last < interval ) return;
last += interval;
Serial.print("loop() count:");
Serial.print(count);
Serial.print("/sec or ms:");
Serial.print(1000.0/count);
Serial.println();
count=0;
}