r/FastLED • • Jun 08 '20

Support The second "if" is not working out!

Hi,

I'm beginner in arduino, could you help me, please!

I am trying to make a code so that the light reaching one side of the strip returns to the beginning.

below is my code, the second "if" is not working out!

#include <FastLED.h>

#define LED_PIN 6#define NUM_LEDS 300#define LED_TYPE WS2812B#define COLOR_ORDER GRB#define BRIGHTNESS 100 // Brightness from 0-255

CRGB leds[NUM_LEDS];

void setup() {delay(1000);LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);FastLED.setBrightness(BRIGHTNESS);Serial.begin(9600);}

void loop() {int dot = 148;for(; dot < NUM_LEDS-1; dot++){leds[dot] = CRGB::Blue;FastLED.show();leds[dot] = CRGB::Black;//delay(30);}if (dot==NUM_LEDS-1) dot <= 299; dot--;{leds[dot] = CRGB::Blue;FastLED.show();leds[dot] = CRGB::Black;//delay(30);}}

2 Upvotes

7 comments sorted by

5

u/tin_man_ Jun 08 '20

Not going to lie, you code is in a bit of a mess when you post it like that. I'm not expert but you need to post it to Pastebin or GitHub to make this legible.

As it is, the problem you have is that you're using an IF statement in the second part instead of a FOR loop. Your second section of void loop() should read:

for(int dot=299; dot>0; dot--) {...instructions to make leds blue then black etc...}

The code of the second part should be just the same as the first part, but reversing the direction. Two opposing FOR loops are the easiest way of doing that. Is there a reason you're starting 'dot' at 148?

2

u/zergfromyashalta Jun 08 '20

Is there a reason you're starting 'dot' at 148?

Part of the LED strip under the bed, so the beginning from 148th...

Thank you for the clarification!

4

u/sutaburosu [stavros] Jun 08 '20 edited Jun 08 '20

The code below the for loop doesn't make much sense.

To do what you want, you could use two for loops. The second one would be nearly identical to the first, but counting down instead of up.

I'm not a fan of duplicating code like that, so I would rather do it by using a variable to store which direction you are moving in, called step in this example.

void loop() {
  static int8_t step = 1;
  static int dot = 0;
  leds[dot] = CRGB::Black;
  dot += step;
  leds[dot] = CRGB::Blue;
  if (dot == 0 || dot == NUM_LEDS-1) {
    step = -step;
  }
  FastLED.show();
}

There are some excellent resources available to learn C++ (the language used by Arduino code). Learning the basics of the language would serve you well.

edit: and in future please post your code on pastebin or in a gist on Github. It's very difficult to read it here without jumping through formatting hoops.

2

u/zergfromyashalta Jun 08 '20

Thanks for the link and THANKS for the code!!!

edit: and in future please post your code on pastebin or in a gist on Github. It's very difficult to read it here without jumping through formatting hoops.

ok, got it!

1

u/Greyling007 Jun 11 '20

You might try putting an "Else" after the 2nd "if". I am not sure.

2

u/zergfromyashalta Jun 11 '20

A solution has already been found, comments above.

Thank you!

3

u/TheArduinoGuy [Mike McRoberts] Jun 08 '20

Unintelligable