Variable changing values (I'm Stumped)

Problem solved (missing brackets), Thanks for the input and help

Hi,
I have a program which the variable is changing when it should not change. I am guessing that I am overwriting it in memory. Any help or ideas will be appreciated.

The complete code is listed at the bottom of this post.

Here is the piece which is causing problems:else
else
Serial.print("Distance before = ");
Serial.println(distance);

if (distance >= startdistance) {
for (int i = 0; i <= NUM_LEDS; i++) {
leds = CRGB(0, 0, 0);

  • }*
  • Serial.print("Distance After= ");*
  • Serial.println(distance);*
    // Serial.println(" Out Of Range");
  • FastLED.show();*
  • delay(50);*
    }
    distance is an int variable that currently is assigned a value.
    Here is the output from the Serial.print
    Distance before = 548 (before entering the if and for loop)
    Distance After= 0 (after the if and for loop)
    Changing the values of the leds *= CRGB(0, 0, 0); line changes the value of distance. *
    Complete Code listing:
    /*
    * Garage Parking Sensor - Published By Bob Torrence edited by Me
    */
    #include <NewPing.h>
    #include <FastLED.h>
    // defining the pins
    //#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
    #define LED_PIN 3
    #define NUM_LEDS 22
    #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    const int trigPin = 12;
    const int echoPin = 13;
    NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    // defining variables
    CRGB leds[NUM_LEDS];
    int distance;
    int stopdistance=48; //parking position from sensor (CENTIMETERS)
    int startdistance=150; //distance from sensor to begin scan as car pulls in(CENTIMETERS)
    //int increment=((startdistance-stopdistance)/15);
    int increment=10;
    void setup() {
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
    Serial.begin(9600); // Starts the serial communication
    distance = stopdistance + increment *50;
    }
    void loop() {
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    // Serial.print("Ping: ");
    // Serial.print(sonar.ping_in()); // Send ping, get distance in cm and print result (0 = outside set distance range)
    // Serial.println(" Inches");
    distance = stopdistance + increment *50;
    delay(2000);
    //distance = sonar.ping_in();
    if (distance <= stopdistance){ //Red
    * for (int i = 0; i <= NUM_LEDS; i++) {
    _ leds = CRGB ( 255, 0, 0);
    FastLED.show();
    delay(50);
    }
    Serial.print("stopdistance = ");
    Serial.println(stopdistance);
    Serial.print(distance);
    Serial.println(" Stop");*_

}
else
if ((distance >= stopdistance+increment2) && (distance < stopdistance+increment3)) {
* for (int i = NUM_LEDS/2-1 ;i <= NUM_LEDS/2+1; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("2nd Stop");
}
else_

_if ((distance >= stopdistance+increment3) && (distance < stopdistance+increment4)){_
for (int i = NUM_LEDS/2-2 ;i <= NUM_LEDS/2+2; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("3rd Stop");
}
else_

_if (distance >= (stopdistance+increment4) && (distance < stopdistance+increment5)){_
for (int i = NUM_LEDS/2-3 ;i <= NUM_LEDS/2+3; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("4th Stop");
}
else_

_if (distance >= (stopdistance+increment5)&& (distance < stopdistance+increment6)){_
for (int i = NUM_LEDS/2-4 ;i <= NUM_LEDS/2+4; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("5th Stop");
}
else_

_if (distance <= (stopdistance+increment6)&& (distance < stopdistance+increment7)){_
for (int i = NUM_LEDS/2-5 ;i <= NUM_LEDS/2+5; i++) {*

leds = CRGB ( 255, 255, 0); }

* FastLED.show();*
* delay(50);*
* Serial.println("6th Stop");*
}
else
if (distance <= (stopdistance+increment7)&& (distance < stopdistance+increment8)){
* for (int i = NUM_LEDS/2-6 ;i <= NUM_LEDS/2+6; i++) {
_ leds = CRGB ( 255, 255, 0);
}*_

* FastLED.show();*
* delay(50);*
* Serial.println("7th Stop");*
}
else
if (distance <= (stopdistance+increment8)&& (distance < stopdistance+increment9)){
* for (int i = NUM_LEDS/2-7 ;i <= NUM_LEDS/2+7; i++) {
leds = CRGB ( 255, 255, 0);
_ }*_

* FastLED.show();*
* delay(50);*
* Serial.println("8th Stop");*
}
else
if (distance <= (stopdistance+increment9)&& (distance < stopdistance+increment10)){
* for (int i = NUM_LEDS/2-8 ;i <= NUM_LEDS/2+8; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("9th Stop");
}
else_

_if (distance <= (stopdistance+increment10)&& (distance < stopdistance+increment11)){_
for (int i = NUM_LEDS/2-9 ;i <= NUM_LEDS/2+9; i++) {
_ leds = CRGB ( 255, 255, 0);
}
FastLED.show();
delay(50);
Serial.println("10th Stop");
}
else _
_if (distance <= (stopdistance+increment11)&& (distance < stopdistance+increment12)){_

for (int i = NUM_LEDS/2-10 ;i <= NUM_LEDS/2+10; i++) {
_ leds = CRGB ( 255, 255,0);
}
FastLED.show();
delay(50);
Serial.println("11th Stop");
}
else_

_if (distance >= (stopdistance) && (distance <= stopdistance + increment13)){ // all green
* for (int i = NUM_LEDS/2-11 ;i <= NUM_LEDS/2+11; i++) {*
leds = CRGB(255, 255, 0);
* }
Serial.println("12th Stop");
FastLED.show();
delay(50);
}
else
Serial.print("Distance before = ");
Serial.println(distance);
if (distance >= startdistance) { _
for (int i = 0; i <= NUM_LEDS; i++) {

_ leds = CRGB(0, 0, 0);
}
Serial.print("Distance After= ");
Serial.println(distance);
// Serial.println(" Out Of Range");
FastLED.show();
delay(50);
}
}*_

Please post your code using code tags according to the following link:

Read this before posting a programming question ...

When you don't use code tags characters are interpreted incorrectly and the code cannot be copied.

I'm going to guess that you are stomping memory:

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

You have 22 LEDs (NUM_LEDS) but this loop is writing 23 LEDs (0..22 inclusive.)

You probably want to change the for loop to:

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

so that you run from indices 0..21 (22 in total.)

There may be other instances of this happening in that code so you should check them all.

Problem solved (missing brackets) Sorry thanks for all help