Hi there.
I am working on modifying a sketch from Bare Conductive to use 2 MPR121 capacitive touch sensors (the ones I have currently are the Adafruit version). They communicate via I2C and have separate addresses. I am a total amateur when it comes to Arduinos and I'm not sure how to get them to run together. I've got one MPR121 with the address of 0x5A and the other 0x5B. They have separate interrupt pins (0 and 1) but in the sketch, I can only get one to actually generate MIDI. The LEDs on both MPR121 breakouts respond and work but only one of them at a time will send MIDI. I've attached my modified code below. The original unmodified code can be found on the Bare Conductive github here.
Any feedback and/or help is greatly appreciated!
MIDI_MPR121_Post.ino (8.17 KB)
#include <MPR121.h>
#include <Wire.h>
#include "Midi_object.h"
#include "Compiler_Errors.h"
MIDIEvent e;
MPR121_t MPR121_2; // a second MPR121 object
#define numElectrodes 12
midi_object_t MIDIobjects[numElectrodes]; // create an array of MIDI objects to use (one for each electrode)
midi_object_t MIDIobjects_2[numElectrodes]; // another array for the second MPR121
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
MPR121.begin(0x5B);
MPR121.setInterruptPin(1);
MPR121_2.begin(0x5A); // a new MPR121 with a different address and interrupt pin
MPR121.setInterruptPin(0);
MIDIobjects[0].type = MIDI_NOTE;
MIDIobjects[0].noteNumber = 36;
MIDIobjects[0].touchThreshold = 40; // always make sure that the touch threshold is
MIDIobjects[0].releaseThreshold = 20; // larger than the release threshold
MIDIobjects[1].type = MIDI_NOTE;
MIDIobjects[1].noteNumber = 37;
MIDIobjects[1].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[1].releaseThreshold = 20;
MIDIobjects[2].type = MIDI_NOTE;
MIDIobjects[2].noteNumber = 38;
MIDIobjects[2].touchThreshold = 40;
MIDIobjects[2].releaseThreshold = 20;
MIDIobjects[3].type = MIDI_NOTE;
MIDIobjects[3].noteNumber = 39;
MIDIobjects[3].touchThreshold = 40;
MIDIobjects[3].releaseThreshold = 20;
MIDIobjects[4].type = MIDI_NOTE;
MIDIobjects[4].noteNumber = 40;
MIDIobjects[4].touchThreshold = 40; // note lower values here mean more sensitivity
MIDIobjects[4].releaseThreshold = 20; // i.e. trigger by proximity, not touch
MIDIobjects[5].type = MIDI_NOTE;
MIDIobjects[5].noteNumber = 41;
MIDIobjects[5].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[5].releaseThreshold = 20;
MIDIobjects[6].type = MIDI_NOTE;
MIDIobjects[6].noteNumber = 42;
MIDIobjects[6].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[6].releaseThreshold = 20;
MIDIobjects[7].type = MIDI_NOTE;
MIDIobjects[7].noteNumber = 43;
MIDIobjects[7].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[7].releaseThreshold = 20;
MIDIobjects[8].type = MIDI_NOTE;
MIDIobjects[8].noteNumber = 44;
MIDIobjects[8].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[8].releaseThreshold = 20;
MIDIobjects[9].type = MIDI_NOTE;
MIDIobjects[9].noteNumber = 45;
MIDIobjects[9].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[9].releaseThreshold = 20;
MIDIobjects[10].type = MIDI_NOTE;
MIDIobjects[10].noteNumber = 46;
MIDIobjects[10].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, proximity
MIDIobjects[10].releaseThreshold = 20;
MIDIobjects[11].type = MIDI_NOTE;
MIDIobjects[11].noteNumber = 47;
MIDIobjects[11].touchThreshold = 40; // higher values = less sensitivity = trigger by touch, not proximity
MIDIobjects[11].releaseThreshold = 20;
MIDIobjects_2[0].type = MIDI_NOTE;
MIDIobjects_2[0].noteNumber = 48;
MIDIobjects_2[0].touchThreshold = 40;
MIDIobjects_2[0].releaseThreshold = 20;
MIDIobjects_2[1].type = MIDI_NOTE;
MIDIobjects_2[1].noteNumber = 49;
MIDIobjects_2[1].touchThreshold = 40;
MIDIobjects_2[1].releaseThreshold = 20;
MIDIobjects_2[2].type = MIDI_NOTE;
MIDIobjects_2[2].noteNumber = 50;
MIDIobjects_2[2].touchThreshold = 40;
MIDIobjects_2[2].releaseThreshold = 20;
MIDIobjects_2[3].type = MIDI_NOTE;
MIDIobjects_2[3].noteNumber = 51;
MIDIobjects_2[3].touchThreshold = 40;
MIDIobjects_2[3].releaseThreshold = 20;
MIDIobjects_2[4].type = MIDI_NOTE;
MIDIobjects_2[4].noteNumber = 52;
MIDIobjects_2[4].touchThreshold = 40;
MIDIobjects_2[4].releaseThreshold = 20;
MIDIobjects_2[5].type = MIDI_NOTE;
MIDIobjects_2[5].noteNumber = 53;
MIDIobjects_2[5].touchThreshold = 40;
MIDIobjects_2[5].releaseThreshold = 20;
MIDIobjects_2[6].type = MIDI_NOTE;
MIDIobjects_2[6].noteNumber = 54;
MIDIobjects_2[6].touchThreshold = 40;
MIDIobjects_2[6].releaseThreshold = 20;
MIDIobjects_2[7].type = MIDI_NOTE;
MIDIobjects_2[7].noteNumber = 55;
MIDIobjects_2[7].touchThreshold = 40;
MIDIobjects_2[7].releaseThreshold = 20;
MIDIobjects_2[8].type = MIDI_NOTE;
MIDIobjects_2[8].noteNumber = 56;
MIDIobjects_2[8].touchThreshold = 40;
MIDIobjects_2[8].releaseThreshold = 20;
MIDIobjects_2[9].type = MIDI_NOTE;
MIDIobjects_2[9].noteNumber = 57;
MIDIobjects_2[9].touchThreshold = 40;
MIDIobjects_2[9].releaseThreshold = 20;
MIDIobjects_2[10].type = MIDI_NOTE;
MIDIobjects_2[10].noteNumber = 58;
MIDIobjects_2[10].touchThreshold = 40;
MIDIobjects_2[10].releaseThreshold = 20;
MIDIobjects_2[11].type = MIDI_NOTE;
MIDIobjects_2[11].noteNumber = 59;
MIDIobjects_2[11].touchThreshold = 40;
MIDIobjects_2[11].releaseThreshold = 20;
// set touch and release thresholds for electrodes that require it
for(int i=0; i<numElectrodes; i++){
if(MIDIobjects[i].type == MIDI_NOTE){
MPR121.setTouchThreshold(i, MIDIobjects[i].touchThreshold);
MPR121.setReleaseThreshold(i, MIDIobjects[i].releaseThreshold);
}
}
for(int i=0; i<numElectrodes; i++){
if(MIDIobjects_2[i].type == MIDI_NOTE){
MPR121_2.setTouchThreshold(i, MIDIobjects_2[i].touchThreshold);
MPR121_2.setReleaseThreshold(i, MIDIobjects_2[i].releaseThreshold);
}
}
// start with fresh data
MPR121.updateAll();
MPR121_2.updateAll();
}
void loop() {
// check note electrodes
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
for(int i=0; i<numElectrodes; i++){
if(MIDIobjects[i].type==MIDI_NOTE){ // if this is a note type object...
e.type = 0x08;
e.m2 = MIDIobjects[i].noteNumber; // set note number
e.m3 = 127; // maximum volume
}
if(MPR121.isNewTouch(i)){
// if we have a new touch, turn on the onboard LED and
// send a "note on" message with the appropriate note set
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);
}
}
}
}
if(MPR121_2.touchStatusChanged()){
MPR121_2.updateTouchData();
for(int i=0; i<numElectrodes; i++){
if(MIDIobjects_2[i].type==MIDI_NOTE){ // if this is a note type object...
e.type = 0x08;
e.m2 = MIDIobjects_2[i].noteNumber; // set note number
e.m3 = 127; // maximum volume
if(MPR121_2.isNewTouch(i)){
// if we have a new touch, turn on the onboard LED and
// send a "note on" message with the appropriate note set
digitalWrite(LED_BUILTIN, HIGH);
e.m1 = 0x90;
} else if(MPR121_2.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);
}
}
}
}
MPR121.updateFilteredData();
MPR121_2.updateFilteredData();
// flush USB buffer to ensure all notes are sent
MIDIUSB.flush();
delay(10); // 10ms delay to give the USB MIDI target time to catch up
}
Did you ever get this worked out? I'm interested in a solution for something similar I'm working on.