How Do I Turn My LED's off?

I’m trying to make a project for my brothers birthday, so I need these LED’s to turn on when an object comes close but when there’s nothing close I need them to turn off. I also need a delay between these, so it turns on then something moves away but the lights stay on for five seconds then turns off unless something comes back in the way.

Can someone please help?

#include <FastLED.h>
#define microsecondsToInches
#define microsecondsToCentimeters

//Setup the variables for the HC-SR04
const int trigPin = 8;
const int echoPin = 9;
unsigned long time;

//Setup the variables for the NeoPixel Strip

// How many leds in your strip?
#define NUM_LEDS 39

// What pin is the NeoPixel’s data line connected to?
#define DATA_PIN 6

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup(){
// initialize serial communication:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:
FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);
}

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds*.nscale8(250); } }*

void loop()
{

  • long duration, distance;*
  • digitalWrite(trigPin,HIGH);*
  • delayMicroseconds(1000);*
  • digitalWrite(trigPin, LOW);*
  • duration=pulseIn(echoPin, HIGH);*
  • distance =(duration/2)/29.1;*
  • Serial.print(distance);*
  • Serial.println(“CM”);*
  • delay(10);*
    if ((distance > 10) && (distance < 450)) {
  • for (int i = NUM_LEDS-1; i <= 0; i++) {*
    _ leds = CRGB ( 1, 0, 0);_
    * FastLED.show();*
    * }}*

if (distance <= 10) {{
* for (int i = 0; i <= NUM_LEDS; i++) {
_ leds = CRGB ( 0, 255, 0);
FastLED.show();
delay(20);}*_

* }*
* for (int i = NUM_LEDS-1; i >= 0; i–) {
_ leds = CRGB ( 255, 0, 255);
FastLED.show();
delay(20); }}}*_

Start by looking at Read this before posting a programming question and revise your post following the instructions on how to post code

You are not properly indexing into your leds array. You need to use a subscript to address each element.

#include <FastLED.h>
#define microsecondsToInches
#define microsecondsToCentimeters

//Setup the variables for the HC-SR04
const int trigPin = 8;
const int echoPin = 9;
unsigned long time;

//Setup the variables for the NeoPixel Strip

// How many leds in your strip?
#define NUM_LEDS 39

// What pin is the NeoPixel's data line connected to?
#define DATA_PIN 6

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:
  FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}

void loop()
{
  long duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);


  if ((distance > 10) && (distance < 450)) {
    for (int i = NUM_LEDS - 1; i >= 0; i--) {
      leds[i] = CRGB( 1, 0, 0);
      FastLED.show();
    }
  }

  if (distance <= 10) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB ( 0, 255, 0);
      FastLED.show();
      delay(20);
    }

    for (int i = NUM_LEDS - 1; i >= 0; i--) {
      leds[i] = CRGB ( 255, 0, 255);
      FastLED.show();
      delay(20);
    }
  }
}

If you want them to stay on for a while after you have detected motion, you will have to add some code to note the time when you detect something and compare that to how much time has elapsed. Study the BlinkWithoutDelay example in the IDE (File->Examples->02.Digital->BlinkWithoutDelay)

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

blh64:
You are not properly indexing into your leds array. You need to use a subscript to address each element.

You’re a proper blessing dude, thanks so much it works!