Please help fill in the gaps with my Proximity and MIDI Interface Code

Hi,

Recently I was introduced to touch point technology. I’m a beginner, so I purchased a Bare Conductive Touchboard (Arduino). Touch an object >> hear a sound. Great. Very easy to set up and upload code.

First, I uploaded the Proximity Code (BELOW) and it worked perfect. I could hover my hand above the touch point and it would trigger a sound.

Next, I uploaded the MIDI Interface Code (BELOW) and it works great. Unfortunately, the MIDI code cancelled out the Proximity Code.

Can someone please give me the full code to handle both Proximity with MIDI Interface?

NOTE: I noticed, when posting this message, a particular code line shows MPR121.setTouchThreshold(8); – The threshold is EIGHT, not an emoji.

See this video sample (first video):

http://www.bareconductive.com/make/the-amazing-touch-board-machine/

------------------------------------------------------------------------------
------------------------------------------------------------------------------
PROXIMITY CODE:
------------------------------------------------------------------------------
------------------------------------------------------------------------------

// compiler error handling
#include “Compiler_Errors.h”

// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4

// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;

// touch behaviour definitions
#define firstPin 0
#define lastPin 11

// sd card instantiation
SdFat sd;

// define LED_BUILTIN for older versions of Arduino
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif

void setup(){
Serial.begin(57600);

pinMode(LED_BUILTIN, OUTPUT);

//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println(“Bare Conductive Proximity MP3 player”);

if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();

if(!MPR121.begin(MPR121_ADDR)) Serial.println(“error setting up MPR121”);
MPR121.setInterruptPin(MPR121_INT);

// Changes from Touch MP3

// this is the touch threshold - setting it low makes it more like a proximity trigger
// default value is 40 for touch
MPR121.setTouchThreshold(8);

// this is the release threshold - must ALWAYS be smaller than the touch threshold
// default value is 20 for touch
MPR121.setReleaseThreshold(4);

result = MP3player.begin();
MP3player.setVolume(10,10);

if(result != 0) {
Serial.print(“Error code: “);
Serial.print(result);
Serial.println(” when trying to start MP3 player”);
}

}

void loop(){
readTouchInputs();
}

void readTouchInputs(){
if(MPR121.touchStatusChanged()){

MPR121.updateTouchData();

// only make an action if we have one or fewer pins touched
// ignore multiple touches

if(MPR121.getNumTouches()<=1){
for (int i=0; i < 12; i++){ // Check which electrodes were pressed
if(MPR121.isNewTouch(i)){

//pin i was just touched
Serial.print(“pin “);
Serial.print(i);
Serial.println(” was just touched”);
digitalWrite(LED_BUILTIN, HIGH);

if(i<=lastPin && i>=firstPin){
if(MP3player.isPlaying()){
if(lastPlayed==i){
// if we’re already playing the requested track, stop it
MP3player.stopTrack();
Serial.print("stopping track ");
Serial.println(i-firstPin);
} else {
// if we’re already playing a different track, stop that
// one and play the newly requested one
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);

// don’t forget to update lastPlayed - without it we don’t
// have a history
lastPlayed = i;
}
} else {
// if we’re playing nothing, play the requested track
// and update lastplayed
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
}else{
if(MPR121.isNewRelease(i)){
Serial.print(“pin “);
Serial.print(i);
Serial.println(” is no longer being touched”);
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}
}
}

------------------------------------------------------------------------------
------------------------------------------------------------------------------
MIDI INTERFACE CODE:
------------------------------------------------------------------------------
------------------------------------------------------------------------------

#include <MPR121.h>
#include <Wire.h>
MIDIEvent e;

#define numElectrodes 12

void setup() {
MPR121.begin(0x5C);
MPR121.setInterruptPin(4);
MPR121.updateTouchData();
e.type = 0x08;
e.m3 = 127; // maximum volume

pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
for(int i=0; i<numElectrodes; i++){

// MIDI note mapping from electrode number to MIDI note
e.m2 = 48 + numElectrodes - 1 - i;

if(MPR121.isNewTouch(i)){
// if we have a new touch, turn on the onboard LED and
// send a “note on” message
digitalWrite(LED_BUILTIN, HIGH);
e.m1 = 0x90;
} else if(MPR121.isNewRelease(i)){
// if we have a new release, turn off the onboard LED and
// send a “note off” message
digitalWrite(LED_BUILTIN, LOW);
e.m1 = 0x80;
} else {
// else set a flag to do nothing…
e.m1 = 0x00;
}
// only send a USB MIDI message if we need to
if(e.m1 != 0x00){
MIDIUSB.write(e);
}
}
// flush USB buffer to ensure all notes are sent
MIDIUSB.flush();
}
}

Unless you can get some one to do the work for you, you will have to learn enough about the code you are using to combine the two scripts into one. Then when you load the modified script, it will (maybe) work with both the Proximity and the MIDI, depending on what you want.

You need to be more clear what your ultimate plan is, not just having them work at the same time.

FIXED! It was VERY simple. Just as I explained above. A simple marriage of the two codes was only divided by two lines.

From the Proximity code, copy the following two lines and place them, appropriately into the Midi Interface code.

In void setup() add

MPR121.setTouchThreshold(x); MPR121.setReleaseThreshold(y);

after MPR121.begin();

NOTE: Where x and y are your touch and release thresholds respectively. The Touch threshold must ALWAYS be higher than the release threshold, and lower values are more sensitive - for example you could use MPR121.setTouchThreshold(4); and MPR121.setReleaseThreshold(2); - also, never set either to 0 - their absolute minimum value must be 1.