Skip to content

Commit

Permalink
Smoothed pot values to reduce jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgallup committed Dec 5, 2018
1 parent 3c050fa commit 74b23e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
15 changes: 11 additions & 4 deletions DUAL_LFO/DUAL_LFO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ const double LFO2_FREQ_RANGE = LFO2_FREQ_MAX - LFO2_FREQ_MIN;
const double clock = 31372.549;

// LFO 1 controls
CS_Pot LFO1_FreqKnob (FREQ1_KNOB_PIN);
CS_Pot LFO1_DepthKnob (DEPTH1_KNOB_PIN);
CS_Pot LFO1_FreqKnob (FREQ1_KNOB_PIN, 3);
CS_Pot LFO1_DepthKnob (DEPTH1_KNOB_PIN, 3);
CS_Switch LFO1_WaveSwitch(WAVE1_SWITCH_PIN);

// LFO 2 controls
CS_Pot LFO2_FreqKnob (FREQ2_KNOB_PIN);
CS_Pot LFO2_DepthKnob (DEPTH2_KNOB_PIN);
CS_Pot LFO2_FreqKnob (FREQ2_KNOB_PIN, 3);
CS_Pot LFO2_DepthKnob (DEPTH2_KNOB_PIN, 3);
CS_Switch LFO2_WaveSwitch (WAVE2_SWITCH_PIN);

// Generic pin state variable
Expand Down Expand Up @@ -150,6 +150,13 @@ void setup()
pinMode(LFO1_OUTPUT_PIN, OUTPUT); // pin11= PWM:A
pinMode(LFO2_OUTPUT_PIN, OUTPUT); // pin 3= PWM:B

// Initialize knobs
LFO1_FreqKnob.begin();
LFO1_DepthKnob.begin();
LFO2_FreqKnob.begin();
LFO2_DepthKnob.begin();


// Initialize wave tables
LFO1_WaveTable = waveTables[0];
LFO2_WaveTable = waveTables[0];
Expand Down
41 changes: 38 additions & 3 deletions DUAL_LFO/src/CS_Pot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,51 @@

#include "CS_Pot.h"

// Macro to increment pointer to circular buffer
#define next(x) {x+=1;if(x>=_buffLen)x=0;}

// Constructor : clear queues
CS_Pot::CS_Pot (byte p)
CS_Pot::CS_Pot (byte p, byte smooth)
{
_smooth = 0;
_pin = p;
value();

// If smoothing value is in range, initialize smoothing
if ((smooth>0)&&(smooth<=MAXSMOOTH)) {
_smooth = smooth;

// Buffer length is 2^smooth. Ptr starts at beginning
_buffLen = (1<<_smooth);
_ptr = 0;

}
}

// Initialize controls
void CS_Pot::begin()
{
// Initialize sum and fill buffer with current reading
_sum = 0;
for (byte i=0; i<_buffLen; i+=1) {
_values[i]=analogRead(_pin);
_sum += _values[i];
}
}

// Read potentiometer value
int CS_Pot::value ()
{
return (_lastValue = analogRead(_pin));

// Take a pot reading. If there's no smoothing, return the raw value
int reading = analogRead(_pin);
if (0==_smooth) return reading;

// Otherwise, update the running sum by adding the new value and subtracting the oldest previous value
_sum = _sum + reading - _values[_ptr];
// Increment the pointer, save the new value, and return the "smoothed" value (divided by 2^smooth)
_values[_ptr] = reading;
next(_ptr);
return (_sum>>_smooth);

}

12 changes: 10 additions & 2 deletions DUAL_LFO/src/CS_Pot.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@

#include "Arduino.h"

#define MAXSMOOTH 3

// CLASS
class CS_Pot
{
public:

CS_Pot(byte);
CS_Pot(byte p, byte smooth=0);
void begin();
int value();

private:

int _lastValue;
byte _pin;
byte _smooth;

int _sum;
byte _ptr;
byte _buffLen;
int _values[2<<MAXSMOOTH];

};

Expand Down

0 comments on commit 74b23e7

Please sign in to comment.