Hey, guys.
So I've been playing around and I decided to discard the idea of controlling the modules (leaves) separate because with only 9 LEDs per unit it was very limited what I could do.
Also, for some reason, the LED strip didn't like to be split up.
Instead I chose to run the strip along the edges of the units and just play with the effects and I was very satisfied at how it looks.
Now, I don't want the same effect to run all the time (I still want some control) so after MUCH trial an error I ended up coding a state machine that'd allow me to change the effect with a button (TTP223) and the code compiles and uploads but the LEDs are frozen so I added some serial print lines and in the monitor it seems to respond as expected, but the LEDs remain frozen. I cannot figure this out.
I have the main code and each effect on different tabs (it seems more organized to me that way), I don't know how to add separate tabs here so I'll just put everything into one and comment where each tab begins and ends.
#include <FastLED.h>
#include <Arduino.h>
#define NUM_LEDS 125
#define LED_PIN 3
#define BUTT_PIN 5
#define BRIGHTNESS 255
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 60
CRGBPalette16 currentPalette( CRGB::Black);
CRGBPalette16 targetPalette( PartyColors_p );
void changeEffect();
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
Serial.begin(9600);
}
void loop() {
//twinkleFox();
//fadeAnimationWrapper();
//cylon();
changeEffect();
}
void changeEffect() {
enum class effectState : uint8_t {
twinkleFox,
fadeAnimationWrapper,
cylon,
};
static effectState currEffect = effectState::twinkleFox;
static effectState prevEffect = effectState::twinkleFox;
static int buttonState = LOW;
static int prevButtonState = LOW;
buttonState = digitalRead(BUTT_PIN);
if (buttonState != prevButtonState) {
if (buttonState == HIGH) {
Serial.println("Button Pressed!");
switch (currEffect) {
case effectState::twinkleFox:
currEffect = effectState::fadeAnimationWrapper;
Serial.println("Fade Running!!");
break;
case effectState::fadeAnimationWrapper:
currEffect = effectState::cylon;
Serial.println("Cylon Running!!");
break;
case effectState::cylon:
currEffect = effectState::twinkleFox;
Serial.println("TwinkleFox Running!!");
break;
}
prevEffect = currEffect;
}
prevButtonState = buttonState;
}
}
//
// TAB 2 - CYLON EFFECT BEGINS
#include <FastLED.h>
#define CLOCK_PIN 13
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
void cylon() {
static uint8_t hue = 0;
Serial.print("x");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
}
// TAB 2 - CYLON EFFECT ENDS
// TAB 3 - TWINKLEFOX EFFECT BEGINS
void twinkleFox()
{
ChangePalettePeriodically();
uint8_t maxChanges = random8(48); // values 0-48
nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
colorIndex += 3;
}
}
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
switch(secondHand) {
case 0:
targetPalette = RainbowColors_p;
break;
case 10:
targetPalette = CRGBPalette16( CHSV(HUE_GREEN, 255, 255), CHSV(HUE_PURPLE, 255, 255), CRGB::Black, CRGB::Black, CHSV(HUE_PURPLE, 255, 255), CHSV(HUE_GREEN, 255, 255), CRGB::Black, CRGB::Black, CHSV(HUE_PURPLE, 255, 255), CHSV(HUE_GREEN, 255, 255), CRGB::Black, CRGB::Black, CHSV(HUE_PURPLE, 255, 255), CHSV(HUE_GREEN, 255, 255), CRGB::Black, CRGB::Black );
break;
case 20:
targetPalette = CRGBPalette16( CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black, CRGB::White );
break;
case 30:
targetPalette = LavaColors_p;
break;
case 40:
targetPalette = CloudColors_p;
break;
case 50:
targetPalette = PartyColors_p;
break;
}
}
// TAB 3 - TWINKLEFOX EFFECT ENDS
// TAB 4 - FADE ANIMATION EFFECT BEGINS
void fadeAnimationWrapper() {
fadeAnimation(random(256), random(256), random(256));
}
void fadeAnimation(int red, int green, int blue) {
float r, g, b;
uint32_t startTime = millis();
uint32_t totalTime = 1000; // total time for fade in and out, in milliseconds
// Define array of durations for fade in and fade out
uint32_t fadeInDurations[] = {600, 800, 1000, 1200, 1600, 2000, 2500};
uint32_t fadeOutDurations[] = {600, 800, 1000, 1200, 1600, 2000, 2500};
int numFadeInDurations = sizeof(fadeInDurations) / sizeof(fadeInDurations[0]);
int numFadeOutDurations = sizeof(fadeOutDurations) / sizeof(fadeOutDurations[0]);
// Randomly select duration for fade in and fade out
uint32_t fadeInTime = fadeInDurations[random(numFadeInDurations)];
uint32_t fadeOutTime = fadeOutDurations[random(numFadeOutDurations)];
// FADE IN
while (millis() - startTime <= fadeInTime) {
float progress = (float)(millis() - startTime) / (float)fadeInTime;
r = progress * red;
g = progress * green;
b = progress * blue;
fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
FastLED.show();
}
// FADE OUT
startTime = millis(); // reset start time for fade out
while (millis() - startTime <= fadeOutTime) {
float progress = 1.0 - ((float)(millis() - startTime) / (float)fadeOutTime);
r = progress * red;
g = progress * green;
b = progress * blue;
fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
FastLED.show();
}
}
// TAB 4 - FADE ANIMATION EFFECT ENDS
//