Midi to logic controls

Status
Not open for further replies.

Anoat

Active member
Hello everybody,

I am currently making a USB MIDI controlled synthesizer. The microcontroller is a teensy 4.1. This should drive 2 dac Max11300 and 7 switch DG333.

I specify, I am new to programming, I learn a little more every day thanks to you

To drive the DG333 switches I use the teensy outputs. Each output is linked to a midi control, at the value 0 the output is at 0, at 127 the output is at 3.3v.

In the order the outputs are linked to the following midi controls:
outputs ( digital pins) : 11, 4, 23, 5, 7, 20, 17, 3, 35, 16, 30, 32, 34, 36
midi controls: 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95

The easiest (at my level) is to create a condition like:

void OnControlChange (byte channel, byte control, byte value) {
if (control == 82)
{
pinout = 11
}
else if (control == 83)
{
pinout = 4
}
else if ......... so on ....

the problem is, it's tedious and I'm sure there must be some other solution.
 
Its good !!! I found the solution !!

While searching on the forum I found a member who wanted to light leds with midi messages.
I took its code and I modified it to match the project (I have on / off switches, on / off / on switches, and an on / on / on / on switch.

Code:
  const int channel = 1; // MIDI channel
  const int D_PINS = 13; // number of Digital PINS for swith on/off
  const int L_PINS = 10; // number of Digital PINS for swith on1/off/on2
    
  // define the pins and notes 
  
  const int SW2[D_PINS] = {36, 34, 32, 30, 33, 35, 3, 17, 20, 7, 5, 23, 4}; 
  const int MIDI_SW2[D_PINS] = {95, 94, 93, 92, 90, 89, 88, 87, 86, 85, 84, 83 ,82 };

  const int SW3_1[L_PINS] = {16, 39, 41, 24, 26, 37, 18, 22, 19, 29}; 
  const int SW3_2[L_PINS] = {9, 25, 15, 14, 38, 27, 6, 21, 8, 31}; 
  const int MIDI_SW3[L_PINS] = {96, 97, 98, 99, 100, 101, 106, 107, 111, 102};

  const int SW4_1 = 1;        // SW4_1/SW4_2 is logic control for VCF (12db/24db/hp/bp)
  const int SW4_2 = 2;        //Switch on1/on2/on3/on4
  const int MIDI_SW4 = 112;   //note for Switch (12db/24db/hp/bp)
  
  const boolean toggled = true;
  boolean state[D_PINS];
  
    
  
void setup() {
      
      usbMIDI.setHandleControlChange(OnControlChange);
    
      
      for (int i=0;i<D_PINS;i++){
        
        pinMode(SW2[i], OUTPUT);
        digitalWrite(SW2[i], LOW); // Toutes les pins out ont pour valeur LOW 
                                }
      for (int j=0;j<L_PINS;j++){
        
        pinMode(SW3_1[j], OUTPUT);
        digitalWrite(SW3_1[j], LOW); // Toutes les pins out ont pour valeur LOW 
        pinMode(SW3_2[j], OUTPUT);
        digitalWrite(SW3_2[j], LOW);
        
        pinMode(SW4_1, OUTPUT);
        pinMode(SW4_2, OUTPUT);
        digitalWrite(SW4_1, LOW);
        digitalWrite(SW4_2, LOW);
      
                                }
     
            }
  

void loop() {  
    
    usbMIDI.read();
            }   
  
    void OnControlChange(byte channel, byte controller, byte value) 
{     for (int i = 0; i < D_PINS ; i++)
  {       if (MIDI_SW2[i] == controller) 
    {          if (value >= 64) 
      {               digitalWrite(SW2[i], HIGH); 
                      state[i] = true;
      }        else {
                      digitalWrite(SW2[i], LOW); 
                      state[i] = false;
                    }    
    }
  }
     for (int j = 0; j < L_PINS ; j++)
  {       if (MIDI_SW3[j] == controller) 
    {          if (value >= 84) 
      {               digitalWrite(SW3_1[j], HIGH); 
                      digitalWrite(SW3_2[j], LOW);
                      state[j] = true;
      }        
                  else if (value < 84 && value > 42)
          {           digitalWrite(SW3_1[j], LOW);
                      digitalWrite(SW3_2[j], LOW); 
                      state[j] = false;
          } 
                  else if (value < 42)
          {           digitalWrite(SW3_1[j], LOW);
                      digitalWrite(SW3_2[j], HIGH); 
                      state[j] = true;
          }                           
    }
  }
  {       if (MIDI_SW4 == controller) 
    {          if (value >= 96) 
      {               digitalWrite(SW4_1, HIGH); 
                      digitalWrite(SW4_2, LOW);
                      
      }        
                  else if (value < 96 && value > 64)
           {          digitalWrite(SW4_1, LOW);
                      digitalWrite(SW4_2, LOW); 
                     
           } 
                   else if (value < 64 && value > 32)
           {           digitalWrite(SW4_1, LOW);
                      digitalWrite(SW4_2, HIGH); 
                     
           } 
                  else if (value < 32)
           {           digitalWrite(SW4_1,HIGH);
                      digitalWrite(SW4_2, HIGH); 
                      
           }                                                            
    }
  }
  
}
 
Status
Not open for further replies.
Back
Top