FastLED - Increase BPM Speed

The sine functions in this library are incredible. My only issue is that I need the BPM to be about 5 times faster. I need an LFO around 20Hz and the fastest I can get is 256 BPM / 60 = 4.26667Hz

Any idea how I can do this?

#include <FastLED.h>
float BPM = 256;

float phase000 = (65535/360)*  0;
float phase090 = (65535/360)* 90;
float phase120 = (65535/360)*120;
float phase180 = (65535/360)*180;
float phase240 = (65535/360)*240;
float phase270 = (65535/360)*270;

void setup() { 
  pinMode(2, OUTPUT);  // sets the pin as output
  pinMode(3, OUTPUT);  // sets the pin as output
  pinMode(4, OUTPUT);  // sets the pin as output

void loop() { 
  float LFO1 = beatsin16(BPM*256,0,65535,0,0);
  float LFO2 = beatsin16(BPM*256,0,65535,0,phase120);
  float LFO3 = beatsin16(BPM*256,0,65535,0,phase240);

  LFO1 = LFO1/256;
  LFO2 = LFO2/256;
  LFO3 = LFO3/256;
  analogWrite(2, LFO1);
  analogWrite(3, LFO2);
  analogWrite(4, LFO3);


You have to explain what you are talking about.

The FastLED has optimized 16-bit integer trigonometry functions, such as sine and cosine. They say it is 10 times faster.

I suppose you use them for sound.
I also suppose that you have an Arduino board.
But that's about it.

If you run this code and output it to the serial plotter you'll see what I mean. It works great, I just need a faster LFO.

Currently running on an Arduino MEGA

The 'BPM' and the 'phase...' are float variables. You should avoid them to keep it fast.
I think the compiler will help you and pre-calculate the value, but you better use only integers in the loop().

Printing text to the serial monitor makes it slow, even with a high baudrate.
You don't have a limit of how many text is printed, the Serial.print() is in the loop() and the loop() runs free at maximum speed.
Remove those Serial.print() and check the speed.

Where can I find the source code and usage of the beatsin16() function ?

Here you go: FastLED: Fast math functions


The speed issue is not a loops per minute issue on the Arduino code. 256 BPM is the maximum value allowed in the FastLED library.

The millis() is used, and with those calculations the sine value of that moment is calucalated. I don't understand all those calculations, but I can see the 256 and the 60 in the code.
I think that you can not use those functions.

The analogWrite() has a PWM frequency of about 490Hz (some pins 980Hz):

With 490Hz, a frequency can be made of 20Hz, but then it has distortion.

I suggest to use a board that has Sound or DSP or a library for Audio.
For you Mega board, there is Mozzi, but it has only one audio output.

Maybe use a lookup table:

const unsigned int LUTsize = 256; // Look Up Table size: has to be power of 2 so that the modulo LUTsize
// can be done by picking bits from the phase avoiding arithmetic
uint8_t sintable[LUTsize] PROGMEM = { // already biased with +127
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.