MIDI library + Capacitive sensor Library Problems

Status
Not open for further replies.

torin

Member
Helllo,

I'm having a strange issue with Capsense Library and the MIDI library running on a Teensy LC. My program utilizes 12 capacitive sensors that act as triggers to send MIDI notes. When I use my code using usbMIDI, I have no issues at all, everything runs smoothly and the sensor response is perfect. When i include the hardware MIDI library, however, it seems as if everything slows to a crawl. The serial monitor returns sensor readings every 7 seconds or so. Any idea what could be causing this? I've checked MIDI out and the cap sensors separately and they work perfectly, so I assume it must be something to do with how they are both being utilized in my code.
Thanks.

Here's my Code(also attached):

//Capsense Multi Sense + Glock code zombie for Teensy LC
//12 sensors with calib button and transposition knob
// transpose is disabled rn tho

#include <CapacitiveSensor.h>
#include <MIDI.h>

//Defining Sensors + Number of Sensors
CapacitiveSensor cs_0_2 = CapacitiveSensor(0,2);
CapacitiveSensor cs_3_4 = CapacitiveSensor(3,4);
CapacitiveSensor cs_5_6 = CapacitiveSensor(5,6);
CapacitiveSensor cs_7_8 = CapacitiveSensor(7,8);
CapacitiveSensor cs_9_10 = CapacitiveSensor(9,10);
CapacitiveSensor cs_11_12 = CapacitiveSensor(11,12);
CapacitiveSensor cs_13_14 = CapacitiveSensor(13,14);
CapacitiveSensor cs_15_16 = CapacitiveSensor(15,16);
CapacitiveSensor cs_17_18 = CapacitiveSensor(17,18);
CapacitiveSensor cs_19_20 = CapacitiveSensor(19,20);
CapacitiveSensor cs_21_22 = CapacitiveSensor(21,22);
CapacitiveSensor cs_24_25 = CapacitiveSensor(24,25);

const int numPins = 12;

//Set the pin to switch to calibration mode
int calibPin = 23;

//Set the pin for the transpose pot, create variables for mapping
int transposePin = 26;
int transposeVal = 0;
int transposeMap = 0;

//Global variables for smoothing functions
int total = 0;
int average = 0;
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;

//Threshold array
int triggerThreshold[numPins];

//Set threshold sensitivity
int calibVal = 200;

//Arrays to keep track of sent notes
int sentMidiNote[numPins];
int sentMidiChannel[numPins];
boolean noteSend[numPins];

//Debug mode for serial printing
boolean debug = true;

//This is just to make the serial printing cleaner
boolean calibSerial = true;


//MIDI variables, array of notes
int keyMidiNote[] = {1,2,3,4,5,6,7,8,9,10,11,12};
int currentMidiNote = 1;
int keyMidiChannel[] = {1,1,1,1,1,1,1,1,1,1,1,1};
int currentMidiChannel = 1;
int currentMidiVelocity = 108;

void setup() {
Serial.begin(9600);
MIDI.begin();

//Initialize readings for smoothing code
initReadings();

//Initialize MIDI sent note memory
initSentMidiNote();

//Initialize noteSend booleans
initNoteSend();

//Calibrate the trigger thresholds on reset
calibrateThresholds();

//For calibration switch
pinMode(calibPin, INPUT);
pinMode(transposePin, INPUT);

delay(500);
Serial.println("Setup Complete");
Serial.print("MIDI Channel: ");
Serial.println(currentMidiChannel);
}

void loop() {

//Check the switch for calibration mode
//*DO NOT TOUCH SENSORS IN THIS MODE*
while (digitalRead(calibPin) == HIGH) {
calibrateThresholds();
if (calibSerial == true){
Serial.println(" Calibrating...DO NOT TOUCH");
calibSerial = false;
}
Serial.println(" ...");
}
if (calibSerial == false){
Serial.println(" Calibration complete ");
}
calibSerial = true;

//Main loop things here

for(int i=0; i<numPins; i++){
//Reset the readIndex for smoothing
readIndex = 0;

//Get the total of 10 readings and store it in an array
total = totalFunc(i);

//Get the average of those 10 readings
average = averageFunc();

//Checks if the threshold is crossed, and if it is send a note
if (checkThreshold(i)) {
playNote(i);

}
}
}

