Yet another blink-without-delay question

Hi

I'm trying to get my head around the BWOD.

I understand the example and have coded and built it without a problem, but I'm trying to implement it as below, but I'm obviously missing something as the timing is non existent, where it should be changing every 500ms

#include "FastLED.h"

#define NUM_LEDS 21

#define DATA_PIN 10
long onTime = 500;
const long offTime = 500;
unsigned long previousMillis = 0;
int numLed = 0;

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= offTime) {
    previousMillis = currentMillis;
  }
  Serial.println(numLed);
  // First slide the led in one direction
  if (numLed <= NUM_LEDS) {
    // Set the i'th led to red
    leds[numLed] = CRGB::Red;
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    leds[numLed] = CRGB::Black;
    FastLED.show();
    numLed++;

  }

  else {
    numLed = 0;
  }




  // Now go in the other direction.
  /*for(int i = NUM_LEDS-1; i >= 0; i--) {
  	// Set the i'th led to red
  	leds[i] = CRGB::Red;
  	// Show the leds
  	FastLED.show();
  	// now that we've shown the leds, reset the i'th led to black
  	leds[i] = CRGB::Black;
  	// Wait a little bit before we loop around and do it again
  }*/
}

The last bit is commented out until I have worked out the rest.

Any help much appreciated

Look again at the first condition in "loop()" and the braces

Thanks AWOL

Couldn't of been any more obvious!

Right, I've re-coded and cleaned it up a bit, but I'm still getting the same problem, although I guess something else is causing it.

Zero timings. All I get is a full white strip

I presume its in the IF ELSE, but I just can't see it

#include "FastLED.h"

#define NUM_LEDS 21

#define DATA_PIN 10
long onTime = 500;
const long offTime = 500;
unsigned long previousMillis = 0;
int numLed = 0;

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);

  for (int q = 0; q < NUM_LEDS; q++) {
    leds[q] = CRGB::Red;
  }
}

void loop() {


  unsigned long currentMillis = millis();

  for (int i = 0; i < NUM_LEDS; i++) {

    Serial.println(i);

    if (currentMillis - previousMillis >= offTime) {
      previousMillis = currentMillis;
      // Set the i'th led to red
      leds[i] = CRGB::White;
      FastLED.show();
      leds[i] = CRGB::Red;
    }
    else {
      leds[i] = CRGB::Red;
      FastLED.show();
    }
  }

  for (int i = NUM_LEDS - 1; i >= 0; i--) {

    Serial.println(i);

    if (currentMillis - previousMillis >= offTime) {
      previousMillis = currentMillis;
      // Set the i'th led to red
      leds[i] = CRGB::White;
      FastLED.show();
      leds[i] = CRGB::Red;

    }
    else {

      leds[i] = CRGB::Red;
      FastLED.show();
    }
  }
}

Where did the for loops come from?
They weren't in your original code(there's a reason for that)

Ok, so I've gone back to my original code.

I've cross reference the BWOD example, and I think its something to do with the IF ELSE.

I've tried different ways, and googled a bit, but I'm still missing something

I need another pointer

I can't see your code.

Oops

#include "FastLED.h"

#define NUM_LEDS 21

#define DATA_PIN 10
long onTime = 500;
const long offTime = 500;
unsigned long previousMillis = 0;
int numLed = 0;

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= offTime) {
    previousMillis = currentMillis;
    Serial.println(numLed);
    // First slide the led in one direction
    if (numLed <= NUM_LEDS) {
      // Set the i'th led to red
      leds[numLed] = CRGB::Red;
      // Show the leds
      FastLED.show();
      // now that we've shown the leds, reset the i'th led to black
      leds[numLed] = CRGB::Black;
      FastLED.show();
      numLed = numLed + 1;
    }


  }

  else {

// This is the bit I think is wrong. Somehing missing or incomplete
    numLed = 0;
  }




  // Now go in the other direction.
  /*for(int i = NUM_LEDS-1; i >= 0; i--) {
    // Set the i'th led to red
    leds[i] = CRGB::Red;
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    leds[i] = CRGB::Black;
    // Wait a little bit before we loop around and do it again
  }*/
}

Are you sure your else relates to the correct if?

Got it, thanks again

Now progressing, I've added the code to reverse the direction of the LED's, but, when watching the count in the serial monitor, I get from 0-20, then straight to 255.

If I check the numLed variable exceeds the NUM_LEDS (20 in this case) with another IF ELSE, how is it counting 0 to 20, then straight to 255?

Reverse code is commented out until I find the issue.

#include "FastLED.h"

#define NUM_LEDS 20

#define DATA_PIN 10
long onTime = 30;
const long offTime = 30;
unsigned long previousMillis = 0;
int numLed = 0;

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= offTime) {
    previousMillis = currentMillis;
    Serial.println(numLed);

    // First slide the led in one direction
    if (numLed <= NUM_LEDS) {
      leds[numLed] = CRGB::Red;
      // Show the leds
      FastLED.show();
      delay(onTime);
      leds[numLed] = CRGB::Black;
      FastLED.show();
      if (numLed >= NUM_LEDS) {
        numLed == 0;
      }
      else {
        numLed++;
      }
    }
  }
        numLed == 0;

should be

        numLed = 0;

I think it's not so clever to test for >= first and increment then

  if (numLed >= NUM_LEDS) {
    numLed = 0;
  }
  else {
    numLed++;
  }

I would suggest incrementing and testing the incremented value

  if (++numLed >= NUM_LEDS) {
    numLed = 0;
  }

Ok

Tweaked it, and now runs 0-21 no problem.

Now I've added the reverse to count from 21-0, but serial now shows 0-21, then -2, -1, 0, 1........21

Why!?!?

#include "FastLED.h"

#define NUM_LEDS 21

#define DATA_PIN 10
long onTime = 250;
const long offTime = 30;
unsigned long previousMillis = 0;
int numLed = 0;

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
  for (int q = 0; q < NUM_LEDS; q++ ) {
    leds[q] = CRGB::Red;
    // Show the leds
    FastLED.show();
  }
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= offTime) {
    previousMillis = currentMillis;
    Serial.println(numLed);

    // First slide the led in one direction
    if (numLed < NUM_LEDS) {
      leds[numLed] = CRGB::White;
      // Show the leds
      FastLED.show();
      delay(onTime);
      leds[numLed] = CRGB::Red;
      FastLED.show();
      numLed++;
    }

    else {

      // Reverse direction
      if (numLed >= NUM_LEDS) {
        numLed = NUM_LEDS;
        leds[numLed] = CRGB::White;
        // Show the leds
        FastLED.show();
        delay(onTime);
        leds[numLed] = CRGB::Red;
        FastLED.show();
        numLed = numLed - 1;
      }
    }
  }
}