joshnishikawa / MIDIcontroller

A library for creating Teensy MIDI controllers with support for hold or latch buttons, potentiometers, encoders, capacitive sensors, Piezo transducers and other velocity sensitive inputs with aftertouch.
223 stars 19 forks source link

MIDI Pot smooth does not work at all. #4

Closed johannthorir closed 4 years ago

johannthorir commented 4 years ago

See comments in code:

int MIDIpot::smooth(int val, int NR){ buffer = 0; // set to zero so any memory gone. balancedValue = 0; // this is set to 0 here so any memory is gone difference = val - balancedValue; // now difference is equal to val - 0 which is val // if val is 0 then buffer = buffer/2, which is 0 which is val // if val is not 0 then buffer = buffer + difference which is 0 + val which is val. // from this you can see that in all cases buffer == val buffer = val == balancedValue ? buffer/2 : buffer+difference;

// if the square of the buffer which is the square of val is greater than the square of NR // then you return val // you return 0 however for any value of val which is less than the sqrt of NR.

if (bufferbuffer > NRNR){ // This works better than abs(buffer) for me. balancedValue = val; buffer = 0; } return balancedValue; };

the only use of the smooth member function is like this: this->smooth(analogRead(pin), 100); so smooth does nothing except clamp all values less than 100 to zero with no actual smoothing.

joshnishikawa commented 4 years ago

Yes the variables are initialized to zero but, as class variables in a member function, they're updated every time the function is called. Have you actually tested this with an analog input and it's not working? Or are you just looking through the code? In the examples folder, there is an image called "AnalogToMidi.jpg" which shows actual serial data recorded during testing. If you're not getting similar test results, please provide more details about your setup and exactly what results you are getting.

rustybrooks commented 4 years ago

I haven't tried it either, but look at the code - all the class variables are initialized to constants every time smooth() gets called. I don't see how it could possibly work.

rustybrooks commented 4 years ago

(I think the function will more or less "work" in that it will return values if they're larger than NR, but they won't be smoothed)

joshnishikawa commented 4 years ago

Ah! I see. They're also being initialized in the function as if they were static variables (that's what they were originally). I didn't remove those lines when I made them class variables. Oops! Thank you for pointing this out. I was able to fix a couple of other things too.

rustybrooks commented 4 years ago

Yeah that makes sense, I kind of thought it might be something like that.