From ae98ad7a8e47b48c12e699870f47eeaf74dfa6f1 Mon Sep 17 00:00:00 2001 From: monkeybiscuits Date: Tue, 20 Jan 2015 11:13:11 +0900 Subject: [PATCH] Stabilized the Potentiometer read() function Adds a second condition that greatly increases the stability of the read() function. Eliminates messages outside the MIDI range when an input is mapped. --- Potentiometer.cpp | 25 +++++++++++++++++-------- Potentiometer.h | 3 ++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Potentiometer.cpp b/Potentiometer.cpp index 202cf61..6cb93b1 100644 --- a/Potentiometer.cpp +++ b/Potentiometer.cpp @@ -39,15 +39,24 @@ void Potentiometer::read(){ if(mapped){ tempRead=constrain(analogRead(pin),inMin,inMax); - tempRead=map(tempRead,inMin,inMax,0,127); + + /* Need to maintain analog resolution for the next + 'if' statement but leaving out the upper and + lower limits prevents messages outside the MIDI range*/ + tempRead=map(tempRead,inMin,inMax,7,1015); } else - tempRead=map(analogRead(pin), 0, 1023, 0, 127); - - if (tempRead!=lastValue) { //value changed - midiCC(tempRead, lastValue); - } - lastValue=tempRead; + tempRead=analogRead(pin); + + /*1 - Split the analog signal into blocks of 4 + and every other block of four is ignored. + 2 - Reduce the resolution for MIDI and compare + against the previously sent message.*/ + if ((tempRead/4)%2 == 0 && tempRead/8 != lastValue){ + newCC = tempRead/8; + midiCC(newCC, lastValue); + lastValue = newCC; + } } // read @@ -149,4 +158,4 @@ void Potentiometer::midiCC(int v, int oldv) { void Potentiometer::changeSecondary( bool s){ secondary=s; -} \ No newline at end of file +} diff --git a/Potentiometer.h b/Potentiometer.h index 1f7c163..a0af1c5 100644 --- a/Potentiometer.h +++ b/Potentiometer.h @@ -24,6 +24,7 @@ class Potentiometer { int inMin, inMax; int lastValue; int tempRead; + int newCC; int readValues[3]; byte pin; // pin on teensy byte channel; // midi channel @@ -44,4 +45,4 @@ class Potentiometer { }; //----------------------------------------------------------------------------------- -#endif \ No newline at end of file +#endif