 # Arduino LFO (code included)

This isn’t a question. I just want to give this code out in case anybody needs an LFO in future.

[code]void setup() {
pinMode(A1, INPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
}

float FloatDif(float f1, float f2) {
if (f1 < f2) {
return f2 - f1;
}
return f1 - f2;
}

void WritePercentage(int outpin, float pctg) {
int adjpctg = constrain(pctg * 2.55, 0, 255);
}

float max_pc = 100;
float max_in_voltage = 1024;
float last_freq = -1;
unsigned long endcyclems = 0;
float rem_pc = 100;
float cycleperiod = 0;

void loop() {
unsigned long nowms = millis();

float freq = max_pc / max_in_voltage * analogRead(A1);
freq = max(freq,0.1);
if (FloatDif(freq, last_freq) > 1 || nowms > endcyclems) {
last_freq = freq;
cycleperiod = freq * 500;
endcyclems = nowms + (cycleperiod / max_pc * rem_pc);
}

float pc = max_pc - (max_pc / cycleperiod * (endcyclems - nowms) );
WritePercentage(5, pc/10);

float lfofreqpc = max_pc;
if (pc < 50) {
lfofreqpc = pc * 2;
} else {
if (pc > 50) {
lfofreqpc = (100 - pc) * 2;
}
}
WritePercentage(9, lfofreqpc);

rem_pc = max_pc - pc;
if (pc > 99) {
rem_pc = 100;
}

}[/code]

You control the speed of the LFO using a dimmer bit connected to the Arduino’s A1 socket. The LFO frequency is outputted by socket D9 (try it with a filter). Optionally, the current stage of the LFO’s “cycle” can be displayed (from 0 to 10) in a number bit connected to D5.

If you want to expand or reduce the potential cycle length, change 500 to some other number here:

``cycleperiod = freq * 500``

I hope this is useful to somebody.

NB. People might wonder why the current frequency is calculated using the end time of the cycle, not the start time. It’s so that you can change the speed of the cycle without changing where you are in the cycle, which would create an unpleasant “jump”.

1 Like

Thanks for a great work. As I am new to Arduino it will take some time before I can write good synth sketches.
That I really need is a sequencer for the Synth Kit and a sequencer with CV and Gate out for Arduino Uno.
Do you have that in your pipe line ?

I’m afraid not, Jan. I wouldn’t know where to begin with such things!

Presumably you know that LB are working on a CV bit? http://littlebits.cc/bits/newsynthmodules