NibblesIsGod
New member
Hey y'all.
So to start, I really have no idea what I'm doing, I'm not a programmer by any means and I figured out what I have so far by a very long game of trial and error. I started with a code from a website about making your own MIDI controller, but that code sent the same CC message from every pot so I just messed around til I made it work. That being said, somehow I managed to make this do exactly what I want over usbMIDI, but I can't figure out how to make the teensy 2.0 send MIDI data from the TX pin to my 5 pin DIN.
Do I need to actually add code to have the teensy send MIDI from the TX pin or should it automatically send? Also, this page from the PJRC site is a bit confusing:
https://www.pjrc.com/teensy/td_libs_MIDI.html
The schematic and the example photo show pins 4 and 5 on the DIN in opposite positions.
Anyhow, here's my code. As I said before, I have no idea what the hell I am doing as far as writing code goes. Honestly, I'm pretty amazed I was able to get it actually working and I'm sure there are better ways to do this and that this code is way wrong, but if anyone could help me figure out how to make it transmit midi data to the TX pin I would be really grateful.
#include <Bounce.h>
// define how many pots are active up to number of available analog inputs
#define analogInputs 12
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;
// cc values for buttons
int cc_off = 0;
int cc_on = 65;
int cc_super = 127;
Bounce button0 = Bounce(0, 3);
Bounce button1 = Bounce(1, 3);
Bounce button2 = Bounce(2, 3);
Bounce button3 = Bounce(3, 3);
void setup() {
// MIDI rate
Serial.begin(31250);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}
void loop() {
// loop trough active inputs for knobs
{
// read current value at i-th input
inputAnalog[0] = analogRead(0);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[0] - iAlag[0]) > 7){
// calc the CC value based on the raw value
ccValue[0] = inputAnalog[0]/8;
// send the MIDI
usbMIDI.sendControlChange(21, ccValue[0], 1);
// set raw reading to lagged array for next comparison
iAlag[0] = inputAnalog[0];
}
inputAnalog[1] = analogRead(1);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[1] - iAlag[1]) > 7){
// calc the CC value based on the raw value
ccValue[1] = inputAnalog[1]/8;
// send the MIDI
usbMIDI.sendControlChange(22, ccValue[1], 1);
// set raw reading to lagged array for next comparison
iAlag[1] = inputAnalog[1];
}
inputAnalog[2] = analogRead(2);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[2] - iAlag[2]) > 7){
// calc the CC value based on the raw value
ccValue[2] = inputAnalog[2]/8;
// send the MIDI
usbMIDI.sendControlChange(25, ccValue[2], 1);
// set raw reading to lagged array for next comparison
iAlag[2] = inputAnalog[2];
}
inputAnalog[3] = analogRead(3);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[3] - iAlag[3]) > 7){
// calc the CC value based on the raw value
ccValue[3] = inputAnalog[3]/8;
// send the MIDI
usbMIDI.sendControlChange(26, ccValue[3], 1);
// set raw reading to lagged array for next comparison
iAlag[3] = inputAnalog[3];
}
inputAnalog[4] = analogRead(4);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[4] - iAlag[4]) > 7){
// calc the CC value based on the raw value
ccValue[4] = inputAnalog[4]/8;
// send the MIDI
usbMIDI.sendControlChange(27, ccValue[4], 1);
// set raw reading to lagged array for next comparison
iAlag[4] = inputAnalog[4];
}
inputAnalog[5] = analogRead(5);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[5] - iAlag[5]) > 7){
// calc the CC value based on the raw value
ccValue[5] = inputAnalog[5]/8;
// send the MIDI
usbMIDI.sendControlChange(28, ccValue[5], 1);
// set raw reading to lagged array for next comparison
iAlag[5] = inputAnalog[5];
}
inputAnalog[6] = analogRead(6);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[6] - iAlag[6]) > 7){
// calc the CC value based on the raw value
ccValue[6] = inputAnalog[6]/8;
// send the MIDI
usbMIDI.sendControlChange(15, ccValue[6], 1);
// set raw reading to lagged array for next comparison
iAlag[6] = inputAnalog[6];
}
inputAnalog[7] = analogRead(7);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[7] - iAlag[7]) > 7){
// calc the CC value based on the raw value
ccValue[7] = inputAnalog[7]/8;
// send the MIDI
usbMIDI.sendControlChange(34, ccValue[7], 1);
// set raw reading to lagged array for next comparison
iAlag[7] = inputAnalog[7];
}
inputAnalog[8] = analogRead(8);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[8] - iAlag[8]) > 7){
// calc the CC value based on the raw value
ccValue[8] = inputAnalog[8]/8;
// send the MIDI
usbMIDI.sendControlChange(30, ccValue[8], 1);
// set raw reading to lagged array for next comparison
iAlag[8] = inputAnalog[8];
}
inputAnalog[9] = analogRead(9);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[9] - iAlag[9]) > 7){
// calc the CC value based on the raw value
ccValue[9] = inputAnalog[9]/8;
// send the MIDI
usbMIDI.sendControlChange(31, ccValue[9], 1);
// set raw reading to lagged array for next comparison
iAlag[9] = inputAnalog[9];
}
inputAnalog[10] = analogRead(10);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[10] - iAlag[10]) > 7){
// calc the CC value based on the raw value
ccValue[10] = inputAnalog[10]/8;
// send the MIDI
usbMIDI.sendControlChange(32, ccValue[10], 1);
// set raw reading to lagged array for next comparison
iAlag[10] = inputAnalog[10];
}
inputAnalog[11] = analogRead(11);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[11] - iAlag[11]) > 7){
// calc the CC value based on the raw value
ccValue[11] = inputAnalog[11]/8;
// send the MIDI
usbMIDI.sendControlChange(33, ccValue[11], 1);
// set raw reading to lagged array for next comparison
iAlag[11] = inputAnalog[11];
}
delay(5); // limits MIDI messages to reasonable number
}
// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();
if (button0.fallingEdge())
{
usbMIDI.sendProgramChange(95, 1);
}
if (button1.fallingEdge())
{
usbMIDI.sendProgramChange(96, 1);
}
if (button2.fallingEdge())
{
usbMIDI.sendProgramChange(97, 1);
}
if (button3.fallingEdge())
{
usbMIDI.sendProgramChange(98, 1);
}
}
So to start, I really have no idea what I'm doing, I'm not a programmer by any means and I figured out what I have so far by a very long game of trial and error. I started with a code from a website about making your own MIDI controller, but that code sent the same CC message from every pot so I just messed around til I made it work. That being said, somehow I managed to make this do exactly what I want over usbMIDI, but I can't figure out how to make the teensy 2.0 send MIDI data from the TX pin to my 5 pin DIN.
Do I need to actually add code to have the teensy send MIDI from the TX pin or should it automatically send? Also, this page from the PJRC site is a bit confusing:
https://www.pjrc.com/teensy/td_libs_MIDI.html
The schematic and the example photo show pins 4 and 5 on the DIN in opposite positions.
Anyhow, here's my code. As I said before, I have no idea what the hell I am doing as far as writing code goes. Honestly, I'm pretty amazed I was able to get it actually working and I'm sure there are better ways to do this and that this code is way wrong, but if anyone could help me figure out how to make it transmit midi data to the TX pin I would be really grateful.
#include <Bounce.h>
// define how many pots are active up to number of available analog inputs
#define analogInputs 12
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;
// cc values for buttons
int cc_off = 0;
int cc_on = 65;
int cc_super = 127;
Bounce button0 = Bounce(0, 3);
Bounce button1 = Bounce(1, 3);
Bounce button2 = Bounce(2, 3);
Bounce button3 = Bounce(3, 3);
void setup() {
// MIDI rate
Serial.begin(31250);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}
void loop() {
// loop trough active inputs for knobs
{
// read current value at i-th input
inputAnalog[0] = analogRead(0);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[0] - iAlag[0]) > 7){
// calc the CC value based on the raw value
ccValue[0] = inputAnalog[0]/8;
// send the MIDI
usbMIDI.sendControlChange(21, ccValue[0], 1);
// set raw reading to lagged array for next comparison
iAlag[0] = inputAnalog[0];
}
inputAnalog[1] = analogRead(1);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[1] - iAlag[1]) > 7){
// calc the CC value based on the raw value
ccValue[1] = inputAnalog[1]/8;
// send the MIDI
usbMIDI.sendControlChange(22, ccValue[1], 1);
// set raw reading to lagged array for next comparison
iAlag[1] = inputAnalog[1];
}
inputAnalog[2] = analogRead(2);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[2] - iAlag[2]) > 7){
// calc the CC value based on the raw value
ccValue[2] = inputAnalog[2]/8;
// send the MIDI
usbMIDI.sendControlChange(25, ccValue[2], 1);
// set raw reading to lagged array for next comparison
iAlag[2] = inputAnalog[2];
}
inputAnalog[3] = analogRead(3);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[3] - iAlag[3]) > 7){
// calc the CC value based on the raw value
ccValue[3] = inputAnalog[3]/8;
// send the MIDI
usbMIDI.sendControlChange(26, ccValue[3], 1);
// set raw reading to lagged array for next comparison
iAlag[3] = inputAnalog[3];
}
inputAnalog[4] = analogRead(4);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[4] - iAlag[4]) > 7){
// calc the CC value based on the raw value
ccValue[4] = inputAnalog[4]/8;
// send the MIDI
usbMIDI.sendControlChange(27, ccValue[4], 1);
// set raw reading to lagged array for next comparison
iAlag[4] = inputAnalog[4];
}
inputAnalog[5] = analogRead(5);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[5] - iAlag[5]) > 7){
// calc the CC value based on the raw value
ccValue[5] = inputAnalog[5]/8;
// send the MIDI
usbMIDI.sendControlChange(28, ccValue[5], 1);
// set raw reading to lagged array for next comparison
iAlag[5] = inputAnalog[5];
}
inputAnalog[6] = analogRead(6);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[6] - iAlag[6]) > 7){
// calc the CC value based on the raw value
ccValue[6] = inputAnalog[6]/8;
// send the MIDI
usbMIDI.sendControlChange(15, ccValue[6], 1);
// set raw reading to lagged array for next comparison
iAlag[6] = inputAnalog[6];
}
inputAnalog[7] = analogRead(7);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[7] - iAlag[7]) > 7){
// calc the CC value based on the raw value
ccValue[7] = inputAnalog[7]/8;
// send the MIDI
usbMIDI.sendControlChange(34, ccValue[7], 1);
// set raw reading to lagged array for next comparison
iAlag[7] = inputAnalog[7];
}
inputAnalog[8] = analogRead(8);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[8] - iAlag[8]) > 7){
// calc the CC value based on the raw value
ccValue[8] = inputAnalog[8]/8;
// send the MIDI
usbMIDI.sendControlChange(30, ccValue[8], 1);
// set raw reading to lagged array for next comparison
iAlag[8] = inputAnalog[8];
}
inputAnalog[9] = analogRead(9);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[9] - iAlag[9]) > 7){
// calc the CC value based on the raw value
ccValue[9] = inputAnalog[9]/8;
// send the MIDI
usbMIDI.sendControlChange(31, ccValue[9], 1);
// set raw reading to lagged array for next comparison
iAlag[9] = inputAnalog[9];
}
inputAnalog[10] = analogRead(10);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[10] - iAlag[10]) > 7){
// calc the CC value based on the raw value
ccValue[10] = inputAnalog[10]/8;
// send the MIDI
usbMIDI.sendControlChange(32, ccValue[10], 1);
// set raw reading to lagged array for next comparison
iAlag[10] = inputAnalog[10];
}
inputAnalog[11] = analogRead(11);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[11] - iAlag[11]) > 7){
// calc the CC value based on the raw value
ccValue[11] = inputAnalog[11]/8;
// send the MIDI
usbMIDI.sendControlChange(33, ccValue[11], 1);
// set raw reading to lagged array for next comparison
iAlag[11] = inputAnalog[11];
}
delay(5); // limits MIDI messages to reasonable number
}
// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();
if (button0.fallingEdge())
{
usbMIDI.sendProgramChange(95, 1);
}
if (button1.fallingEdge())
{
usbMIDI.sendProgramChange(96, 1);
}
if (button2.fallingEdge())
{
usbMIDI.sendProgramChange(97, 1);
}
if (button3.fallingEdge())
{
usbMIDI.sendProgramChange(98, 1);
}
}