Saturday, March 5, 2016

Using the Sparkfun Arduino Midi Shield to Control Virtual Synthesizer

I was able to get the Sparkfun midi Shield to work fairly easily without any major complications... The basic code (see below) simply setup the integrated pushbutton to trigger a note while pitch bend control with the first integrated potentiometer and cutoff with the second potentiometer. I would like to investigate how difficult it is to reassign the shield controls to alter different parameters of virtual synthesizers.

// SparkFun MIDI Shield and MIDI Breakout test code
// Defines bare-bones routines for sending and receiving MIDI data
// Written 02/16/10


// defines for MIDI Shield components only


//  Reason 6 MIDI implementation chart:
//  http://dl.propellerheads.se/Reason6/Reason%206%20MIDI%20Implementation%20Chart.pdf


#define KNOB1  0
#define KNOB2  1


#define BUTTON1  2
#define BUTTON2  3
#define BUTTON3  4


#define STAT1  7
#define STAT2  6


byte ccVal;
int pot;
int ccPot;
int note;
int lastNote = -1;
int lastCcVal = -1;


void setup() {
 pinMode(STAT1,OUTPUT);   
 pinMode(STAT2,OUTPUT);
 pinMode(BUTTON1,INPUT);
 pinMode(BUTTON2,INPUT);
 pinMode(BUTTON3,INPUT);


 digitalWrite(BUTTON1,HIGH);
 digitalWrite(BUTTON2,HIGH);
 digitalWrite(BUTTON3,HIGH);


 for(int i = 0;i < 10;i++) // flash MIDI Shield LED's on startup
 {
   digitalWrite(STAT1,HIGH);  
   digitalWrite(STAT2,LOW);
   delay(30);
   digitalWrite(STAT1,LOW);  
   digitalWrite(STAT2,HIGH);
   delay(30);
 }
 digitalWrite(STAT1,HIGH);   
 digitalWrite(STAT2,HIGH);


 //start serial with midi baudrate 31250
 Serial.begin(31250);     
}
void loop () {


 //*************** MIDI OUT ***************//
 pot = analogRead(KNOB1);
 ccPot = analogRead(KNOB2);
 note = pot / 8;  // convert value to value 0-50 // changed value from 0-127 to 0-50 for less drastic note range
 ccVal = 127 - ccPot / 8;
 //  send notes
 if(button(BUTTON2) || button(BUTTON3))
 {
   if (lastNote > -1) Midi_Send(0x80,lastNote, 0x0);
   Midi_Send(0x90,note,0x45);
   lastNote = note;
   while(button(BUTTON2) || button(BUTTON3));
 }
 //  send last note off
 if (button(BUTTON1) && lastNote > -1) {
   Midi_Send(0x80, lastNote, 0x0);
 }
 
 // send cc
 // only if the value has changed
 if (ccVal != lastCcVal) {
   Midi_Send(0xB0, 74, ccVal);
   lastCcVal = ccVal;
 }
}
void Midi_Send(byte cmd, byte data1, byte data2) {
 Serial.write(cmd);
 Serial.write(data1);
 Serial.write(data2);
}
char button(char button_num) {
 return (!(digitalRead(button_num)));
}

No comments:

Post a Comment