What is the best way to make this FastLED animation code non-blocking?
// Code from https://github.com/s-marley/FastLED-basics/tree/main/5.%20Multiple%20patterns/classesTimer
// using a wokwi-neopixel-canvas
// https://wokwi.com/projects/356665860602531841
// See https://www.youtube.com/watch?v=fRXJQVdwrog
//
#include <FastLED.h>
#define NUM_LEDS 18
#define LED_PIN 2
CRGB leds[NUM_LEDS];
uint8_t patternCounter = 0;
bool isRunning = false;
void checkTimer();
#include "Fire2012.h"
#include "Pacifica.h"
#include "Pride2015.h"
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
Serial.begin(57600);
}
void loop() {
switch (patternCounter) {
case 0:
runFire2012();
break;
case 1:
runPacifica();
break;
case 2:
runPride2015();
break;
}
}
void checkTimer() {
EVERY_N_SECONDS(50) {
nextPattern();
}
}
void nextPattern() {
isRunning = false;
patternCounter = (patternCounter + 1) % 3;
}
void runFire2012(){
isRunning = true;
Fire2012 fire2012 = Fire2012();
while(isRunning) fire2012.runPattern();
}
void runPacifica(){
isRunning = true;
Pacifica pacifica = Pacifica();
while(isRunning) pacifica.runPattern();
}
void runPride2015(){
isRunning = true;
Pride2015 pride2015 = Pride2015();
while(isRunning) pride2015.runPattern();
}
Following the original example, I wanted the delete the objects when they weren't in use, but I also wanted them to persist through iterations of loop() so the animations would be non-blocking.
I tried the code below, but I'm not sure of the new-delete-NULL logic I use.
Any suggestions?
// Attempt at non-blocking version of FastLED classesTimer.ino example
// https://wokwi.com/projects/356666059490147329
//
// Based on sim from https://wokwi.com/projects/356665860602531841
// with code from https://github.com/s-marley/FastLED-basics/tree/main/5.%20Multiple%20patterns/classesTimer
// using a wokwi-neopixel-canvas
// Try to make non-blocking
//
// On Nano:
// A0 potentiometer: led speed 1-600 sec
// A1 potentiometer: report speed 1-1000 ms
//
// Note that the reading of the A0 report() happens frequently while the loop animations are going
#include <FastLED.h>
#define NUM_LEDS 18
const int LED_PIN = 2;
const int LED_speed_pin = A0;
const int report_speed_pin = A1;
CRGB leds[NUM_LEDS];
uint8_t patternCounter = 0;
bool isRunning = false;
bool changePattern = false;
void checkTimer();
int reportInterval = 500; // ms
int ledInterval = 1; // sec
#include "Fire2012.h"
#include "Pacifica.h"
#include "Pride2015.h"
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
Serial.begin(115200);
pinMode(LED_speed_pin,INPUT);
pinMode(report_speed_pin,INPUT);
}
void loop() {
handleIO();
switch (patternCounter) {
case 0:
runFire2012();
break;
case 1:
runPacifica();
break;
case 2:
runPride2015();
break;
}
report();
}
void report(){
static uint32_t last=0;
//const int interval=1;
if(millis() - last > reportInterval){
last+=reportInterval;
Serial.print('.');
}
}
void handleIO(void){
const int interval = 250; //ms
static uint32_t last = -interval;
if(millis() - last >= interval){
ledInterval = map(analogRead(LED_speed_pin),0,1024,1,600); // seconds
reportInterval = map(analogRead(report_speed_pin),0,1024,1,1000); // ms
}
}
void checkTimer() {
static uint32_t last = 0;
if(millis() - last >= ledInterval*1000UL){
last = millis();
nextPattern();
}
}
void nextPattern() {
isRunning = false;
changePattern = true;
//patternCounter = (patternCounter + 1) % 3;
}
void runFire2012(){
static Fire2012 *fire2012;
if(! fire2012) {
isRunning = true;
fire2012 = new Fire2012;
Serial.print("Fire2012");
}
if(changePattern){ // shut things down and transition
isRunning = false;
if(fire2012){ // de-allocate
delete fire2012;
fire2012=NULL;
Serial.print("deleted\n");
}
patternCounter = (patternCounter+1)%3;
changePattern=false; // oneshot
}
// Fire2012 fire2012 = Fire2012();
if(isRunning) fire2012->runPattern();
}
void runPacifica(){
static Pacifica *pacifica;
// if( !isRunning || ! pacifica) {
if( ! pacifica) {
isRunning = true;
pacifica = new Pacifica;
Serial.print("Pacifica");
}
if(changePattern){ // shut things down and transition
isRunning = false;
if(pacifica){
delete pacifica;
pacifica = NULL;
Serial.print("deleted\n");
}
patternCounter = (patternCounter+1)%3;
changePattern=false; // oneshot
}
if(isRunning) pacifica->runPattern();
}
void runPride2015(){
static Pride2015 *pride2015;
if( ! pride2015) {
isRunning = true;
pride2015 = new Pride2015;
Serial.print("Pride2015");
}
if(changePattern){ // shut things down and transition
isRunning = false;
if(pride2015){
delete pride2015;
pride2015 = NULL;
Serial.print("deleted\n");
}
patternCounter = (patternCounter+1)%3;
changePattern=false; // oneshot
}
if(isRunning) pride2015->runPattern();
}
There is a simulation of my attempt at
Oops, it's Valentine's day. I should come up with a good dinner plan.
