Arduino Code Help for LED Strip

Hello,
I am working on an escape room effect using an LED Strip and need some help with the code. Basically the effect we are trying to get is once a ground signal is sent to an Arduino pin, it will start the LED light effect. The effect we are trying to get is a burst of electricity going down the LED strip.
What we want is a certain number of LEDS will be in the burst of electricity and that is decided by the int addLED. For example, if int addLED = 20; that means once the code starts, it will start sending 20 white LED lights down the strip and then once is reached 20 it would stop sending anymore and allow that chunk of LED lights to go down the whole strip. We have this code but all it does is send a single LED light down the strip instead of a burst of LEDS that we decided in the int addLED. Can anyone help me?

Here is the code:

#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 300
int addLED = 20;
CRGB leds[NUM_LEDS];
const byte solved = 5;
bool solvedOn = 0;
boolean completed = false;

void setup() {
    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
    FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
    FastLED.clear();
    FastLED.show();

    pinMode(solved, INPUT_PULLUP);
    Serial.begin(9600);
}

void loop() {
  
bool solvedVal = digitalRead(solved);
Serial.println(solvedVal);

if (solvedVal == 0 && solvedOn == 0) {
  start_from_middle();
}
}

void start_from_middle() {
    
    for (int i=0; i<=NUM_LEDS+addLED; i++) {
   
      //white LEDS start
      leds[i] = CRGB(255, 255, 255);
// If the white LEDS past the number of addLED lights, then the rest of the LEDS will turn black so it looks like a burst of electricity. 
    if(i >= addLED) {
    //black LED starts
        leds[i-addLED] = CRGB(0, 0, 0);       
    }
        FastLED.show();
        delay(20);
        FastLED.clear();
    //Make lights go off if it is at it's last led.
    if(i = NUM_LEDS + addLED -1){
completed = true; }

 }
 }

Thank you in advanced!

Isn't that guaranteed to write outside the bounds of your array?

Thanks for your response! I found the solution. I changed my code to this and it works perfectly fine.

  
#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 300
int PulseSize = 20;
CRGB leds[NUM_LEDS];



void setup() {
    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
    FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
    FastLED.clear();
    FastLED.show();

}

void loop() {


const int PulseSize = 20;
  const CRGB PulseColor = CRGB(255, 255, 102);
  const int FrameTime = 50;  // ms, rough delay
  
  int pulsePosition = -PulseSize;  // location of the start of the pulse
  while(pulsePosition <= NUM_LEDS) {
    const int nPos = pulsePosition - 1;
    if(nPos >= 0) leds[nPos] = CRGB::Black;  // blank previous
    
    const int pos = pulsePosition + PulseSize;  // end of the pulse
    if(pos >= 0 && pos < NUM_LEDS) leds[pos] = PulseColor;

    pulsePosition++;
    delay(FrameTime);
     FastLED.show();
  }
}


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.