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() { 
  Serial.begin(115200);
  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);
    
  Serial.print(LFO1);
  Serial.print(",");
  Serial.print(LFO2);
  Serial.print(",");
  Serial.println(LFO3);

  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

and

http://fastled.io/docs/3.1/lib8tion_8h_source.html#l00901

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):
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

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
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,
  176,179,182,184,187,190,193,195,198,200,203,205,208,210,213,215,
  217,219,221,224,226,228,229,231,233,235,236,238,239,241,242,244,
  245,246,247,248,249,250,251,251,252,253,253,254,254,254,254,254,
  255,254,254,254,254,254,253,253,252,251,251,250,249,248,247,246,
  245,244,242,241,239,238,236,235,233,231,229,228,226,224,221,219,
  217,215,213,210,208,205,203,200,198,195,193,190,187,184,182,179,
  176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,
  127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,
  78,75,72,70,67,64,61,59,56,54,51,49,46,44,41,39,
  37,35,33,30,28,26,25,23,21,19,18,16,15,13,12,10,
  9,8,7,6,5,4,3,3,2,1,1,0,0,0,0,0,
  0,0,0,0,0,0,1,1,2,3,3,4,5,6,7,8,
  9,10,12,13,15,16,18,19,21,23,25,26,28,30,33,35,
  37,39,41,44,46,49,51,54,56,59,61,64,67,70,72,75,
78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124};
1 Like