LED strip up and down

hey,

I want to make a led train that goes up and down. now I think I am very close, but there is still one bug that I can’t get out of it.

my Led train goes from begin to end, but instead of going right back to begin of the strip it starts at the begin of the strip and goes a few leds down and than goes in reverse from and to back.

I have tried several things and non of them gave the result I want

I hope someone can help my out

led_train.ino (1.51 KB)

Please post your code with code tags instead of attaching it..

I think you're sending the front of your train off the end of the strip before you put it into reverse.

FastLed doesn’t do an error check for writing beyond the size of the Array, that means you have to !
instead you limit the writing of the size of the index using the ‘%’ to the preferably within the function that does the writing to the array. that would work if you would use the same counter direction, but instead substract the index from the (NUM_LEDS-8) actually having the loop run for the whole length of the strip (150) can be correct but then you have to make sure the functions you call can not write beyond the size of the array.
Something like this would work (though your train doesn’t actually turn around) the filling of the train is not done very elegantly i do apologize. It actually should be done using a loop and an array for the colors per led (which would make it easier to turn around) but i think you’ll catch my drift.

#include <FastLED.h>

// Define the array of leds
#define LED_DT 52
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];

int index1;
int index2;
int i;
unsigned long initialtime;


//colors for the led trains
void set4leds(int pos) {
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (200, 0, 200);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (0, 0, 255);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (0, 255, 0);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (255, 0, 0);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (200, 0, 200);
  pos++;
  if (pos>=NUM_LEDS) return;  
  leds[pos].setRGB (0, 0, 255);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (0, 255, 0);
  pos++;
  if (pos>=NUM_LEDS) return;
  leds[pos].setRGB (255, 0, 0);
}


void setup() {
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
  index1 = 0;
  index2 = 0;

  initialtime = millis();
}

void loop() {

  for (i = 0; i <NUM_LEDS; i++) {//for loop to get the led  train from begin to end 

    FastLED.clear();    
    index1 = i;
    set4leds(index1)
    delay(40);
    FastLED.show();
  }

  for ( i = 0; i<NUM_LEDS;; i++) {//for loop to get the led from and to begin 
    FastLED.clear();
    set4leds(index1);
    index1 = NUM_LEDS-1-i;
    delay(40);
    FastLED.show();
  }
}

Also i can see you are planning to use millis() for your timing this is definitely the way to go.

thank you so much!!! I never had tought about that! really nice!

thanks for the help, I have learned some new things :)