# Need assistance running my code backwards

**URL:** <https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024>\
**Category:** General Guidance\
**Created:** [July 28, 2022, 1:03pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024 "2022-07-28T13:03:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![theralfzer0](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@theralfzer0](https://forum.arduino.cc/u/theralfzer0)\
**Post date:** [July 28, 2022, 1:03pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/1 "2022-07-28T13:03:29Z")

</div>

Good afternoon everybody

As ya'll can guess I am new to Arduino and coding, withsome help with friends we created this code  
What it is is basically Music reactive LED lights, running from Centre outwards:

I want to change the direction to run outward to Centre but we cannot figure it out, we thought using opposite symbols like changing all the "+" to "-" and "\>" to "\<" would work, but alas, no is doesn't work that way

anny suggestion would be very helpfull

The Code:

```cpp
#include <FastLED.h>

int r=0;
int g=0;
int b=0;

#define LED_PIN 8
#define NUM_LEDS 100// Enter Number of LEDS
#define BRIGHTNESS 0
#define LED_MID_L 50
#define LED_MID_R 50
uint8_t hue = 0;

CRGB leds[NUM_LEDS];
CRGB led[NUM_LEDS];
int s=0;
int k=0;

void setup() {

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  for (int i =50; i >= 0; i--) 
  {
leds[i] = CRGB ( r,g,b);
leds[100-i] = CRGB (r,g,b );
     delay(0);
    FastLED.show();
  }

   pinMode(A0,INPUT);
   pinMode(A1,INPUT);

}
void loop(){

//Treble();
Bass();
}

void Bass(){
{
  s=analogRead(A0);
   
   if((s>=536)&&(s<=540))//High Freq 4
     {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(144);
    delay(50);
        
  }
      else if((s>=541)&&(s<=545)) //Max Freq 5
   {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255); 
    FastLED.setBrightness(180);
    delay(75);
    
      }
      else if((s>=546)&&(s<=550)) //Max Freq* 6
   {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(216);
    delay(100);
        
      }
      else if((s>=551)&&(s<=700)) //Max Freq** 7
   {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(255);
    delay (125);
        
   }
   
  else if((s>=531)&&(s<=535)) //Mid Freq 3
   { 
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(108);
    delay(30);
        
  }
    else if((s>=526)&&(s<=530)) //Low Freq 2
   {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(72);
    delay(20);
        
   }
    else if((s>=495)&&(s<=525)) //Very Low Freq 1
   {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(36);
    delay(0);
    fadeToBlackBy( leds, NUM_LEDS, 1);

    

   }
  else
   {
     leds[LED_MID_L] = CRGB ( r,g,b);
     leds[LED_MID_R] = CRGB ( r,g,b);
     
  }

    for (int i = 0; i <= 50; i++) 
  {
     leds[i] = leds[i+1];
     leds[100-i] = leds[99-i];
  }

  EVERY_N_MILLISECONDS(65){
    hue++;
     
}
 FastLED.show();
 
}
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 28, 2022, 1:22pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/2 "2022-07-28T13:22:27Z")

</div>

You have 100 LED's in your strip. Which end is "Centre" and which end is Outside? What do "LED\_MID\_L" and "LED\_MID\_R" (both 50) represent?

Why have "led" and "leds" when you seem to only use "leds"?

---

<div class="post-metadata">

**Author:** ![theralfzer0](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@theralfzer0](https://forum.arduino.cc/u/theralfzer0)\
**Post date:** [July 28, 2022, 1:36pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/3 "2022-07-28T13:36:35Z")

</div>

Okay so i have two strips of 50 LEDS each  
"LED\_MID\_L" is the Left LED strip  
"LEF\_MID\_R" is the Right LED strip respectively

And for some reason unknown to me without the "leds" the code does not work.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 28, 2022, 1:53pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/4 "2022-07-28T13:53:20Z")

</div>

> [@theralfzer0](#):
>
> And for some reason unknown to me without the "leds" the code does not work.

Becue that's the one you use. If you comment out `CRGB led[NUM_LEDS];` the sketch still compiles without error. If your sketch doesn't work without it, that probably means you are running off the end of the 'leds' array and altering other variables that you DO need.

This appear to be the place where colors are moved 'out' one step:

> [@theralfzer0](#):
>
> ```arduino
> for (int i = 0; i <= 50; i++) 
> {
> leds[i] = leds[i+1];
> leds[100-i] = leds[99-i];
> }
> 
> ```

If you want them to move IN one step:

```arduino
  for (int i = 49; i > 0; i--) 
  {
     leds[i] = leds[i-1]; // 48->49, 47->48, 46->47...0->1
     leds[98-i] = leds[99-i]; // 50->49, 51->50... 99->98
  }

```

---

<div class="post-metadata">

**Author:** ![theralfzer0](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@theralfzer0](https://forum.arduino.cc/u/theralfzer0)\
**Post date:** [July 28, 2022, 2:05pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/5 "2022-07-28T14:05:36Z")

</div>

I will change it as suggested and report back, but i do believe that it is the solution. Thanks a million

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 28, 2022, 2:13pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/6 "2022-07-28T14:13:03Z")

</div>

To run code backwards, you need to use the "comefrom" statement, everywhere you would have used a "goto".

---

<div class="post-metadata">

**Author:** ![theralfzer0](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@theralfzer0](https://forum.arduino.cc/u/theralfzer0)\
**Post date:** [July 28, 2022, 2:53pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/8 "2022-07-28T14:53:41Z")

</div>

Unfortunatly it does not work all it does is switches on the first LED of each strip

I'm startingm to think that it needs an entirely different code to work the other way around

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 28, 2022, 7:25pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/9 "2022-07-28T19:25:29Z")

</div>

> [@theralfzer0](#):
>
> Unfortunatly it does not work all it does is switches on the first LED of each strip

It looks like you are setting leds[50] (twice) and then spreading the leads out from the center. If you want to spread the colors in from the ends you will probably want to set leds[0] and less[99] (the ends).

For example:

```arduino
  if((s>=536)&&(s<=540))//High Freq 4
  {
    leds[LED_MID_L]=CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[LED_MID_R]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(144);
    delay(50);
  }

```

becomes:

```arduino
  if((s>=536)&&(s<=540)) //High Freq 4
  {
    leds[0] =CHSV(hue + (LED_MID_L * 10), 255, 255); //RED
    leds[99]=CHSV(hue + (LED_MID_R * 10), 255, 255);
    FastLED.setBrightness(144);
    delay(50);
  }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 24, 2023, 7:25pm UTC](https://forum.arduino.cc/t/need-assistance-running-my-code-backwards/1017024/10 "2023-01-24T19:25:46Z")

</div>

Closed on January 24, 2023, 7:25pm UTC

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