I come here with what most probably is a very basic usage question. I am still dealing with the basics…
How do I handle the following code in order to have each of the 5 pixels cycling at a different speed?
#include “FastLED.h”
#define NUM_LEDS 5
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<UCS2903, 49>(leds, NUM_LEDS);
}
void loop() {
static uint8_t hue = 0;
FastLED.showColor(CHSV(hue++, 255, 255));
delay(100);
}
Define an array with the determined speeds you’d like the LEDs to change to and an array to hold what they’re currently at.
Then make a loop that increments the current leds color and then sets that color using something like leds[i]=CHSV(currentcolor[i],255,255);
marmil
(Marc Miller)
April 2, 2016, 4:46am
3
Leo, if it’s just 5 pixels (and if I understand what you’re wanting to do) I might try out using five hue variables (hue1, hue2, etc) and also five EVERY_N_MILLISECONDS setups, one for each pixel.
I’m curious what you come up with.
so far I have this
#include “FastLED.h”
#define NUM_LEDS 5
CRGB leds[NUM_LEDS];
static uint8_t hue1 = 0;
static uint8_t hue2 = 72;
static uint8_t hue3 = 144;
static uint8_t hue4 = 216;
static uint8_t hue5 = 288;
void setup() {
FastLED.addLeds<UCS2903, 49>(leds, NUM_LEDS);
}
void loop() {
leds[0] = (CHSV(hue1++, 255, 255));
leds[1] = (CHSV(hue2++, 255, 255));
leds[2] = (CHSV(hue3++, 255, 255));
leds[3] = (CHSV(hue4++, 255, 255));
leds[4] = (CHSV(hue5++, 255, 255));
FastLED.show();
delay(100);
}
I am using “static uint8_t” because I saw it in use, not because I understand what it does
would you be so kind to guide me with “EVERY_N_MILLISECONDS”?
danke!
marmil
(Marc Miller)
April 2, 2016, 2:50pm
5
I don’t think you need to use static uint8_t, just unit8_t.
Here’s an example of using EVERY_N_MILLISECONDS:
//***************************************************************
// beat8 example. Moves pixel position based on beat8.
//
// -Marc Miller, Jan 2016
// Updated Feb 2016 - made move speed and fade rate variables.
//***************************************************************
#include "FastLED.h"
#define LED_TYPE LPD8806
#define DATA_PIN 11
#define CLOCK_PIN 13
#define NUM_LEDS 32
#define COLOR_ORDER GRB
#define BRIGHTNESS 50
CRGB leds[NUM_LEDS];
uint8_t gHue = 0; // Used to cycle through rainbow.
uint8_t moveSpeed = 6; // Higher value moves pixel faster.
uint8_t fadeRate = 80; // Use lower value to give a fading tail.
This file has been truncated. show original
Here’s another that uses EVERY_N_SECONDS, which works the same except you give it a number of seconds instead of milliseconds:
// Marc Miller, Jan 2016
#include "FastLED.h"
#define NUM_LEDS 32
//CRGB leds[NUM_LEDS]; // Not using this. Using CRGBArray instead.
CRGBArray<NUM_LEDS> leds;
CRGBSet partA(leds(6,9)); // Define custom pixel range with a name.
CRGBSet partB(leds(22,25)); // Define custom pixel range with a name.
CHSV colorOne(0,222,255); // Define a custom color.
CHSV colorTwo(82,222,255); // Define a custom color.
//---------------------------------------------------------------
void setup() {
FastLED.addLeds<LPD8806, 11, 13, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(255);
}
This file has been truncated. show original
what do you think? I have it running like this:
#include “FastLED.h”
#define NUM_LEDS 5
CRGB leds[NUM_LEDS];
uint8_t hue0 = 0;
uint8_t hue1 = 72;
uint8_t hue2 = 144;
uint8_t hue3 = 216;
uint8_t hue4 = 288;
void setup() {
delay(3000);
FastLED.addLeds<UCS2903, 49>(leds, NUM_LEDS);
}
void loop() {
led0();
led1();
led2();
led3();
led4();
FastLED.show();
}
void led0() {
EVERY_N_MILLISECONDS(10)
leds[0] = (CHSV(hue0++, 255, 255));
}
void led1() {
EVERY_N_MILLISECONDS(100)
leds[1] = (CHSV(hue1++, 255, 255));
}
void led2() {
EVERY_N_MILLISECONDS(200)
leds[2] = (CHSV(hue2++, 255, 255));
}
void led3() {
EVERY_N_MILLISECONDS(500)
leds[3] = (CHSV(hue3++, 255, 255));
}
void led4() {
EVERY_N_MILLISECONDS(1000)
leds[4] = (CHSV(hue4++, 255, 255));
}
marmil
(Marc Miller)
April 2, 2016, 3:32pm
7
I think the EVERY_N_MILLISECONDS lines need to be inside the main loop. (I think @Mark_Kriegsman said that.) Also note the use of braces { } around the code that will run every N milliseconds. But otherwise, yes, I think you got it.
ack… EVERY_N_MILLISECONDS is built into fastled to replace (help) with the currentmillis()-previousmillis()?
dammit… ive been wasting a lot of time getting my projects to run if i can just use this!
marmil
(Marc Miller)
April 9, 2016, 9:59pm
9
Ack, my brain must have slipped the other day. Obviously using EVERY_N* works fine outside of the main loop and in a subroutine, as I did that in my first example provided above. (It must have been something else I remember Mark saying needed to be inside the main loop.) Did you get it all working @Leo_Bettinelli ? I would still suggest the use of the braces though, so for your example:
void led0() {
EVERY_N_MILLISECONDS(10) { // <–don’t forget these braces
leds[0] = (CHSV(hue0++, 255, 255));
} // <–every_n closing brace
}
@Kelvin_Mead , yes the addition of EVERY_N* to FastLED is wonderful. It definitely simplifies the code, and also makes it much more readable.
ey marc!
it obviously work outside of the main loop because it was already working for me when you mentioned that it needs to be inside of the loop.
I take your advice on braces!
super thank you!