Summary: fadeToBlackBy() is working for some LEDs but not for others. I am confussed.
Ok, the project here is an art project for a local Burn event. The strip has two modes, controlled by a tilt switch and I’d like each mode to fade out smoothly, for which I am using fadeToBlackBy(). The outer sets of LEDs will be a light and those fade just fine, as seen in the video. The inner row of LEDs will be a sign and that isn’t fading. Those LEDs just go black immediately. I’m using very similar code for each and I am failing to spot an error. So assistance requested, please.
Full code below, but in updateSign(), this gives the desired fade:
fadeToBlackBy( &(leds[START_LEDS_LIGHT1]), NUM_LEDS_LIGHT1, 10);
fadeToBlackBy( &(leds[START_LEDS_LIGHT2]), NUM_LEDS_LIGHT2, 10);
And in updateLight(), this doesn’t give a fade:
fadeToBlackBy( &(leds[START_LEDS_SIGN]), NUM_LEDS_SIGN, 10);
Full code:
// 7 Jan 2017 - Debugging sign fade-down
#include “FastLED.h”
FASTLED_USING_NAMESPACE
#if FASTLED_VERSION < 3001000
#error “Requires FastLED 3.1 or later; check github for latest code.”
#endif
#define DATA_PIN 9
#define CLK_PIN 10
#define LED_TYPE APA102
#define COLOR_ORDER BGR
#define NUM_LEDS 24
CRGB leds[NUM_LEDS];
// LEDS run:
// 0…3 LIGHT1
// 4…19 SIGN
// 20…23 LIGHT2
#define START_LEDS_LIGHT1 0 // First LED of light 1 is led[0]
#define NUM_LEDS_LIGHT1 4 // Last LED of light 1 is led[3]
#define START_LEDS_SIGN 4 // First LED of sign is led[4]
#define NUM_LEDS_SIGN 16 // So end of sign is led[19]
#define START_LEDS_LIGHT2 20 // First LED of light 2 is led[20]
#define NUM_LEDS_LIGHT2 4 // Last LED of light 2 is led[23]
#define BRIGHTNESS 32
#define FRAMES_PER_SECOND 120
// LED power control settings:
const uint8_t LEDenablePowerPin = 12;
#define POWER_PIN_ON LOW
#define POWER_PIN_OFF HIGH
bool powerOn = POWER_PIN_OFF;
// Tilt Switch settings
uint8_t tiltSwitchPin = 11;
enum switchMode { SIGN, LIGHT };
// =============================================
// Functions
// =============================================
void turnPowerOn() {
if ( powerOn == false ) {
digitalWrite(LEDenablePowerPin, POWER_PIN_ON);
powerOn = true;
Serial.println(“tPOn:\tPower was off, now on”);
} else {
//Serial.println(“tPOn:\tPower already on”);
}
}
void turnPowerOff() {
if ( powerOn == true ) {
digitalWrite(LEDenablePowerPin, POWER_PIN_OFF);
powerOn = false;
Serial.println(“tPOn:\tPower was on, now off”);
} else {
//Serial.println(“tPOn:\tPower already on”);
}
}
void updateSign() {
// Fade lights down
// This works just fine.
fadeToBlackBy( &(leds[START_LEDS_LIGHT1]), NUM_LEDS_LIGHT1, 10);
fadeToBlackBy( &(leds[START_LEDS_LIGHT2]), NUM_LEDS_LIGHT2, 10);
fill_solid( &(leds[START_LEDS_SIGN]), NUM_LEDS_SIGN, CRGB( 255, 0, 255 ) );
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
//Serial.println(“Sign should be on; lights should fade to off”);
}
void updateLight() {
// Fade sign down
// ** This doesn’t work and the LEDs for the sign go black immediately. Huh?
fadeToBlackBy( &(leds[START_LEDS_SIGN]), NUM_LEDS_SIGN, 10);
fill_solid( &(leds[START_LEDS_LIGHT1]), NUM_LEDS_LIGHT1, CRGB( 255, 255, 255 ) );
fill_solid( &(leds[START_LEDS_LIGHT2]), NUM_LEDS_LIGHT2, CRGB( 255, 255, 255 ) );
FastLED.show();
//Serial.println(“Light should be on; sign should fade to off”);
}
switchMode readTiltSwitch() {
bool tiltSwitch = digitalRead(tiltSwitchPin);
// TODO Might need to do debouncing here?
if ( tiltSwitch == HIGH ) { // HIGH for SIGN
//Serial.println(“rTS:\t setting SIGN”);
return(SIGN);
} else { // LOW for LIGHT
//Serial.println(“rTS:\t setting LIGHT”);
return(LIGHT);
}
}
// =============================================
// Setup()
// =============================================
void setup() {
Serial.begin(38400);
delay(100);
Serial.println(“Starting TTOE_Testing_Fade”);
pinMode(LEDenablePowerPin, OUTPUT);
turnPowerOff();
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
turnPowerOn();
Serial.println(“Done setup()”);
}
// =============================================
// Main loop
// =============================================
void loop() {
switch ( readTiltSwitch() ) {
case SIGN: {
updateSign();
break;
}
case LIGHT: {
updateLight();
break;
}
default: {
Serial.println("No valid mode set. This shouldn't happen.");
break;
}
}
}