Tuesday, October 24, 2017

Midi To Trig with selectable MIDI channel.

I combined the work of two great minds and made this midi to trigger interface with changeable midi channel.

http://www.notesandvolts.com and http://youthvulture.blogspot.com/2014/03/rhythm-ace-fr-2l-midi-retrofit-with.html




// ************************************************************************
// AUDUINO - MIDI Upgrade v1.1
// For Arduino NANO
//
// MIDI programming by Notes and Volts http://www.notesandvolts.com
//
// Tutorial - http://www.notesandvolts.com/2015/05/auduino-synth-midi.html
//
// ** Requires Arduino MIDI Library v4.2 or later **
//
// Based on the Auduino Synthesizer v5 by Peter Knight http://tinker.it
// ************************************************************************
// Version 1.0 - Initial release
// Version 1.1 - Fixed bug that caused note to hang with some DAWs
// ************************************************************************

#include <avr/io.h>
#include <avr/interrupt.h>
#include <MIDI.h> // Requires Arduino MIDI Library v4.2 or later

// *******************************************************************************
// If the DIP Switch option is installed - set dipSwitchInstalled to "true"
const bool dipSwitchInstalled = true;
// DIP Switch Setting = Channel (1=ON, 0=OFF)
// 0000 = 1   0001 = 2   0010 = 3   0011 = 4
// 0100 = 5   0101 = 6   0110 = 7   0111 = 8
// 1000 = 9   1001 = 10  1010 = 11  1011 = 12
// 1100 = 13  1101 = 14  1110 = 15  1111 = 16
//
// If DIP Switch is NOT installed - change dipSwitchInstalled to "false"
// If dipSwitchInstalled is set to false, the MIDI channel will be set
// to the value of MIDICHANNEL
#define MIDICHANNEL 1 // Can be a value from 1 to 16
// *******************************************************************************

MIDI_CREATE_DEFAULT_INSTANCE();

// DIP Switch Pins
#define DIP_SW1 9
#define DIP_SW2 10
#define DIP_SW3 11
#define DIP_SW4 12

#if defined(__AVR_ATmega8__)
//
// On old ATmega8 boards.
//    Output is on pin 11
//
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       5
#elif defined(__AVR_ATmega1280__)
//
// On the Arduino Mega
//    Output is on pin 3
//
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       7
#else
//
// For modern ATmega168 and ATmega328 boards
//    Output is on pin 3
//
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       5
#endif

#define TRIG1 2     // Trig1 set to Arduino Pin 2
#define TRIG2 3     // Trig2 set to Arduino Pin 3
#define TRIG3 4     // Trig3 set to Arduino Pin 4
#define TRIG4 5     // Trig4 set to Arduino Pin 5
#define TRIG5 6     // Trig5 set to Arduino Pin 6
#define TRIG6 7     // Trig6 set to Arduino Pin 7
#define TRIG7 8     // Trig7 set to Arduino Pin 8

int TIME = 5;        // Very short delay time to keep the loop snappy

void NoteOnMidi(byte channel, byte pitch, byte velocity){
  digitalWrite(TRIG1, LOW);    // All pins set to LOW, since
  digitalWrite(TRIG2, LOW);  // rising edge (HIGH to LOW).
  digitalWrite(TRIG3, LOW);
  digitalWrite(TRIG4, LOW);
  digitalWrite(TRIG5, LOW);
  digitalWrite(TRIG6, LOW);
  digitalWrite(TRIG7, LOW);


  if (velocity > 0){             // So midi devices that send note-on at zero
                                 // velocity for note-off don't double trigger.
    if (pitch == 60) {        
      digitalWrite(TRIG1, HIGH);   // Looks for specific note numbers. If
      delay(TIME);               // they occur, rises from LOW to HIGH,
      digitalWrite(TRIG1, LOW);  // firing the trigger circuit, then
    }                            // goes back to LOW in anticipation of
    else if(pitch == 62) {
      digitalWrite(TRIG2, HIGH);
      delay(TIME);
      digitalWrite(TRIG2, LOW);
    }
       else if(pitch == 64) {
      digitalWrite(TRIG3, HIGH);
      delay(TIME);
      digitalWrite(TRIG3, LOW);
    }
    else if(pitch == 65) {
      digitalWrite(TRIG4, HIGH);
      delay(TIME);
      digitalWrite(TRIG4, LOW);
    }
    else if(pitch == 67) {
      digitalWrite(TRIG5, HIGH);
      delay(TIME);
      digitalWrite(TRIG5, LOW);
    }
    else if(pitch == 69) {
      digitalWrite(TRIG6, HIGH);
      delay(TIME);
      digitalWrite(TRIG6, LOW);
    }
    else if(pitch == 71) {
      digitalWrite(TRIG7, HIGH);
      delay(TIME);
      digitalWrite(TRIG7, LOW);
 
    }
  }
}




void setup() {
  if (dipSwitchInstalled) {
    pinMode(DIP_SW1, INPUT_PULLUP);
    pinMode(DIP_SW2, INPUT_PULLUP);
    pinMode(DIP_SW3, INPUT_PULLUP);
    pinMode(DIP_SW4, INPUT_PULLUP);
  }
  pinMode(LED_PIN, OUTPUT);
  pinMode(TRIG1, OUTPUT);
  pinMode(TRIG2, OUTPUT);
  pinMode(TRIG3, OUTPUT);
  pinMode(TRIG4, OUTPUT);
  pinMode(TRIG5, OUTPUT);
  pinMode(TRIG6, OUTPUT);
  pinMode(TRIG7, OUTPUT);
 
    if (dipSwitchInstalled) {
    MIDI.begin(DipSwitch());
  }
  else {
    MIDI.begin(MIDICHANNEL);
  }
  MIDI.setHandleNoteOn(NoteOnMidi);
 
}
byte DipSwitch() {
  byte value = 0;
  if (digitalRead(DIP_SW4) == LOW)
    value += 1;
  if (digitalRead(DIP_SW3) == LOW)
    value += 2;
  if (digitalRead(DIP_SW2) == LOW)
    value += 4;
  if (digitalRead(DIP_SW1) == LOW)
    value += 8;
  return (value + 1);
}


void loop(){
  MIDI.read();          // Tells Arduino to start listening for
}                       // midi info on the serial port

No comments: