Need assistance running my code backwards

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:

#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();
 
}
}

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"?

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.

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:

If you want them to move IN one step:

  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
  }

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

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

2 Likes

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

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:

  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:

  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);
  }

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