Hi,
I feel like I'm going round in circles here and can't figure this out.
I am trying to rewrite this original code to work with the FastLED library (working fine) and without delay().
This is my code:
// candle for Adafruit NeoPixel
// 1 pixel version
// by Tim Bartlett, December 2013
// current settings for 5v Trinket
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 1
#define BRIGHTNESS 100 // set at 25 to run on 5v (powersaving)
struct CRGB leds[NUM_LEDS];
byte onLedNumber = 0;
unsigned long currentTime;
unsigned long loopTime;
unsigned long onCurrentTime;
unsigned long onLoopTime;
// color variables: mix RGB (0-255) for desired yellow
int redPx = 255;
int grnHigh = 100; //110-120 for 5v, 135 for 3.3v
int bluePx = 10; //10 for 5v, 15 for 3.3v
// animation time variables, with recommendations
int burnDepth = 14; //10 for 5v, 14 for 3.3v -- how much green dips below grnHigh for normal burn -
int flutterDepth = 30; //25 for 5v, 30 for 3.3v -- maximum dip for flutter
int cycleTime = 120; //120 -- duration of one dip in microseconds
// pay no attention to that man behind the curtain
int fDelay;
int fRep;
int flickerDepth;
int burnDelay;
int burnLow;
int flickDelay;
int flickLow;
int flutDelay;
int flutLow;
void flutter(int f);
void flicker(int f);
void burn(int f);
void fullOn(int f);
void fire(int grnLow);
void setup() {
FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
flickerDepth = (burnDepth + flutterDepth) / 2.4;
burnLow = grnHigh - burnDepth;
burnDelay = (cycleTime / 2) / burnDepth;
flickLow = grnHigh - flickerDepth;
flickDelay = (cycleTime / 2) / flickerDepth;
flutLow = grnHigh - flutterDepth;
flutDelay = ((cycleTime / 2) / flutterDepth);
Serial.begin(9600);
onCurrentTime = millis();
onLoopTime = onCurrentTime;
currentTime = millis();
loopTime = currentTime;
}
// In loop, call CANDLE STATES, with duration in seconds
// 1. fullOn() = solid yellow
// 2. burn() = candle is burning normally, flickering slightly
// 3. flicker() = candle flickers noticably
// 4. flutter() = the candle needs air!
void loop() {
// Serial.println("Burn");
// burn(10);
// Serial.println("Flicker");
// flicker(5);
// Serial.println("Burn");
// burn(8);
Serial.println("Flutter");
flutter(6);
// Serial.println("Burn");
// burn(3);
Serial.println("On");
fullOn(10);
Serial.println("Burn");
burn(10);
// Serial.println("Flicker");
// flicker(10);
}
// basic fire funciton - not called in main loop
void fire(int grnLow) {
for (int grnPx = grnHigh; grnPx > grnLow; grnPx--) {
currentTime = millis();
if (currentTime >= (loopTime + fDelay)) {
leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
FastLED.show();
loopTime = currentTime; // Updates loopTime
}
}
for (int grnPx = grnLow; grnPx < grnHigh; grnPx++) {
currentTime = millis();
if (currentTime >= (loopTime + fDelay)) {
leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
FastLED.show();
loopTime = currentTime; // Updates loopTime
}
}
}
// fire animation
void fullOn(int f) {
fRep = f * 1000;
onCurrentTime = millis();
if (onCurrentTime >= loopTime + fRep) {
// int grnPx = grnHigh - 5;
// leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
leds[onLedNumber] = CRGB::Red;
FastLED.show();
onLoopTime = onCurrentTime; // Updates loopTime
}
}
void burn(int f) {
fRep = f * 8;
fDelay = burnDelay;
for (int var = 0; var < fRep; var++) {
fire(burnLow);
}
}
void flicker(int f) {
fRep = f * 8;
fDelay = burnDelay;
fire(burnLow);
fDelay = flickDelay;
for (int var = 0; var < fRep; var++) {
fire(flickLow);
}
fDelay = burnDelay;
fire(burnLow);
fire(burnLow);
fire(burnLow);
}
void flutter(int f) {
fRep = f * 8;
fDelay = burnDelay;
fire(burnLow);
fDelay = flickDelay;
fire(flickLow);
fDelay = flutDelay;
for (int var = 0; var < fRep; var++) {
fire(flutLow);
}
fDelay = flickDelay;
fire(flickLow);
fire(flickLow);
fDelay = burnDelay;
fire(burnLow);
fire(burnLow);
}
I have stuff commented out and Serial.prints to try and isolate the problem but it isn't helping.
The problem is that that this section seems to just get skipped and the LED does not light.
// fire animation
void fullOn(int f) {
fRep = f * 1000;
onCurrentTime = millis();
if (onCurrentTime >= loopTime + fRep) {
// int grnPx = grnHigh - 5;
// leds[onLedNumber] = CRGB(redPx, grnPx, bluePx);
leds[onLedNumber] = CRGB::Red;
FastLED.show();
onLoopTime = onCurrentTime; // Updates loopTime
}
}
Does anyone have any ideas? I'm going crazy!