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.
#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
}*/
}
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++;
}
}
}