Go Down

Topic: Fading command not working ? (Read 754 times) previous topic - next topic

Shanedm1

I changed the way the LED is expressed with individual colours and the math has been changed.

I've looked and looked at it, but i can't see whats wrong tho...

Suggestions?

Code: [Select]
void loop() {
// Fade Down to Black
for (int dot = 0; dot<NUM_LEDS; dot++) {
  leds[dot] = CRGB(255, 255, 0);
  FastLED.show();
  for(int fadeNum = 0; fadeNum<120; fadeNum++) {
  leds[dot].fadeToBlackBy(5);
  FastLED.show();
  delay(90);
  }
  delay(400);
  // Fade From Black to Full Colour
     leds[dot].red = 255;
     leds[dot].green = 255;
     leds[dot].blue = 0;

  for(int fadeUp = 0; fadeUp<100; fadeUp++) {
    leds[dot].red = (leds[dot].red * fadeUp)/100;
    leds[dot].green = (leds[dot].green * fadeUp)/100;
    leds[dot].blue = (leds[dot].blue * fadeUp)/100;
   
    FastLED.show();
    delay(90);
  }
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(170);
}
}

Shanedm1

ahhh after a little longer I figured it out !

Shanedm1

I solved what I had wanted to do!

A huge thank you guys !!

Shanedm1

So the fade in and out loop that I have been working on works great!

But when I put it into the main program, It turns off all the previous LEDs that were just turned on. Why is that? I had wanted to keep those LEDs turned on instead of to black.

There must be something that is turning them off, but why?


Here is the code that I've been working on that works great!

Code: [Select]
void eatFade() { 
//loop to control position
for (int dot = 0; dot<NUM_LEDS; dot +=2) {
// Fade From Black to Full Colour
     leds[dot].red = 255;
     leds[dot].green = 255;
     leds[dot].blue = 0;
  for(int fadeUp = 0; fadeUp<130; fadeUp++) {
    leds[dot].red = (200 * fadeUp)/130;
    leds[dot].green = (200 * fadeUp)/130;
    leds[dot].blue = (0 * fadeUp)/130;
    FastLED.show();
    delay(170);
  }
  delay(300);
  leds[dot] = CRGB(255, 255, 0);
  FastLED.show();
  for(int fadeNum = 0; fadeNum<120; fadeNum++) {
  leds[dot].fadeToBlackBy(5);
  FastLED.show();
  delay(90);
  }
  delay(400);
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(170);
}
}




MAIN PROGRAM:

Code: [Select]

#include <Adafruit_NeoPixel.h>
#include <FastLED.h>

#define DATA_PIN 6
#define LAMPS 30
#define NUM_LEDS 30
CRGB leds [NUM_LEDS];

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LAMPS, DATA_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN> (leds, NUM_LEDS);
  strip.begin();
  strip.setBrightness(80);    // overall brightness can be 0 to 255
  strip.show();               // Initialize all pixels to 'off'
}

void loop() {
  // white food pixels
  setFood();
  delay(1800);

  //pacman eats the food with Fade
  eatFade();
  delay(1800);

  // pacman is chased by ghosts
  chasedByGhosts();
  delay(1800);

  // now pacman is chasing the ghosts
  chaseGhosts();
  delay(1800);
}


void setFood() {
  // fade in
  for(int br=0; br<170; br++) {
    uint32_t white = strip.Color(br, br, br);
    for(int i=0; i<strip.numPixels(); i+=4) {
        strip.setPixelColor(i, white);
       delay(25);
    }
    strip.show();
    delay(5);
  }
}

void chasedByGhosts() {
  uint32_t pacman = strip.Color(255, 255, 0);
  uint32_t clyde = strip.Color(249, 154, 0);
  uint32_t blinky = strip.Color(244, 7, 8);
  uint32_t pinky = strip.Color(249, 161, 166);
  uint32_t inky = strip.Color(48, 218, 219);

  // run back
  for(int i=strip.numPixels(); i>-30; i--) {
      // check: color, position, space between pacman and ghost
      checkLed(pacman, i, 0);

      checkLed(clyde, i, 10);
      checkLed(blinky, i, 16);
      checkLed(pinky, i, 22);
      checkLed(inky, i, 28);

      strip.show();
      delay(600);
  }
}

