// Experiments with Fastled beatsin16, beatsin88
// Based on code from https://forum.arduino.cc/t/fastled-beatsin16-just-one-way/1085303/4?u=davex
// modified to be mostly non-blocking
// Wokwi simulation at https://wokwi.com/projects/355578772690767873
#include <FastLED.h>
#define NUM_LEDS 250
#define LED_PIN 6
//CRGB leds[NUM_LEDS];
CRGBArray <NUM_LEDS> leds;
uint8_t data[ NUM_LEDS];
const float BPMf = 20; // BPM in floating point
const uint32_t BPM88 = BPMf * 256; // Q8.8 fixed point
const uint16_t BPM = BPMf; // integer
void setup() {
delay(300);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(255);
}
void movingDotred() {
const uint32_t interval = 60000UL / BPMf / 2; // = (60000ms/min)/20BPM/2
static unsigned long lastOffsetTime = -interval; // make an immediate change with -interval initialization trick
static uint32_t t0 = 0; // timebase
static uint16_t phase = 1 * 32768 / 2; // which segment of sine
if (millis() - lastOffsetTime >= interval) {
lastOffsetTime += interval;
t0 = millis() ; // update timebase
}
uint16_t sinBeat = beatsin88(BPM88, 0, NUM_LEDS - 1, t0, phase);
if(sinBeat < 150)
{
leds[sinBeat] = CRGB(0, 255, 0);
}
else
{
leds[sinBeat] = CRGB(255, 0, 0);
}
fadeToBlackBy(leds, NUM_LEDS, 3);
FastLED.show();
}
void loop() {
static uint32_t loopcount = 0;
static uint32_t fastcount = 0;
static uint32_t now = 0;
static uint32_t lastnow = -1;
static int seqVar = -1;
// use millis to update sequence:
static uint32_t lastseq = -1;
static uint32_t period2 = 0L; // X seconds
now = millis(); loopcount++;
if ( now - lastseq >= period2) { //change sequence
lastseq += period2;
seqVar++;
if (seqVar > 8) seqVar = 0;
//seqVar = 7; // override
period2 = random(10, 20) * 1000L; // X seconds
loopcount = 0; fastcount = 0;
}
movingDotred();
}