Hello, I am wondering if it's possible to use beatsin8 (or some related beat

Hello,

I am wondering if it’s possible to use beatsin8 (or some related beat generating function) to produce floating point and/or negative values? I would like to smoothly change the speed of a pattern back and forth by changing how quickly some index value changes. Typically, good ranges are between (-2,2), otherwise the pattern moves too quickly. But beatsin8 only generates int values, making it jerky. Is there another similar function I can use?

My attempted solution was to add a beatsin_float function in the FastLED library, in the lib8tion.h file. However, the function doesn’t seem to be saved after defining it in the file, saving, restarting arduino, reloading the library, etc. How can I add another definition here? Thank you!

How much precision do you want out of the function? 8-bits worth? 16-bits worth? You could use (sin16(t) / 32768.0), e.g.

@Mark_Kriegsman I don’t need much precision, maybe to two decimal places, but the important part is to allow decimal values for smooth transitions between -2,2. Sin16 is still an int value, correct? I figured the easiest thing would be to make a new sin_float definition in the library, but it doesn’t seem to save.

Yep- Sin16 is an integer between -32768 and 32767. Dividing by 32768.0 will give you float between -1.000 and 0.9999

Dividing by 16384.0 would directly give you a float between -2.000 and 1.999

In general, we don’t use float in library because of how slow it is on AVR ATmega chips. We tend to prefer fixed-point math with is fast on all platforms. Hence lots of integers, which you can then scale as needed.

@Mark_Kriegsman I see, makes sense. I actually tried some of the 16-bit beat functions, dividing by 32767 to scale them, but they always rounded to the nearest integer. I assumed sin16 would behave similarly, but perhaps not. I liked the simple elegance of the beatsin function and ability to simply choose a BPM that will match other functions, but I’ll give this a shot when I get a chance after work. Thank you!

@Dylan_Lovinger Note: You have to divide by 16384.0 (with the .0 on the end, not just 16384) otherwise you’ll get an integer.

That’s it right there ^… what @marmil said. You have to divide by a floating point number (16384.0) to get a floating point result.

@marmil @Mark_Kriegsman Ah! That’s what it is! Works perfectly now. Most of my programming experience is in matlab, where I don’t need to worry about different data types. Thank you both for helping me catch the stupid mistake!