Using MIDI Library and Capacitive Sensing to Control Guitar Pedal

Hello, Arduino community. Total newbie with a little bit of prior programming (Java, C#, VBA) experience here.

I've been working on some code for my Bare Conductive Touch Board with an Olimex MIDI shield to send a simple MIDI CC message to my Digitech Whammy guitar effects pedal based on the proximity to a capacitive sensor. Basically, it will be controlled like a Theremin. I managed to compile my code with no errors, but (no surprise) the code doesn't actually do anything when tested with the Touch Board and the pedal. I borrowed somewhat heavily from the MIDI Theremin code found on the Bare Conductive website, which actually works when I upload it to the Touch Board.

For those not familiar with it, the Touch Board is more or less (for this application, anyway) an Arduino Leonardo with a built-in MPR121 capacitive touch controller. More information can be found here.

Also for reference, the MIDI shield I am working with can be found here.

To reiterate, the example MIDI Theremin code works flawlessly when uploaded to the board, and my code does nothing. There is even an led on the MIDI shield that lights up with the example code but not with mine. There is obviously something missing from mine, but I'm not sure what. And one important difference to note between mine and the example code is that I am trying to send MIDI data over hardware serial, but the example code uses SoftwareSerial since it doesn't need to actually send any data out of the board itself. Before anyone asks, yes I have soldered the real-time MIDI mode pads on the Touch Board, and yes, I have set the correct channel on the Whammy pedal.

Should I try to rewrite my code without the MIDI Library? Should I be using SoftwareSerial in my code for some reason? Any insight would be greatly appreciated! Thank you in advance.

midi_theremin.ino (18.5 KB)

Theremin_Whammy.ino (3.55 KB)

So you don't have to download my code, here it is:

//This sketch uses the Bare Conductive Touch Board and a Theremin antenna to send and modulate a MIDI
//Continuous Controller message on channel 16. This is originally intended to control the virtual
//expression pedal position of a Digitech Whammy pedal, but it could easily be used or modified for
//a number of other applications.

//included libraries
#include <MPR121.h>
#include <MIDI.h>

//Define fscale function
float fscale( float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve)
{
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;

// condition curve parameter
// limit range

if (curve > 10) curve = 10;
if (curve < -10) curve = -10;

curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function

// Check for out of range inputValues
if (inputValue < originalMin)
{
inputValue = originalMin;
}
if (inputValue > originalMax)
{
inputValue = originalMax;
}

// Zero Refference the values
OriginalRange = originalMax - originalMin;

if (newEnd > newBegin)
{
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}

zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float

// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}

if (invFlag == 0){
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;

}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}

return rangedValue;
}

//Create instance of MIDI library
MIDI_CREATE_INSTANCE(HardwareSerial,Serial1,realMidi);

//Touch Board pin setup
byte pin_CC = 0; //pin to control MIDI CC message value

//CC setup
byte min_CC = 0; //lowest CC message value
byte max_CC = 127; //highest CC message value
byte direction_CC = 0; //0 = highest CC with touch, 1 = lowest CC with touch
int nonlinear_CC = -10; //fscale 'curve' parameter, 0 is linear, -10 is max change at large distance

//Proximity initialization
int level_CC = 0;
int level_CC_old = 0;
int CC = 0;
int CC_old = 0;
int min_level_CC = 1000; //dummy value
int max_level_CC = 0; //dummy value

void setup()
{
realMidi.begin(MIDI_CHANNEL_OMNI);

//pin 4 is the MPR121 interrupt on the Bare Conductive Touch Board
MPR121.setInterruptPin(4);

//initial data update
MPR121.updateTouchData();
}

void loop()
{
MPR121.updateAll();

//MIDI CC code

level_CC_old = level_CC;
level_CC = MPR121.getFilteredData(pin_CC);

if (level_CC != level_CC_old)
{
//dynamically set up maxes and mins for level
if (level_CC > max_level_CC)
{
max_level_CC = level_CC;
}
if (level_CC < min_level_CC)
{
min_level_CC = level_CC;
}

//set CC
else
{
if (direction_CC == 0)
{
CC = fscale(min_level_CC,max_level_CC,max_CC,min_CC,level_CC,nonlinear_CC);
}
else if (direction_CC == 1)
{
CC = fscale(min_level_CC,max_level_CC,min_CC,max_CC,level_CC,nonlinear_CC);
}
if (CC != CC_old)
{
realMidi.sendControlChange(12,CC,16);
CC_old = CC;
}
}
}
}