Midi notes to Digital outputs

Great! I think I just need a few breadcrumbs to follow. Let me see where this leads. Thanks!

Well that worked! Thanks slipstick! This code will be used to control my Revox A77 reel to reel tape recorder(s) with my DAW (Digital Performer) for performance and I just crossed the first big hurdle! Next step is to connect the Arduino to the relays of the machine. I will post the finished project when it is complete in a new post.

Wow I never thought I would see the day I would write my own program! It's an amazing feeling.

If there is a way to make this code more efficient please do let me know.

Thanks!

#include <MIDI.h>  // Add Midi Library


// Define labels for the 6 pins
#define REW 7   
#define FF 6
#define PLAY1 5
#define PLAY2 4
#define STOP 3
#define REC 2

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();



//output pin 7-2 when receiving noteON message with note = 60-65

void setup() {
  pinMode (REW, OUTPUT); 
  pinMode (FF, OUTPUT); 
  pinMode (PLAY1, OUTPUT); 
  pinMode (PLAY2, OUTPUT); 
  pinMode (STOP, OUTPUT); 
  pinMode (REC, OUTPUT); 
 
  MIDI.begin(16); // Initialize the Midi Library. Respond to notes on channel 16 only.
  MIDI.setHandleNoteOn(Press); 
  MIDI.setHandleNoteOff(Release); 
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
}

// Press is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity

void Press (byte channel, byte note, byte velocity){   
  if (note== 60 ){ 
  digitalWrite(REW,HIGH);  // Send on pin 7 
  }
  if (note == 61){
    digitalWrite(FF,HIGH);  // Send on pin 6
  }
   if (note == 62){
    digitalWrite(PLAY1,HIGH); 
    digitalWrite(PLAY2,HIGH);// Send on pin 4 and 5
  }
   if (note == 63){
    digitalWrite(STOP,HIGH);  // Send on pin 4
  }
   if (note == 64){
    digitalWrite(REC,HIGH); 
    digitalWrite(PLAY2,HIGH);// Send on pin 4 and 6
  }
}

// Release is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity

void Release(byte channel, byte note, byte velocity) {  
  if (note== 60){
  digitalWrite(REW,LOW);  // Stop send on pin 7
  }
   
    if (note== 61){
  digitalWrite(FF,LOW);  // Stop send on pin 6
    }
    
    if (note == 62){
  digitalWrite(PLAY1,LOW);
  digitalWrite(PLAY2,LOW);// Stop send on pin 4 AND 5
  }
   
    if (note == 63){
  digitalWrite(STOP,LOW);  // Stop send on pin 4
  }
 
    if (note == 64){
  digitalWrite(REC,LOW);
  digitalWrite(PLAY2,LOW);// Stop send on pin 2 AND 4
  }
  }

If there is a way to make this code more efficient please do let me know.

Make the notes and the pin numbers into an array each and all those if statements disappear into a for loop and single if.

For the cases with two digital writes like play1 and play2 have a separate array for these extra writes, this will be a sparse, that is lots of numbers that signify do nothing or the number of a pin. You then need another if statement inside the loop to see if you need to write the supplemental.

As a rule when you are doing the same thing over and over you can nearly always change this into an array and a loop.