Variable keep resetting to 0 every loop

Hello, i'm new to arduino. I want to make a running led for my bike tail

#include "Arduino.h"
#include <FastLED.h>

#define LED_PIN 2 //LED Strip Signal Connection 
#define BrakeSignal 8 //Brake Signal Connection
#define LeftSignal 6 //Left Blinker Signal Connection
#define RightSignal 7 //Right Blinker Signal Connection

#define NUM_LEDS 74 //Total no of LEDs in the LED strip
#define BlinkerLEDs 13 //LEDs for Left/Right Blinker 
int l,j;
int bright = 30;
byte *c;
uint16_t a, b = 0;
int r = NUM_LEDS;
//int BlinkerSpeed = 5; //Blinker Running LED Speed. Adjust this to match with your Bike blinker speed.
int BlinkerOffDelay = 300; //Blinker Off time. Adjust this to match with your Bike blinker speed.
unsigned long tAwal;
unsigned long tLsein;
unsigned long tRsein;
unsigned long tSekarang;

CRGB leds[NUM_LEDS];


void setup() 
{
  Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(BrakeSignal, INPUT);
pinMode(LeftSignal, INPUT);
pinMode(RightSignal, INPUT);
  for (int i = 0; i < (NUM_LEDS/2); i++)
  {
    leds[i] = CRGB(60, 0, 0);
    leds[i-1] = CRGB(0, 0, 0);
    leds[(NUM_LEDS-1)-i] = CRGB(60, 0, 0);
    leds[(NUM_LEDS)-i] = CRGB(0, 0, 0);
    FastLED.show();
    delay (50);    
  }
  
  for (int j = ((NUM_LEDS/2)-1); j >= 0; j--)
  {
    leds[j] = CRGB(60, 0, 0);
    leds[(NUM_LEDS/2-1)+((NUM_LEDS/2)-j)] = CRGB(60, 0, 0);
    FastLED.show();
    delay (50);    
  }

  for (int i = 0; i < 5; i++)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    {
    leds[i] = CRGB(0, 0, 0);
    }
    FastLED.show();
    delay (75);
    for (int i = 0; i < NUM_LEDS; i++)
    {
    leds[i] = CRGB(60, 0, 0);
    }
    FastLED.show();
    delay (50);
  }
  LeftDim();
  RightDim();
}

void loop() 
{
  tSekarang = millis();
  if(digitalRead(LeftSignal)==1){
    if (tLsein == 0){
      tLsein = millis();
    }else{
      if (tSekarang - tLsein >= 50UL){
        if (l <= (BlinkerLEDs-1)){
          leds[l] = CRGB(255, 150, 0);
          l++;
          tLsein = tSekarang;
        }else{
          for (int j = 0; j <= (BlinkerLEDs-1); j++){
            leds[j] = CRGB(0, 0, 0);
          }
          l = 0;
        }
        FastLED.show();
        FastLED.setBrightness(bright);
      }
    }
  }

  if(digitalRead(LeftSignal)==0){
    tLsein = 0;
    for (int j = 0; j <= (BlinkerLEDs-1); j++){
      leds[j] = CRGB(0, 0, 0);
    }
    FastLED.show();
    l=0;
  }

  if(digitalRead(RightSignal)==1){
    if (tRsein == 0){
      tRsein = millis();
    }else{
      if (tSekarang - tRsein >= 50UL){
        if (r >= (NUM_LEDS - BlinkerLEDs)){
          leds[r] = CRGB(255, 150, 0);
          r--;
          tRsein = tSekarang;
        }else{
          for (int j = NUM_LEDS; j >= (NUM_LEDS - BlinkerLEDs); j--){
            leds[j] = CRGB(0, 0, 0);
          }
          r = NUM_LEDS;
        }
        FastLED.show();
        FastLED.setBrightness(bright);
      }
    }
  }
  
  if(digitalRead(RightSignal)==0){
    tRsein = 0;
    for (int j = NUM_LEDS; j >= (NUM_LEDS - BlinkerLEDs); j--){
        leds[j] = CRGB(0, 0, 0);
    }
    FastLED.show();
    r = NUM_LEDS;
  }

  if (tSekarang - tAwal >= 15UL){
      if (digitalRead(BrakeSignal)==1){
      for (int i = BlinkerLEDs; i < (NUM_LEDS - BlinkerLEDs); i++){
        leds[i] = CRGB(255, 0, 0);
      }
      FastLED.show();
      FastLED.setBrightness(bright);  
    }else{
      for(a=NUM_LEDS/2; a > BlinkerLEDs; a--) {
        c = Wheel(((a * 256 / (NUM_LEDS/2)) + b) & 255);
        leds[(NUM_LEDS) - a].setRGB(*c, *(c+1), *(c+2));
        leds[(NUM_LEDS - 1) - (NUM_LEDS - a)].setRGB(*c, *(c+1), *(c+2));
      }
      FastLED.show();
      FastLED.setBrightness(bright);
      tAwal = tSekarang;
      b++;
    }
  }

}

