How do I get my LED's to turn off?

I'm very new to arduino but I'm making a project for my brother where I have an ultrasonic sound sensor trigger a series of RGB LED lights and it turns on fine, but will not turn off, can anybody tell me what modifications I need to make? Thank you
</

#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) {

  • for (int i = 0; i <= 39; i++) {*
    _ leds = CRGB ( 0, 255, 0);_
    * FastLED.show();*
    * delay(20);*
    * }*
    * for (int i = 39; i >= 0; i--) {*
    _ leds = CRGB ( 255, 0, 255);
    * FastLED.show();
    delay(20);
    }*_

if (distance > 10) {
* for (int i = 0; i <= 0; i++) {*
_ leds = CRGB ( 0, 0, 0);
* FastLED.show();
delay(20); }}}}
/>
Basketball.ino (1.33 KB)*_

Hum

for (int i = 0; i <= 0; i++)

Post your code inside </> so that people can see it without downloading, you are more likely to get a reply.
Put print statements in your code so that you can see what value the variable 'distance' takes.

larryd:
Hum

for (int i = 0; i <= 0; i++)

That was just after I was annoyed and started playing around

Well of course, we should have known that :wink: .

You need to fix the for statement that @larryd spotted.
pulseln() returns an unsigned long.
What values of 'distance' do you see?

#define NUM_LEDS 39
. . .
for (int i = 0; i <= 39; i++)
. . .
for (int i = 39; i >= 0; i--)

How many LEDs :wink:

larryd:
#define NUM_LEDS 39
. . .
for (int i = 0; i <= 39; i++)
. . .
for (int i = 39; i >= 0; i--)

How many LEDs :wink:

In case that wasn't obvious, 39 LEDs means an index from 0 to 38 if you include the equals sign. Typically, you don't include the equals sign. In other words, you are running past the end of your array so all bets are off

for (int i = 0; i < 39; i++) 
. . .
for (int i = 38; i >= 0; i--)

It is also common practice to assign a constant value to a variable that represents the size so you can only change it in one place and everything continues to work

#define NUM_LEDS 39
...
for (int i = 0; i < NUM_LEDS; i++) 
. . .
for (int i = NUM_LEDS-1; i >= 0; i--)