void chaseGhosts() {
  uint32_t pacman = strip.Color(255, 255, 0);
  uint32_t scaredghost = strip.Color(0, 0, 246);

  // run ghost, run!
  int maximum = strip.numPixels() + 5;
  for(int j = -30; j<maximum; j++) {
      checkLed(scaredghost, j, 28);
      checkLed(scaredghost, j, 22);
      checkLed(scaredghost, j, 16);
      checkLed(scaredghost, j, 10);
      checkLed(pacman, j, 0);
      strip.show();
      delay(600);
  }
}

// check if this position is within the led strip, if yes then show the color
void checkLed(uint32_t color, int i, int offset) {
  int pos = i + offset;

  if(pos < strip.numPixels() && pos > -1){
     strip.setPixelColor(pos, color);
  }
  // switch off the pixel before and after this one (depends on direction)
  if(pos+1 < strip.numPixels() && pos+1 > -1){
     strip.setPixelColor(pos+1, strip.Color(0, 0, 0));
  }
  if(pos-1 < strip.numPixels() && pos-1 > -1){
    strip.setPixelColor(pos-1, strip.Color(0, 0, 0));
  }
}

void eatFade() { 
//loop to control position
for (int dot = 0; dot<NUM_LEDS; dot +=2) {
// Fade From Black to Full Colour
     leds[dot].red = 255;
     leds[dot].green = 255;
     leds[dot].blue = 0;
  for(int fadeUp = 0; fadeUp<130; fadeUp++) {
    leds[dot].red = (200 * fadeUp)/130;
    leds[dot].green = (200 * fadeUp)/130;
    leds[dot].blue = (0 * fadeUp)/130;
    FastLED.show();
    delay(170);
  }
  delay(300);
  leds[dot] = CRGB(255, 255, 0);
  FastLED.show();
  for(int fadeNum = 0; fadeNum<120; fadeNum++) {
  leds[dot].fadeToBlackBy(5);
  FastLED.show();
  delay(90);
  }
  delay(400);
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(170);
}
}

Grumpy_Mike

Quote
But when I put it into the main program, It turns off all the previous LEDs that were just turned on. Why is that?
Because that is what your eatFade function does. It first fades up to a full brightness, which is odd because you already set that. It then fades down to black and finally sets the LEDs to black. So when you come out of that function every other LED will be black. Every other because the loop index changes by 2 each iteration.

Shanedm1

#35
Aug 20, 2019, 06:26 pm Last Edit: Aug 20, 2019, 06:27 pm by Shanedm1
In the previous function called "setFood()" I have LEDs that are lit up followed by my fade function. I'd want those LEDs that are in set food to remain lit. I have LEDs that are lit coming out of "setFood" and wanted them to remain lit while my fade function does its thing.
So I'm turning them all off in my fade function ?

Grumpy_Mike

#36
Aug 21, 2019, 06:59 am Last Edit: Aug 21, 2019, 07:02 am by Grumpy_Mike
Quote
So I'm turning them all off in my fade function ?
Yes this bit turns off the LEDs.

Code: [Select]
leds[dot]= CRGB::Black;
It is at the end of your loop, so every LED that is addressed by a value of dot is turned off by this function.

Code: [Select]
for (int dot = 0; dot<NUM_LEDS; dot +=2) {
So you will turn off LEDs number 0, 2, 4, 6, 8 ...... and so on for the whole strip.

You need to get more practice in reading what you write and try and understand the whole picture.

Shanedm1

I think I figured out why the first set of LED's were actually turning off.

The first loop I called out was using commands that are in the  AdaFruit library. Then I have my fade loop which uses the FastLED library. That's when everything went dark. I reprogrammed things with just the FastLED commands and now it's working fine.
I have to spend a bit more time programming this weekend.

Also- a HUGE thank you Grumpy_Mike for helping me ! And to the others too !!!!
Thanks Guys !

Go Up