phptuts / fastled-animator

Create simple rgb led light animations without code.
https://www.fastledanimator.com/
GNU General Public License v3.0
24 stars 4 forks source link

pattern fix better code generated #20

Closed phptuts closed 1 year ago

phptuts commented 1 year ago

Purpose

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
fastled-animator-111v ✅ Ready (Inspect) Visit Preview Oct 16, 2022 at 1:01AM (UTC)
phptuts commented 1 year ago

@marmilicious can you let me know what you think about these changes?

marmilicious commented 1 year ago

I like the direction this is going with this new frame storage format.

// We declare these large values here for memory reasons
    byte reds[] = { 0,0,255,255,...
    byte greens[] = { 0,0,123,123,36,....
    byte blues[] = { 0,0,0,0,202,....

I also wondered about storing as hex. Maybe an interesting idea for you here:
https://www.reddit.com/r/FastLED/comments/sb1ogp/

Another thought-- What about bundling this even a bit more in some fashion, with a pattern name. And locate it outside of the main loop. My thought is that ultimately this way there could be multiple patterns that could be called to run (cycled through). Even if you didn't implement that, at least it would make it easy to copy a pattern into another program.

And please consider this-- One of the things we often try to discourage people from doing is using delay to set the timing of their pattern. Using delay() is often where many of us start out, but if we can provide an alternative that doesn't use delay() then we will be better off down the line. Mostly because if you're using delay() you can't do other stuff like read sensors or buttons, or have more then one thing happening at a time. We like to encourage people to use millis timers or the FastLED EVERYN* timers.

void loop() {
  EVERY_N_MILLISECONDS(500) {
    load next frame of pattern
  }
  FastLED.show()
}
phptuts commented 1 year ago

@marmilicious That's great feedback! Thank you!

I going to not use delay and I am going to bundle everything up as you suggested.

Thank you

phptuts commented 1 year ago

@marmilicious

Here is what the new pattern code looks like. The spacing looks right on the website. I think this is ready to merge.

#include <FastLED.h>
#define NUM_LEDS 12
#define DATA_PIN A0
#define BOUNCE_SWITCH 10
#define MAX_FRAMES 19

CRGB leds[NUM_LEDS];
int frameIndex = 0;

void shiftRight() {
    CRGB tempColor = leds[NUM_LEDS - 1];
    for(int ledIndex = NUM_LEDS - 1; ledIndex > 0; ledIndex -= 1) {
        leds[ledIndex].setRGB(leds[ledIndex - 1].r, leds[ledIndex - 1].g, leds[ledIndex - 1].b);
    }
    leds[0] = tempColor; 
}

void shiftLeft() {
    CRGB tempColor = leds[0];
    for(int ledIndex = 0; ledIndex < NUM_LEDS - 1; ledIndex += 1) {
        leds[ledIndex].setRGB(leds[ledIndex + 1].r, leds[ledIndex + 1].g, leds[ledIndex + 1].b);
    }
    leds[NUM_LEDS - 1] = tempColor; 
}

int nextFrameIndex() {
  if (frameIndex < MAX_FRAMES) {
      return frameIndex + 1;
    } else {
      return 0;
    }
}

void bounceRight() {
  if (frameIndex < BOUNCE_SWITCH) {
      shiftRight();
  } else {
      shiftLeft();
  }
  frameIndex = nextFrameIndex(); 
}

void bounceLeft() {
  if (frameIndex < BOUNCE_SWITCH) {
      shiftLeft();
  } else {
      shiftRight();
  }
  frameIndex = nextFrameIndex(); 
}

void setup() {
    FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
    FastLED.setBrightness(10);
    FastLED.clear();
    // Frame 1
    leds[0].setRGB(236, 95, 232);
    leds[1].setRGB(236, 95, 232);
    leds[2].setRGB(0, 0, 0);
    leds[3].setRGB(0, 0, 0);
    leds[4].setRGB(0, 0, 0);
    leds[5].setRGB(0, 0, 0);
    leds[6].setRGB(0, 0, 0);
    leds[7].setRGB(0, 0, 0);
    leds[8].setRGB(0, 0, 0);
    leds[9].setRGB(0, 0, 0);
    leds[10].setRGB(0, 0, 0);
    leds[11].setRGB(0, 0, 0);

}

void loop() {
    EVERY_N_MILLISECONDS(200) {
        bounceRight();
    }
    FastLED.show();
}

This is what the new nonpattern code looks like. It's not perfect, but I think it's a lot better. Especially without delay.

#include <FastLED.h>
#define NUM_LEDS 12
#define DATA_PIN A0
#define MAX_FRAMES 14

CRGB leds[NUM_LEDS];
int frameIndex = 0;

int nextFrameIndex() {
  if (frameIndex < MAX_FRAMES - 1) {
      return frameIndex + 1;
    } else {
      return 0;
    }
}

void setup() {
    FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
    FastLED.setBrightness(10);
    // Frame 1
    leds[0].setRGB(236, 95, 232);
    leds[1].setRGB(236, 95, 232);
    leds[2].setRGB(236, 95, 232);
    leds[3].setRGB(236, 95, 232);
    leds[4].setRGB(236, 95, 232);
    leds[5].setRGB(236, 95, 232);
    leds[6].setRGB(236, 95, 232);
    leds[7].setRGB(236, 95, 232);
    leds[8].setRGB(236, 95, 232);
    leds[9].setRGB(236, 95, 232);
    leds[10].setRGB(236, 95, 232);
    leds[11].setRGB(236, 95, 232);

}

void loop() {
    EVERY_N_MILLISECONDS(200) {
        frameIndex = nextFrameIndex();
        setColors();
    }
    FastLED.show();
}

void setColors() {
    switch (frameIndex) {
        case 0:
            leds[0].setRGB(236, 95, 232);
            leds[1].setRGB(236, 95, 232);
            leds[2].setRGB(236, 95, 232);
            leds[3].setRGB(236, 95, 232);
            leds[4].setRGB(236, 95, 232);
            leds[5].setRGB(236, 95, 232);
            leds[6].setRGB(236, 95, 232);
            leds[7].setRGB(236, 95, 232);
            leds[8].setRGB(236, 95, 232);
            leds[9].setRGB(236, 95, 232);
            leds[10].setRGB(236, 95, 232);
            leds[11].setRGB(236, 95, 232);
            break;
        case 1:
            leds[0].setRGB(0, 0, 0);
            leds[1].setRGB(170, 0, 0);
            leds[2].setRGB(170, 0, 0);
            leds[3].setRGB(170, 0, 0);
            leds[4].setRGB(170, 0, 0);
            leds[5].setRGB(170, 0, 0);
            leds[6].setRGB(0, 0, 0);
            leds[7].setRGB(0, 0, 0);
            leds[8].setRGB(0, 0, 0);
            leds[9].setRGB(0, 0, 0);
            leds[10].setRGB(0, 0, 0);
            leds[11].setRGB(0, 0, 0);
            break;
        case 2:
            leds[1].setRGB(0, 0, 0);
            leds[6].setRGB(170, 0, 0);
            break;
        case 3:
            leds[2].setRGB(0, 0, 0);
            leds[7].setRGB(170, 0, 0);
            break;
        case 4:
            leds[3].setRGB(0, 0, 0);
            leds[8].setRGB(170, 0, 0);
            break;
        case 5:
            leds[4].setRGB(0, 0, 0);
            leds[9].setRGB(170, 0, 0);
            break;
        case 6:
            leds[5].setRGB(0, 0, 0);
            leds[10].setRGB(170, 0, 0);
            break;
        case 7:
            leds[6].setRGB(0, 0, 0);
            leds[11].setRGB(170, 0, 0);
            break;
        case 8:
            leds[6].setRGB(170, 0, 0);
            leds[11].setRGB(0, 0, 0);
            break;
        case 9:
            leds[5].setRGB(170, 0, 0);
            leds[10].setRGB(0, 0, 0);
            break;
        case 10:
            leds[4].setRGB(170, 0, 0);
            leds[9].setRGB(0, 0, 0);
            break;
        case 11:
            leds[3].setRGB(170, 0, 0);
            leds[8].setRGB(0, 0, 0);
            break;
        case 12:
            leds[2].setRGB(170, 0, 0);
            leds[7].setRGB(0, 0, 0);
            break;
        case 13:
            leds[1].setRGB(170, 0, 0);
            leds[6].setRGB(0, 0, 0);
            break;

    }
}