Go Down

Topic: Sequential LEDs, come and go (Read 229 times) previous topic - next topic

nozor

Guys, I'm having trouble creating an infinite back and forth LED sequencing. Like the Knight Rider, from the 1980s.  :)
My goal is to turn on the LED, turn off and turn on the next ... However, when the delay (500) or FastLED.clear, from the first For, it turns off the last led, instead of just changing the color of the last LED, to return.
It was to be expected that he had this behavior on the first LED as well. After all there is delay (500), there is Fast.LED.clear at the end of the routine, in addition to the void loop () itself, at the beginning, but no, it just changes the color, the way I need it. Very strange.
------------------ the skatch------------------
#include <FastLED.h>
#define LED_PIN     6
#define NUM_LEDS    6
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); }

void loop() {
  for (int i = 0; i <= 6; i++) {
    leds = CRGB ( 255, 255, 0);
    FastLED.show();
    delay(500);
    FastLED.clear(); }
  
  for (int i = 6; i >= 0; i--) {
    leds = CRGB ( 255, 255, 255);
    FastLED.show();
    delay(500);
    FastLED.clear(); }
}


Tank You for the opportunity

uxomm

Always use code tags when posting code please. So that it looks like this:
Code: [Select]
#include <FastLED.h>
#define LED_PIN     6
#define NUM_LEDS    6
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); }

void loop() {
  for (int i = 0; i <= 6; i++) {
    leds[i] = CRGB ( 255, 255, 0);
    FastLED.show();
    delay(500);
    FastLED.clear(); }
 
  for (int i = 6; i >= 0; i--) {
    leds[i] = CRGB ( 255, 255, 255);
    FastLED.show();
    delay(500);
    FastLED.clear(); }
}



I am not sure that I can understand what you mean :)

But what I understand is that the arrays in the code run out of bounds.
Take a look here: https://www.arduino.cc/reference/en/language/variables/data-types/array/
An array like leds[6]
starts at leds[0]
and ends at leds[5]

In the code you write to leds[6]. Not a good idea :)

Maybe this is what you want:
Code: [Select]
#include <FastLED.h>
#define LED_PIN     6
#define NUM_LEDS    6

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
  for (int i = 0; i < 6; i++) {       // line changed
    leds[i] = CRGB ( 255, 255, 0);
    FastLED.show();
    delay(500);
    FastLED.clear();
  }

  for (int i = 5; i >= 0; i--) {      // line changed
    leds[i] = CRGB ( 255, 255, 255);
    FastLED.show();
    delay(500);
    FastLED.clear();
  }
}
Always decouple electronic circuitry.

Go Up