void calibrateThresholds() {
for(int i=0; i<numPins; i++){
total = totalFunc(i);
average = averageFunc();
readIndex = 0;
triggerThreshold = average + calibVal;
}
}

void initReadings() {
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}

void initNoteSend() {
for(int i=0; i<numPins; i++){
noteSend = false;
}
}

void initSentMidiNote() {
for(int i=0; i<numPins; i++){
sentMidiNote = 0;
}
}

//Read Func
long csReadFunc (int _i){
long reading = 0;
if (_i == 0){
long reading = cs_0_2.capacitiveSensor(15);
return reading;
}
else if (_i == 1){
long reading = cs_3_4.capacitiveSensor(15);
return reading;
}
else if (_i == 2){
long reading = cs_5_6.capacitiveSensor(15);
return reading;
}
else if (_i == 3){
long reading = cs_7_8.capacitiveSensor(15);
return reading;
}
else if (_i == 4){
long reading = cs_9_10.capacitiveSensor(15);
return reading;
}
else if (_i == 5){
long reading = cs_11_12.capacitiveSensor(15);
return reading;
}
else if (_i == 6){
long reading = cs_13_14.capacitiveSensor(15);
return reading;
}
else if (_i == 7){
long reading = cs_15_16.capacitiveSensor(15);
return reading;
}
else if (_i == 8){
long reading = cs_17_18.capacitiveSensor(15);
return reading;
}
else if (_i == 9){
long reading = cs_19_20.capacitiveSensor(15);
return reading;
}
else if (_i == 10){
long reading = cs_21_22.capacitiveSensor(15);
return reading;
}
else if (_i == 11){
long reading = cs_24_25.capacitiveSensor(15);
return reading;
}
}

//Find the total...
long totalFunc (int _i){
int sum=0;
while (readIndex < numReadings){
// subtract the last reading:
// sum = sum - readings[readIndex];
// read from the sensor:
readings[readIndex] = csReadFunc(_i);
// add the reading to the total:
sum = sum + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
}
return sum;
//Serial.println(sum);
}

//FIND THE AVERAGE (STORED GLOBALLY)
int averageFunc (){
int averagecalc = 0;
//Calculate the average:
averagecalc = total / numReadings;

//Uncomment the serial print to get a constant stream of averages
Serial.println(averagecalc);

return averagecalc;
}

// PLAY A NOTE
void playNote(int _i) {
//Check if a note is already being sent to avoid retriggering
if(noteSend[_i] == false) {

//Transposing stuff
transposeVal = analogRead(transposePin);
transposeMap = 0; //map(transposeVal, 0, 1000, 0, 12);
//Serial.println(transposeMap);

currentMidiNote = keyMidiNote[_i] + transposeMap;
currentMidiChannel = keyMidiChannel[_i];
MIDI.sendNoteOn(currentMidiNote,currentMidiVelocity,currentMidiChannel);
sentMidiNote[_i] = currentMidiNote;
sentMidiChannel[_i] = currentMidiChannel;

//Flip the boolean to show a note is being sent
noteSend[_i] = true;

//Serial prints for debugging showing the sensor number and the sensor reading
if(debug) {
Serial.print(" Sensor ");
Serial.println(_i);
Serial.print(" Reading ");
Serial.println(average);
Serial.print(" MIDI Note ");
Serial.println(currentMidiNote);
Serial.print(" MIDI Channel ");
Serial.println(currentMidiChannel);
Serial.println();
}
}
}

boolean checkThreshold(int _i) {
//If the threshold is crossed, return true
if(average > triggerThreshold[_i]){
return true;
}

//Else return false and
else {
if(noteSend[_i] == true){
if(debug) {
Serial.print("NOTE OFF");
Serial.println();
}


//Cancel the midi note that was previously sent
MIDI.sendNoteOff(sentMidiNote[_i],0,sentMidiChannel[_i]);
noteSend[_i] = false;
}
return false;
}
}
 

Attachments

  • NANO1_0.ino
    7 KB · Views: 69
Last edited:
Status
Not open for further replies.
Back
Top