Link to home
Start Free TrialLog in
Avatar of Zeke Rich
Zeke RichFlag for United States of America

asked on

Change Direction of Lights using C

I have a set of lights ws2812 tha are addressible using the FASTLED library on a Arduino writen in C .

The code below makes this animation:  ○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙○◙ --------->  on a LED strip

The lights are working great in its current direction. I need the lights to travel now in the opposite direction.  Thank you.

#include <FastLED.h>

// Define the array of leds
#define LED_DT 3
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 38
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];


void setLEDs (int K, int N) {
  FastLED.clear();
for (int i = 0; i < NUM_LEDS; i++) {
    //( int i = NUM_LEDS-1; i >=0; i–-)  //  tried this but it didnt work
    if (i % N == K) leds[i].setRGB (0, 0, 250); // set red LED
  }
  FastLED.show();
}

void setup() {
  delay (1000);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
}

void loop() {
  int N = 5;
  for  (int i = 0; i < N; i++) {
    setLEDs (i, N);
    delay (150);
  }
}

Open in new window

Avatar of Michael Elliott
Michael Elliott
Flag of United States of America image

if (i % N == K) leds[i].setRGB (0, 0, 250);

Open in new window


Couldn't you just do?
if (i % N == K) leds[NUM_LEDS - i].setRGB (0, 0, 250);

Open in new window


So if i == 0 you would set the RGB of LED Num 38 instead of 0?
That way it'd go 38 -> 0 instead of 0 -> 38, right?
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zeke Rich

ASKER

WOW This is incredible!!! Thank you so much!!!  This solved it!!!!

if (i % N == K) leds[NUM_LEDS - 1 - i].setRGB (0, 0, 250);

Open in new window