Help programming LED strip custom functions

void TurnSignal() {
  for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[i] = CRGB::Red;
    FastLED.show();
    leds[i] = CRGB::Red;
    delay(50);
  }
}

Why do you have the Second leds[ i ] = CRGB::Red; ? That memory is already Red.

You should clear you strip in the setup so that you know when entering the loop all are black, then don't need to do the [rr]FastLED.show();[/tt] as the first instruction in the loop. Your loop then would be call TurnSignal, show the LEDs, pause a smal bit, clear the LEDs, pause a bit.

Oh yeah,

Good point! Thanks very much:

#include "FastLED.h"

#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_LEDS_PER_STRIP];


void setup() { 

  
  FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS_PER_STRIP);
  FastLED.show();
}

void TurnSignal() {
  for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    // set our current dot to red
    leds[i] = CRGB::Red;
    FastLED.show();
    delay(40);
  }
}

void loop() {
 
  TurnSignal();
  FastLED.clear();
  delay(2000);
  
  
}

What should I add in the setup to achieve a blank strip? I'll have a look at some more examples too

Thanks again,

Kyle

Not sure why you have a show in your setup. You know how to use clear, and you're still asking how to clear in setup.
Remember that setup is stuff that only runs once.

Thanks,

I tried to add FastLED.clear(); in the setup instead of show but it also did nothing to turn off the first led before my loop ran.

Not sure what I can do next.

Kyle

Frankly, the first LED is on because it's probably broken if clear doesn't turn it off.
Only thing left in the code to maybe fiddle with is where you select addleds neopixels when there's a dedicated option for WS2812B from Fastled.

Hi,

Just to check--try adding FastLED.show() immediately after FastLED.clear().

best,
Michael

Thanks, I'll replace neopixels with the actual led type.

I'm no expert obviously but would I still have control over the colour of the first led if it were broken?

The code with the delay before the sweep works too and the delay is negligible when you press the button.

I've included my code into a button setup too:

#include "FastLED.h"

#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_LEDS_PER_STRIP];

const int buttonPin = 2;    
int buttonState = 0;         

void setup() { 
  
  pinMode(buttonPin, INPUT); 
  FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS_PER_STRIP);
  FastLED.clear();
}

void TurnSignal() {
  for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    // set our current dot to red
    leds[i] = CRGB::Yellow;
    FastLED.show();
    delay(40);
  }
}

void loop() {

  
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    
    FastLED.showColor(CRGB::Black);
    delay(200);
    TurnSignal();
    FastLED.clear();
    
  } else {
    FastLED.showColor(CRGB::Black); 
  } 
}

Many thanks,

Kyle

I actually think the problem may be using addleds Neopixels instead of addleds WS2812B. There's an extra thingy defining RGB order for the WS. Go back to the Fastled sample code and find that correct addleds line

Thanks Guys,

Michael,

Adding FastLED.show() immediately after FastLED.clear() doesn't seem to change the outcome either.

I also changed from NEOPIXEL to WS2812B but no joy:

#include "FastLED.h"

#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_LEDS_PER_STRIP];

const int buttonPin = 2;    
int buttonState = 0;         

void setup() { 
  
  pinMode(buttonPin, INPUT); 
  FastLED.addLeds<WS2812B, 6>(leds, NUM_LEDS_PER_STRIP);
  
  FastLED.clear();
  FastLED.show();
  
}

void TurnSignal() {
  for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    // set our current dot to red
    leds[i] = CRGB::Yellow;
    FastLED.show();
    delay(40);
  }
}

void loop() {

  
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    
    TurnSignal();
    FastLED.clear();
    delay(200);
    
  } else {
    FastLED.showColor(CRGB::Black); 
  } 
}

Here's the result so far:

I'm happy to add the delay before the sweep since it's negligible but I'm all ears if there's any other thoughts.

Thanks again,

Kyle

This is what I see in the sample

FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

Thanks,

I've changed a couple of things to emulate the example I've seen that in:

#include "FastLED.h"


#define NUM_LEDS 15
#define DATA_PIN 6

CRGB leds[NUM_LEDS];

const int buttonPin = 2;    
int buttonState = 0;         

void setup() { 

  
  
  pinMode(buttonPin, INPUT); 
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  
  FastLED.clear();
  FastLED.show();
  
}

void TurnSignal() {
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to red
    leds[i] = CRGB::Yellow;
    FastLED.show();
    delay(30);
  }
}

void loop() {

  
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    TurnSignal();
    FastLED.clear();
    delay(200);
    
  } else {
    FastLED.showColor(CRGB::Black); 
  } 
}

How does this look? it's doing the same thing unfortunately.

Kyle

Should clarify something- what kind of LEDs do you actually have in that strip?
WS2812, WS2812B, or something else? Take a close up picture of an LED if you're unsure.

I have WS2812B's, and they actually use GRB order instead of RGB.
My code uses:
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

And can you get a vid of your first LED staying on? I'm thinking you only think it's on because the cycle starts right after a clear and you don't see it. When I put a delay in the right spot, it shows that the first actually does turn off.

Use this. Stay consistent with which code you're using, and if it calls it NUM_LEDS or NUM_LEDS_PER_STRIP. It's whatever is defined at the very beginning of your code. No reason to use the long winded name.

void TurnSignal() {

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(250, 100, 0);      //A better shade of yellow. Don't use predefined color words. JMHO.
    FastLED.show();
    delay(40);
  }                                                          //^^Turns on LEDs
  
  delay(1200);                                 //Keeps them on for this long
  FastLED.clear();
  FastLED.show();                           //Turns them all off
  delay(600);                                  //Keeps them off for this long
}

Thanks very much for your time, I appreciate it,

That's worked a treat and as you said, you need to put the delay in just the right place to solve it. It didn't occur to me to delay how long they were on:

#include "FastLED.h"


#define NUM_LEDS 15
#define DATA_PIN 6

CRGB leds[NUM_LEDS];

const int buttonPin = 2;    
int buttonState = 0;         

void setup() { 

  
  pinMode(buttonPin, INPUT); 
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  
}

void TurnSignal() {
  
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(100, 250, 0);
    FastLED.show();
    delay(30);
  }

  delay(100);                                
  FastLED.clear();
  FastLED.show();                           
  delay(100);                                  
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    TurnSignal();
   
  } else {
    
    FastLED.showColor(CRGB::Black); 
  } 
}

This is looking very clean now!

The next step is to add 2 other buttons for the other 2 functions. This 'sweep' is for a turn signal and the other functions will be a brake light and running lights. I'll start with the brake light I think but it will be working on the same strip (I'll have 10 strips stacked on top of each other to make a 15x10 pixel matrix) and the turn signal will look as if it's passing over the brake light when pressed.

I suppose the trick will be tracking where the turnsignal is along it's sequence so the red brake leds can be activated at any time without 'resetting' the turnsignal.

Any thoughts for this?

Thanks again!

Kyle

So I've been using this DemoReel100 often, its a great sketch. But I need some help trying to figure out how to pass an additional variable to the function.

I have 2 different LED strips of different lengths attached to 2 different PINS. I want to be able to call a function and send to it the strip number I want it applied to. Basically what I want to do is be able to call the function, let's say rainbow(), and pass it a variable that tells it which LED strip to use. Without using this SimplePatternList array of pointers, it would look like:

rainbow(1);

Somehow, I would like to incorporate the rainbow(1) into the SimplePatternList because this is such an elegant and easy way to control what you're sending to the LEDs.

Hope this makes sense.

J-M-L:

typedef void (*SimplePatternList[])();

defines SimplePatternList as a new type of array of pointers to functions

SimplePatternList gPatterns = { sinelon, };

Creates gPatterns as such an array and initializes it with only one element (thus at position 0) which is the pointer to the function sinelon() that you have somewhere else in your code