Help with a potentiometer of my project of a MIDI controller

Hi, I'm making a full functional keyboard MIDI controller with Arduino Mega, the Control Surface library and my 61 keys keyboard. I have this code, with it, I can have all the keys with a pin of Arduino, but I want to control the "velocity" of the keys with a potentiometer.

I want to add a potentiometer that control the x value of the velocity of all keys, this way, if I want to play something soft, I reduce the value with the potentiometer (this is diferent of the volume).

How can I make that?

#include <Control_Surface.h> // Include the Control Surface library
#include <MIDI_Interfaces/SerialMIDI_Interface.hpp>
HairlessMIDI_Interface midi;
byte x = 100; 

NoteButton C1 {2,  {MIDI_Notes::C (2), CHANNEL_1},x};
NoteButton Db1{3,  {MIDI_Notes::Db(2), CHANNEL_1},x};
NoteButton D1 {4,  {MIDI_Notes::D (2), CHANNEL_1},x};
NoteButton Eb1{5,  {MIDI_Notes::Eb(2), CHANNEL_1},x};
NoteButton E1 {6,  {MIDI_Notes::E (2), CHANNEL_1},x};
NoteButton F1 {7,  {MIDI_Notes::F_(2), CHANNEL_1},x};
NoteButton Gb1{8,  {MIDI_Notes::Gb(2), CHANNEL_1},x};
NoteButton G1 {A1, {MIDI_Notes::G (2), CHANNEL_1},x};
NoteButton Ab1{A2, {MIDI_Notes::Ab(2), CHANNEL_1},x};
NoteButton As1{A3, {MIDI_Notes::A (2), CHANNEL_1},x};
NoteButton Bb1{A4, {MIDI_Notes::Bb(2), CHANNEL_1},x};
NoteButton Bs1{A5, {MIDI_Notes::B (2), CHANNEL_1},x};

void setup() {
  Control_Surface.begin(); } // Initialize Control Surface
void loop() {
  Control_Surface.loop(); } // Update the Control Surface

In this code I only have 12 notes, but in my complete code I have all the 61.

https://tttapa.github.io/Control-Surface-doc/Doxygen/db/d32/classCCPotentiometer.html

Consider using the NoteButtons<N> class instead of individual NoteButton objects.

Then you can use the NoteButtons::setVelocity() function to change the velocity based on a potentiometer value.

For example:

#include <Control_Surface.h>

USBMIDI_Interface midi;

NoteButtons<12> buttons {
  {2, 3, 4, 5, 6, 7, 8, A1, A2, A3, A4, A5},
  {MIDI_Notes::C(2), CHANNEL_1},
  {1, 0},
};

FilteredAnalog<7> potentiometer {A0}; // Reads 7-bit values

void setup() {
  Control_Surface.begin();
  potentiometer.resetToCurrentValue();
}

void loop() {
  static Timer<millis> timer = 5; // ms
  if (timer && potentiometer.update())
    buttons.setVelocity(potentiometer.getValue());
  Control_Surface.loop();
}
1 Like

Thank you very much, it worked perfectly! :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.