tttapa / MIDI_controller

This is a library for creating a MIDI controller using an Arduino or Teensy board.
GNU General Public License v3.0
402 stars 70 forks source link

Current midi value for each analog input #60

Closed squirelo closed 6 years ago

squirelo commented 6 years ago

Hi,

I'm using 4 faders and I need to know their current midi value (0-127) to control motors.

Is there an easy way to get the current value for each fader from your library? A small example would be awesome :)

Sorry if this question looks noobish, I spent consequent time digging your code to understand how to do it by myself but it seems I need a little extra help.

Thx a lot

tttapa commented 6 years ago

The value is saved in the oldVal member variable: https://github.com/tttapa/MIDI_controller/blob/79070e9531d738d1e47a8c6d94b4a5c4e502fe28/src/MIDI_Outputs/Analog.h#L91

If you just want the value of a potentiometer, and don't care about the MIDI values sent out, take a look at this ´FilteredAnalog´ class I use for my Control Surface library: https://github.com/tttapa/Control-Surface/blob/revision-4/src/Hardware/FilteredAnalog.h

squirelo commented 6 years ago

Awesome, thanks a lot for your reactivity, much appreciated.

I had to learn about hierarchy and how to derive a class ^^, but here it is for other who will have the same need than me.

https://gist.github.com/squirelo/8eeb720a435426783389ec836d9217a0

tttapa commented 6 years ago

Great!

One thing to watch out for: Sending Serial messages and other things that rely on interrupts can cause serious problems if you use them inside of an ISR (where interrupts are disabled automatically).
The best solution is to set buttonState to true (keeping the ISR as short as possible) and then check the value of buttonState in your main loop.

For example:

volatile bool buttonWasPressed = false;

void loop() {
  MIDI_Controller.refresh();
  if (buttonWasPressed) {
    doSomethingWithValues();
    buttonWasPressed = false;
  }
}

void pin_ISR() {
  buttonWasPressed = true;
}