I'm guessing there's something with the code that it needs to be detected via serial monitor for the code to run, and I would like to figure out how to omit that from the code so that it can work as a MIDI usb device when plugged into my computer with the Arduino IDE closed.
Thanks in advance, I'm so stumped and would love to figure this out.
Code: Select all
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor
Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/
These sensors use I2C communicate, at least 2 pins are required
to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**********************************************************/
//sketch in progress. trying to read i2c from mpr121 and send midi message from teensy in response to which pin is touched.
//important links: teensy wire library: https://www.pjrc.com/teensy/td_libs_Wire.html
// teensy midi library: https://www.pjrc.com/teensy/td_midi.html
//current issues: doesn't work once unplugged and plugged back in. only works when connected to the Arduino serial monitor
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
const uint8_t numElectrodes = 12; //added by drc
const uint8_t notes[numElectrodes] = {36, 38, 40, 43, 45, 47, 48, 50, 52, 55, 57, 60}; //added by drc
int channel = 1; //added by drc
void setup() {
Wire.begin(0x5A); //added by drc
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); //added by drc
Wire.setSDA(18); //use a 4.7k pullup resistor //added by drc
Wire.setSCL(19); //use a 4.7k pullup resistor //added by drc
while (!Serial) { // adding a delay to avoid issues seen in other forum posts.
delay(10);
}
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop() {
// Get the currently touched pads
currtouched = cap.touched();
for (uint8_t i=0; i<numElectrodes; i++) { //changed i<0 to i<numElectrodes
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
digitalWrite (LED_BUILTIN, HIGH); //added by drc
usbMIDI.sendNoteOn(notes[i], 127, channel); // added by drc
Serial.print(i); Serial.println(" touched");
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
digitalWrite (LED_BUILTIN, LOW);
usbMIDI.sendNoteOff(notes[i], 127, channel); // maximum velocity
Serial.print(i); Serial.println(" released");
}
}
// reset our state
lasttouched = currtouched;
// comment out this line for detailed data from the sensor!
return;
// debugging info, what
Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
Serial.print("Filt: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.filteredData(i)); Serial.print("\t");
}
Serial.println();
Serial.print("Base: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.baselineData(i)); Serial.print("\t");
}
Serial.println();
// put a delay so it isn't overwhelming
delay(100);
}
/*
void noteOn(uint8_t channel, uint8_t pitch, uint8_t velocity) {
usbMIDI.sendNoteOn(notes[numElectrodes], velocity, channel); //notes changed to [numElectrodes] by drc
}
void noteOff(uint8_t channel, uint8_t pitch, uint8_t velocity) {
usbMIDI.sendNoteOff(notes[numElectrodes], velocity, channel);
}
*/