void rainbowCycle(int Delay) {
  byte *c;
  uint16_t i, j;

  for(j=0; j < 256; j++) {
    if (tSekarang - tAwal >= Delay){
      for(i=NUM_LEDS/2; i > BlinkerLEDs; i--) {
        c = Wheel(((i * 256 / (NUM_LEDS/2)) + j) & 255);
        leds[(NUM_LEDS) - i].setRGB(*c, *(c+1), *(c+2));
        leds[(NUM_LEDS - 1) - (NUM_LEDS - i)].setRGB(*c, *(c+1), *(c+2));
      }
      FastLED.show();
      tAwal = millis();
    }
  }
}

byte *Wheel(byte WheelPosition) {
  static byte c[3];
 
  if(WheelPosition < 85) {
   c[0] = WheelPosition * 3;
   c[1] = 255 - WheelPosition * 3;
   c[2] = 0;
  }
  else if(WheelPosition < 170) {
   WheelPosition -= 85;
   c[0] = 255 - WheelPosition * 3;
   c[1] = 0;
   c[2] = WheelPosition * 3;
  }
  else {
   WheelPosition -= 170;
   c[0] = 0;
   c[1] = WheelPosition * 3;
   c[2] = 255 - WheelPosition * 3;
  }

  return c;
}

void LeftDim()
{
  for (int i = 0; i < BlinkerLEDs; i++)
  {
    leds[i] = CRGB(0, 0, 0);
  }
    FastLED.show();
}

void RightDim()
{
   for (int i = (NUM_LEDS - BlinkerLEDs); i < NUM_LEDS; i++)
  {
    leds[i] = CRGB(0, 0, 0);
  }
    FastLED.show();
}

but everytime it loops, the variable l on LeftSignal always become 0. But when I remove the Rigthsignal function, It run smoothly. I cant understand what happen

Please Help

LeftSignal does not appear to be a variable at all, but looks like it should be a constant. There is nothing in the code you posted that alters LeftSignal.

However you need to post all your code so we can see what it is and how and where it is defined.

There is no RightSignal function.
It looks like RightSignal is a pin number.

i've edited my post, could you please help, LeftSignal is an input

RightSignal and LeftSignal are input, yes

Try posting your whole code. Do the following, in order:
In the IDE, press ctrl-t.
Then press ctrl-r, to ensure it compiles. If all is well,
press ctrl-shift-C, then go back to your browser,
open a new message in this topic, and press ctrl-V.
That will give us the whole picture. Do not re-edit your first post, it just confuses the issue.
Thank you. No further help from me will be forthcoming until you do this. It's your choice.

'''
#define LeftSignal 6 //Left Blinker Signal Connection
'''

That is a define, every time the compiler sees LeftSignal it replaces it by a 6.

There is no way that this can be altered in the loop function or by any other piece of code, unless you redefine it, which you are not.

So the problem is not what you thought.
What made you think this was being changed to zero?

CRGB leds[NUM_LEDS];
          for (int j = NUM_LEDS; j >= (NUM_LEDS - BlinkerLEDs); j--) {
            leds[j] = CRGB(0, 0, 0);
          }

You are writing past the end of the array, when j is equal to NUM_LEDS.

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