Hey, guys! Newbie here; been working my way through one of the starter kits, but as soon as I attempted my first solo project...stumped.
Here's what I'm trying to do:
I have a strip of WS2811 LED's (50), and I'd like to be able to select pairs to blink together, randomly, in a color of my choice.
I can download other people's random effect codes all day long, but I can't seem to satisfactorily edit that code to do anything more meaningful than change the color of the LED's.
I'm a quick learner, but this is a stumbling block I can't seem to get around. I throw myself at the mercy of the community; talk slow and please be kind.
JordanG:
This is probably a great resource, but I'm honestly too unfamiliar with what any of that code means to make any meaningful changes to it.
Do you (or anyone on here) know of a step-by-step how-to for doing what I need to?
Thanks!!!
JG
So let's go through this step-by-step.
1.) the referenced post has code displayed which will generate a random number; code has been reduced to the nuts and bolts.
const int mn = 0, mx = 49; // 50 LEDs in strip
unsigned long myLED;
void setup(){
Serial.begin(9600);
}
void loop(){
myLED = random(mn,mx);
Serial.print("My random LED = ");
Serial.println(myLED);
delay(1000);
}
2.) The same post has a link to a web page, with a link to a library (which can also be downloaded from within the arduino programmer). If you follow the library link to github.com, you'll eventually find the examples folder.
3.) Pairing step #1 with step #2 (and additional custom code), you are "able to select pairs to blink together, randomly, in a color of my choice."
JMeller:
So let's go through this step-by-step.
1.) the referenced post has code displayed which will generate a random number; code has been reduced to the nuts and bolts.
const int mn = 0, mx = 49; // 50 LEDs in strip
unsigned long myLED;
void setup(){
Serial.begin(9600);
}
void loop(){
myLED = random(mn,mx);
Serial.print("My random LED = ");
Serial.println(myLED);
delay(1000);
}
2.) The same post has a link to a web page, with a link to a library (which can also be downloaded from within the arduino programmer). If you follow the library link to github.com, you'll eventually find the examples [folder](https://github.com/FastLED/FastLED/blob/3.2.0/examples/Blink/Blink.ino).
3.) Pairing step #1 with step #2 (and additional custom code), you are "able to select pairs to blink together, randomly, in a color of my choice."
That's helpful, thank you. I think I need to do a LOT more study on the coding aspect of Arduino before any of this will work for me. Right now, even that stripped-down code means virtually nothing to me.
Still, I thank you all a great deal. If I manage to crack it, I'll post an update!
I've gotten my code to the point where I can decide which LEDs are lit, and in what color. The only thing I can't seem to find any reference to absolutely ANYwhere is fading those LEDs to black and back up to full brightness.
At this point, I'll even forego the need to have the fade be random; I'd just like to figure out how to fade at all!!
Where TEMPERATURE_1 is globally defined as "Tungsten100W", it can be defined as a dozen other options as noted in the example.
Where BRIGHTNESS is globally defined with a value of 128, it can have a value from 0 to 254.
After setting the values globally, you can assign the temperatures and brightness while code is running:
void loop(){
//referencing code example: https://github.com/FastLED/FastLED/blob/master/examples/ColorTemperature/ColorTemperature.ino
FastLED.setBrightness( BRIGHTNESS );
FastLED.setTemperature( TEMPERATURE_1 ); // first temperature
leds[0] = TEMPERATURE_1; // show indicator pixel
FastLED.show();
delay(3000);
FastLED.setTemperature( TEMPERATURE_2); // second temperature
leds[0] = TEMPERATURE_2; // show indicator pixel
FastLED.show();
delay(3000);
}
Now if wish to change the brightness during the running of the code, you can:
1.) assign multiple statically define brightness levels; call them when needed:
#define BRIGHTNESS_1 64
#define BRIGHTNESS_2 128
#define BRIGHTNESS_3 254
//BRIGHTNESS_1, BRIGHTNESS_2, BRIGHTNESS_3 now have statically assigned values which cannot be changed at anytime due the use of #define,
void loop(){
//referencing code example: https://github.com/FastLED/FastLED/blob/master/examples/ColorTemperature/ColorTemperature.ino
FastLED.setBrightness( BRIGHTNESS_1 );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
FastLED.setBrightness( BRIGHTNESS_2 );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
FastLED.setBrightness( BRIGHTNESS_3 );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
}
2.) or, to use brightness as a dynamic variable, create an integer called BRIGHTNESS (don't use #define).
int BRIGHTNESS;
// The value of BRIGHTNESS can be assigned any value (0-254 in this case) - at anytime during the running code.
void loop(){
//referencing code example: https://github.com/FastLED/FastLED/blob/master/examples/ColorTemperature/ColorTemperature.ino
BRIGHTNESS = 64;
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
BRIGHTNESS = 128;
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
BRIGHTNESS = 254;
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
FastLED.show();
delay(3000);
}
JMeller:
If you haven't already, peruse through all of the...
That was INCREDIBLY helpful, thank you! With your post and a BUNCH of Googling and testing, I've managed to get 99% of the way there.
I have separate pairs of LEDs on the strand slowly fading from off to high, with randomly-generated fade times and high/low times, for maximum spookiness.
The one and ONLY thing left is to separate the LED activation times...right now, all pairs go off simultaneously, and are governed by the same randomization function, meaning they fade up and down at the exact same (albeit random) times.
Here's what I've created:
#include "FastLED.h"
#define NUM_LEDS 50
#define DATA_PIN 5
int BRIGHTNESS = 254;
int fadeAmount = 1;
CRGB leds[NUM_LEDS];
void setup() {
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(3000);
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
BRIGHTNESS = BRIGHTNESS + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (BRIGHTNESS <= 0 || BRIGHTNESS >= 255) {
// the length of time it takes to begin fading again after reaching high/low
delay(random(1000, 10000));
fadeAmount = -fadeAmount;
}
{
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
FastLED.show();
//the amount of time it takes to fade (measured in ms per loop)
delay(random(0, 20));
}
{
FastLED.setBrightness( BRIGHTNESS );
leds[12] = CRGB::Red;
leds[13] = CRGB::Red;
FastLED.show();
//the amount of time it takes to fade (measured in ms per loop)
delay(random(0, 20));
}
}
I get the sense that there's a way to do this that involves splitting up the different sets of LEDs in the code, but I can't seem to figure out how.
The one and ONLY thing left is to separate the LED activation times...right now, all pairs go off simultaneously, and are governed by the same randomization function,
Ah, that is where it gets tricky.
If you mean one pair fades then after a time another pair fades, that is simple, but if you want both pairs to fade at the same time you need to go up to the next level of coding for this.
What you have to do is to implement a state machine with your code so that each step of the fade is a separate task.
If you mean one pair fades then after a time another pair fades, that is simple, but if you want both pairs to fade at the same time you need to go up to the next level of coding for this.
I MIGHT need the machine state thing, but let's try the "simple" way you mentioned first.
That is simply to do one fade at once. So repeat your code for the second pair after the first.
Better, make the fade into a function that takes in the parameters you want to use and then call this function as many times in the loop function as you want passing different variables to control what LEDs to use, fade speeds and so on.
I like the idea of creating a function, but for now, I just want something rough to get it working. The problem is, no matter what I try (vis a vis replicating code to get one pair of LEDs to fade up after another), the LEDs fade up and down simultaneously; it's maddening.
If you could have a look at my current code and tell me what you'd do, I'd appreciate it so much:
#include "FastLED.h"
#define NUM_LEDS 50
#define DATA_PIN 5
int BRIGHTNESS = 254;
int fadeAmount = 1;
CRGB leds[NUM_LEDS];
void setup() {
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(3000);
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
BRIGHTNESS = BRIGHTNESS + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (BRIGHTNESS <= 0 || BRIGHTNESS >= 255) {
// the length of time it takes to begin fading again after reaching high/low
delay(random(1000, 10000));
fadeAmount = -fadeAmount;
}
{
FastLED.setBrightness( BRIGHTNESS );
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
FastLED.show();
//the amount of time it takes to fade (measured in ms per loop)
delay(random(0, 20));
}
{
leds[12] = CRGB::Blue;
leds[13] = CRGB::Blue;
FastLED.show();
//the amount of time it takes to fade (measured in ms per loop)
delay(random(0, 20));
}
}
You are using the looping of the loop function to make the fade work. To get the two fades one after the other you have to have both fades in a loop. That is probably best done whit a while loop arround the bit that does the fading. That is from the start of the loop function until just before your referance to the second lot of LEDs.
Make a new Boolean variable called fadeCompleat and set it to false. Then the while statement just says while( !fadeCompleate){
Then in this loop set the fadeComplete variable to true when you reverse the direction of fading.
I won’t be arround for the next day and a half as I am traveling back home after my holidays so don’t think I have abandoned you.
If you have time before you go, I'm a visual learner; would it be too much trouble for you to slap together an example of the code you're talking about, so I can see what goes where and when? It's really the way I learn best.