FastLED v3.0: One more thing...

FastLED v3.0: One more thing…

Beat generators that generate sine waves (and sawtooth waves) at a BPM you specify. Try this:

#define BPM 60
#define DIMMEST 128
#define BRIGHTEST 255

void loop()
{
// Put something visible on the LEDs
static uint16_t hue16 = 0;
hue16 += 9;
fill_rainbow( leds, NUM_LEDS, hue16 / 256, 3);

// set the brightness to a sine wave that moves with a beat
uint8_t bright = beatsin8( BPM, DIMMEST, BRIGHTEST);
FastLED.setBrightness( bright );

FastLED.show();
}

Docs are coming, but check lib8tion.h for beat8, beat16, beatsin8, and beatsin16. Enjoy!

Ooh, this looks cool. I guess my plans for today have changed.

I’m trying to only show the fade-down part of the function in an easy way, however I’m not quite right on anything. Any suggestions? I would like to use the BPM only to trigger the fade-out, and not show any fade-up…

Well, let me see. How about something roughly like this:

  1. If beatsin8 is still rising, don’t start the fade out yet.
  2. Once beatsin8 starts falling, start the fade out.
  3. When beatsin8 starts rising again, the fade out is done.

So some of that code would look like:

bool beatRising() {
static uint8_t prevBeatsin8 = 0;
uint8_t thisBeatsin8 = beatsin8( BPM);
bool rising = thisBeatsin8 > prevBeatsin8;
return rising;
}

…and then…

// wait for top of wave
while( beatRising() ) { delay(1); }

// fade out
while( ! beatRising() ) {
FastLED.setBrightness( beatsin8( BPM));
FastLED.show();
}

// we’re done. Set full black just to be sure
FastLED.showColor(CRGB::Black);

Like that?

I sort of wonder if the beat functions should take an optional ‘clock value’, so you can sync them to other things.

@Mark_Kriegsman It’s amazing what some sleep will do. I tried your code structure, but it didn’t quite work out of the box. So, here is what I just came up with:

void loop() {
// set the brightness to a sine wave that moves with a beat
uint8_t bright = beatsin8( BPM, DIMMEST, BRIGHTEST);

//if we are at the bottom
if(bright == DIMMEST+1){ trigger = false; }

//if we are at the top
if(bright == BRIGHTEST-1){ trigger = true; }

//only show the fade out
if(trigger == true){
LEDS.setBrightness( bright ); }
else{
LEDS.setBrightness( 0 ); }

LEDS.show();

}

And yes, I would like to see the ‘clock source’ idea too.

One thing to beware of: you may not hit the sine beat at the exact top or bottom, depending on when it gets sampled…

Why exactly is that? If you don’t mind explaining. EDIT: Is that in reference to my code snippet or the function in general?

Well, imagine the worst case: you only call beatsin8 once per second, and it HAPPENS to be just when the return value is 128. Every time. The real world isn’t so extreme but the same idea applies.

In other words, because the value is always changing whether you sample it or not, you might accidentally miss certain values. Solutions are: sample often enough that you are sure it never happens, or code the way I did, which is to look for the inflections. Either way works just as well!

I still don’t quite understand your code, as I wasn’t able to get in functioning. So, a way to fix this in the way I did it, would be to do this:

//if we are at the bottom
if(bright < DIMMEST+5){ trigger = false; }

//if we are at the top
if(bright > BRIGHTEST-5){ trigger = true; }

The < and > instead of ==, would allow a bit of headroom, I’m sure you could go higher as the eye might not see the difference.

Yep. I bet that’ll do it!

Hi Mark, this is potentially a very powerful feature. It is cool to specifiy upper and lower limits, this is exaclty what I did for many animations. Can I “stack” several beatsin8s for more complex waveforms?
I am assuming your function is also meant to sync leds effects to music by hand (?). If so, some remarks from a guy who synced light effects to beats long before he touched the first microcontroller: a “start from zero button” is very helpful to move the starting point ofthe animation within the beat sequence itself. A more fine adjustment of the bpm helps to have the leds running syncron (enough) longer without triggering “start from zero” again. Is there an easy way to do that with your functions?
Thanks and greetings, Stefan

Yeah- I know what you mean and there is an annoying way to do it already, but there really should be an easier way. I sense v3.0.1 coming.

But: read lib8tion.h near the beat functions and there is a little note there about defining your own time base – which means you could reset it at any time.
BEFORE you #include FastLED, do this:
#define USE_GET_MILLISECOND_TIMER 1
Then you can (have to!) supply a function like this
uint32_t get_millisecond_timer()
{
return millis();
}

Initially, just define that to return millis() as shown. But then you can change it to return “millis() - resetTime” with resetTime being a global uint32_t, and there you go. Just set resetTime = millis() any time you want to reset the beats to zero.

But really, the beat functions should allow separate time bases and phase offsets to be passed in, for this and other reasons. I’ll put that on the “to do” list.

Ooooo, this is so much fun, I get such a greater range of randomized hue’s this way:
byte BEAT_3 = beatsin8( 6, 0, 255);
byte BEAT_4 = beatsin8( 2, 0, 255);
average = (BEAT_3 + BEAT_4) / 2;

And do start getting more of the possible range of colors, I vary the saturation and brightness of the CHSV assignment:

brightness= beatsin8( 3, 185, 255);
saturation= beatsin8( 6, 200, 255);

leds[i] = CHSV(average , saturation, brightness);

I’m sort of a beginner trying to use Fastled 3.1

  1. I connected a pot to vary the bpm of a fade in / out effect. The pot helps me beat-match to any music manually. However as with standard dj’ing cases I also need a button to reset the timer. Think of it as the play button of cd-dj’s. It will re-start my sine beat at the exact top. The bpm wont change though.

I read your #define USE_GET_MILLISECOND_TIMER 1 however still not clear. Cannot get my head to wrap around the details and uses of the timing. Do you have any sources I can read on this? or do you have a code example?

So far I have:
////
#define USE_GET_MILLISECOND_TIMER 1
uint32_t resetTime;
const int buttonPin = 7;
void setup() {
pinMode(buttonPin, INPUT);
uint32_t get_millisecond_timer()
{
return millis();
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
resetTime = millis() ;
}
////

  1. I’m still researching this one but the leds flicker as I change the pot. Is there a way to prevent this flickering, I tried using a capacitor but didnt seem to help.

Change your
get_millisecond_timer to return millis() - resetTime and try that!

that halted the entire strip at max brightness interestingly. Here is my entire code: http://pastie.org/10342234

You sure that HIGH means Pressed, the way you have it wired? Does the strip run if you hold the button?

Ok now it worked! With a linear pot and a (play) button its basically a dj / vj controller now. Thanks! My final code is here: http://pastie.org/10362159