Control_surface Midi Channel DIP switches

Hi,

I want to be able to set the MIDI channel for my controller with 4 DIP switches and some resistors connected to an analog input.

But I do not understand how to change between CHANNEL_1, CHANNEL_2 and so on, since theses are constants and not values.

Can somebody help?

Best Regards,
Jan Sjöberg

please post an example sketch which uses your CHANNEL_1, CHANNEL_2 constants

Use the Channel constructor or Channel::createChannel() depending on whether your input is zero-based or one-based.

See also the definition of CHANNEL_1:

constexpr Channel CHANNEL_1 = Channel::createChannel(1);

Can I use Channel::createChannel(midi_channel) instead of CHANNEL_3 in NoteButtonMatrix section?

This is my code:

#include <Control_Surface.h>
 
 // Midi-channel

#define PIN_CHANNEL A5
// channel definition
int channel_read;
int midi_channel = 16;

// DIP4  common thru 1k to 5V and 1M to gnd,  1=10k, 2=4,7k, 4=2,2k, 8=1k  to analog channel pin
// theoretical channel readings:       {1023, 930, 844, 779, 704, 658, 614, 579, 512, 487, 463, 443, 417, 401, 384, 370}; 
// borders between the channel readings, adopted to real channel readings:
const int channel_dig[15] = {976, 887, 813, 743, 682, 637, 598, 546, 501, 477, 455, 431, 410, 394, 378}; 

USBMIDI_Interface usbmidi;
HardwareSerialMIDI_Interface serialmidi {Serial1, MIDI_BAUD};

// Create a MIDI pipe factory to connect the MIDI interfaces to Control Surface
BidirectionalMIDI_PipeFactory<2> pipes;
 
// The note numbers corresponding to the buttons in the matrix
const AddressMatrix<4, 8> addresses {{
  //{24, 25, 26, 27, 28, 29, 30, 31},  //Low C
  //{32, 33, 34, 35, 36, 37, 38, 39},
  //{40, 41, 42, 43, 44, 45, 46, 47},
  //{48, 49, 50, 51, 52, 53, 54, 55},
  {36, 37, 38, 39, 40, 41, 42, 43},    // Standard C ?
  {44, 45, 46, 47, 48, 49, 50, 51},
  {52, 53, 54, 55, 56, 57, 58, 59},
  {60, 61, 62, 63, 64, 65, 66, 67},
}};
 
 
NoteButtonMatrix<4, 8> buttonmatrix {
  {2, 3, 4, 5}, // row pins
  {6, 7, 8, 9, 10, 11, 12, 13},    // column pins
  addresses,    // address matrix
  CHANNEL_3    // channel and cable number
};
 
void setup() {
    // Manually connect the MIDI interfaces to Control Surface
  Control_Surface | pipes | usbmidi;
  Control_Surface | pipes | serialmidi;
 
  pinMode(PIN_CHANNEL, INPUT);

   // determine channel
  channel_read = analogRead(PIN_CHANNEL);
  for (int i = 1; i < 16; i++) {
    if (channel_read > channel_dig[i-1] && channel == 16) {
      midi_channel = i;
    }

  
  }
  Control_Surface.begin();
}
 
void loop() {
  Control_Surface.loop();
}

Sorry,

if (channel_read > channel_dig[i-1] && channel == 16)

should be

if (channel_read > channel_dig[i-1] && midi_channel == 16)

And I am aware of that the midi channel is read on startup , and could not be changed while power is on: at least this is my intention.

A quick solution is to use a static variable inside of the setup function:

#include <Control_Surface.h>
 
 // Midi-channel
const int PIN_CHANNEL = A5;

// DIP4  common thru 1k to 5V and 1M to gnd,  1=10k, 2=4,7k, 4=2,2k, 8=1k  to analog channel pin
// theoretical channel readings:       {1023, 930, 844, 779, 704, 658, 614, 579, 512, 487, 463, 443, 417, 401, 384, 370}; 
// borders between the channel readings, adopted to real channel readings:
const int channel_dig[15] = {976, 887, 813, 743, 682, 637, 598, 546, 501, 477, 455, 431, 410, 394, 378}; 

USBMIDI_Interface usbmidi;
HardwareSerialMIDI_Interface serialmidi {Serial1, MIDI_BAUD};

// Create a MIDI pipe factory to connect the MIDI interfaces to Control Surface
BidirectionalMIDI_PipeFactory<2> pipes;
 
// The note numbers corresponding to the buttons in the matrix
constexpr AddressMatrix<4, 8> addresses {{
  {36, 37, 38, 39, 40, 41, 42, 43},    // Standard C ?
  {44, 45, 46, 47, 48, 49, 50, 51},
  {52, 53, 54, 55, 56, 57, 58, 59},
  {60, 61, 62, 63, 64, 65, 66, 67},
}};

void setup() {
    // Manually connect the MIDI interfaces to Control Surface
  Control_Surface | pipes | usbmidi;
  Control_Surface | pipes | serialmidi;
 
  pinMode(PIN_CHANNEL, INPUT);

  // determine channel
  int channel_read = analogRead(PIN_CHANNEL);
  int midi_channel = 16;
  for (int i = 1; i < 16; i++) {
    if (channel_read > channel_dig[i-1]) {
      midi_channel = i;
      break;
    }
  }

  static NoteButtonMatrix<4, 8> buttonmatrix {
    {2, 3, 4, 5},                    // row pins
    {6, 7, 8, 9, 10, 11, 12, 13},    // column pins
    addresses,                       // address matrix
    Channel::createChannel(midi_channel)
  };
  
  Control_Surface.begin();
}
 
void loop() {
  Control_Surface.loop();
}

The static keyword is important: you don't want the buttonmatrix variable to be destroyed at the end of the setup function.

PieterP: Thank's a lot! It works perfectly! And you added a break, wich I had forgot as well.

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