Hi,
I made a midi controller with a Teensy 3.6. It's pretty cool and it totally works. However, in able to play it like a real instrument I want it to read analog data as fast as possible.
I made a similar instrument with an Arduino Mega and I put a 4 millisecond delay in it, and it feels pretty fast.
In the code I am using for the Teensy, which I altered from the USB Midi Analong Control Change example, I think it is reading the analong inputs every 20 milliseconds and I can definitely feel the lag. It's reading after a certain amount of elapsed milliseconds, and I think that's 20 in my code currently.
My question is how small of an amount of milliseconds is a safe amount to read the analog data? I know the Teensy's speed is way faster than a Arduino Mega, so can I make it 2 milliseconds for example? Or can I do away with reading it after a certain amount of milliseconds entirely and let it go as fast as it can?
I have 13 analog inputs in this code and 17 digital inputs.
Thank You, Nick
I made a midi controller with a Teensy 3.6. It's pretty cool and it totally works. However, in able to play it like a real instrument I want it to read analog data as fast as possible.
I made a similar instrument with an Arduino Mega and I put a 4 millisecond delay in it, and it feels pretty fast.
In the code I am using for the Teensy, which I altered from the USB Midi Analong Control Change example, I think it is reading the analong inputs every 20 milliseconds and I can definitely feel the lag. It's reading after a certain amount of elapsed milliseconds, and I think that's 20 in my code currently.
My question is how small of an amount of milliseconds is a safe amount to read the analog data? I know the Teensy's speed is way faster than a Arduino Mega, so can I make it 2 milliseconds for example? Or can I do away with reading it after a certain amount of milliseconds entirely and let it go as fast as it can?
I have 13 analog inputs in this code and 17 digital inputs.
Thank You, Nick
Code:
/* USB MIDI AnalogControlChange Example
You must select MIDI from the "Tools > USB Type" menu
http://www.pjrc.com/teensy/td_midi.html
This example code is in the public domain.
*/
#include <Bounce.h>
// the MIDI channel number to send messages
const int channel = 1;
// the MIDI continuous controller for each analog input
const int controllerA0 = 1; // control change 1, ribbon
const int controllerA1 = 2; //control change 2
const int controllerA2 = 3; //control change 3
const int controllerA3 = 4; // control change 4
const int controllerA4 = 5; // control change 5
const int controllerA5 = 6; // control change 6
const int controllerA6 = 7; // control change 7
const int controllerA7 = 8; // control change 8
const int controllerA8 = 9; // control change 9 last ribbon
const int controllerA9 = 10; // control change 10 joystick
const int controllerA10 = 11; // joystick
const int controllerA11 = 12; // control change 12
const int controllerA12 = 13; // control change 13 joystick
const int controllerD14 = 14; // toggle, control change 14
const int controllerD15 = 15; // Button, control change 15
const int controllerD16 = 16; //
const int controllerD17 = 17;
const int controllerD18 = 18; //
const int controllerD19 = 19;
const int controllerD20 = 20;
const int controllerD21 = 21;
const int controllerD22 = 22;
const int controllerD23 = 23;
const int controllerD24 = 24;
const int controllerD25 = 25;
const int controllerD26 = 26;
const int controllerD27 = 27;
const int controllerD28 = 28;
const int controllerD29 = 29;
const int controllerD30 = 30;
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5); // which is appropriate for good
Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 5); // to rapid touch, you can
Bounce button7 = Bounce(7, 5); // increase this time.
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);
Bounce button12 = Bounce(12, 5); //
Bounce button24 = Bounce(24, 5);
Bounce button25 = Bounce(25, 5);
Bounce button26 = Bounce(26, 5);
Bounce button27 = Bounce(27, 5);
void setup() {
// wire 3.3V to AREF for 3.3V reference
analogReference(DEFAULT);
pinMode(0, INPUT_PULLUP); // for button swithces
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);
pinMode(12, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
}
// store previously sent values, to detect changes
int previousA0 = -1;
int previousA1 = -1;
int previousA2 = -1;
int previousA3 = -1;
int previousA4 = -1;
int previousA5 = -1;
int previousA6 = -1;
int previousA7 = -1;
int previousA8 = -1;
int previousA9 = -1;
int previousA10 = -1;
int previousA11 = -1;
int previousA12 = -1;
elapsedMillis msec = 0;
void loop() {
{
button0.update(); // this button code works, goes from 0 to 127 when pressed
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button24.update();
button25.update();
button26.update();
button27.update();
if (button0.fallingEdge()) { // this deals with reading the button swithces
usbMIDI.sendControlChange(controllerD14, 127, channel);
}
if (button0.risingEdge()) {
usbMIDI.sendControlChange(controllerD14, 0, channel);
}
if (button1.fallingEdge()) {
usbMIDI.sendControlChange(controllerD15, 127, channel);
}
if (button1.risingEdge()) {
usbMIDI.sendControlChange(controllerD15, 0, channel);
}
if (button2.fallingEdge()) {
usbMIDI.sendControlChange(controllerD16, 127, channel);
}
if (button2.risingEdge()) {
usbMIDI.sendControlChange(controllerD16, 0, channel);
}
if (button3.fallingEdge()) {
usbMIDI.sendControlChange(controllerD17, 127, channel);
}
if (button3.risingEdge()) {
usbMIDI.sendControlChange(controllerD17, 0, channel);
}
if (button4.fallingEdge()) {
usbMIDI.sendControlChange(controllerD18, 127, channel);
}
if (button4.risingEdge()) {
usbMIDI.sendControlChange(controllerD18, 0, channel);
}
if (button5.fallingEdge()) {
usbMIDI.sendControlChange(controllerD19, 127, channel);
}
if (button5.risingEdge()) {
usbMIDI.sendControlChange(controllerD19, 0, channel);
}
if (button6.fallingEdge()) {
usbMIDI.sendControlChange(controllerD20, 127, channel);
}
if (button6.risingEdge()) {
usbMIDI.sendControlChange(controllerD20, 0, channel);
}
if (button7.fallingEdge()) {
usbMIDI.sendControlChange(controllerD21, 127, channel);
}
if (button7.risingEdge()) {
usbMIDI.sendControlChange(controllerD21, 0, channel);
}
if (button8.fallingEdge()) {
usbMIDI.sendControlChange(controllerD22, 127, channel);
}
if (button8.risingEdge()) {
usbMIDI.sendControlChange(controllerD22, 0, channel);
}
if (button9.fallingEdge()) {
usbMIDI.sendControlChange(controllerD23, 127, channel);
}
if (button9.risingEdge()) {
usbMIDI.sendControlChange(controllerD23, 0, channel);
}
if (button10.fallingEdge()) {
usbMIDI.sendControlChange(controllerD24, 127, channel);
}
if (button10.risingEdge()) {
usbMIDI.sendControlChange(controllerD24, 0, channel);
}
if (button11.fallingEdge()) {
usbMIDI.sendControlChange(controllerD25, 127, channel);
}
if (button11.risingEdge()) {
usbMIDI.sendControlChange(controllerD25, 0, channel);
}
if (button12.fallingEdge()) {
usbMIDI.sendControlChange(controllerD26, 127, channel);
}
if (button12.risingEdge()) {
usbMIDI.sendControlChange(controllerD26, 0, channel);
}
if (button24.fallingEdge()) {
usbMIDI.sendControlChange(controllerD27, 127, channel);
}
if (button24.risingEdge()) {
usbMIDI.sendControlChange(controllerD27, 0, channel);
}
if (button25.fallingEdge()) {
usbMIDI.sendControlChange(controllerD28, 127, channel);
}
if (button25.risingEdge()) {
usbMIDI.sendControlChange(controllerD28, 0, channel);
}
if (button26.fallingEdge()) {
usbMIDI.sendControlChange(controllerD29, 127, channel);
}
if (button26.risingEdge()) {
usbMIDI.sendControlChange(controllerD29, 0, channel);
}
if (button27.fallingEdge()) {
usbMIDI.sendControlChange(controllerD30, 127, channel);
}
if (button27.risingEdge()) {
usbMIDI.sendControlChange(controllerD30, 0, channel);
}
}
// only check the analog inputs 50 times per second,
// to prevent a flood of MIDI messages
if (msec >= 20) { //
msec = 0;
int n0 = map(analogRead(A0), 0, 1023, 0, 127);
int n1 = map(analogRead(A1), 0, 1023, 0, 127);
int n2 = map(analogRead(A2), 0, 1023, 0, 127);
int n3 = map(analogRead(A3), 0, 1023, 0, 127);
int n4 = map(analogRead(A4), 0, 1023, 0, 127);
int n5 = map(analogRead(A5), 0, 1023, 0, 127);
int n6 = map(analogRead(A6), 0, 1023, 0, 127);
int n7 = map(analogRead(A7), 0, 1023, 0, 127);
int n8 = map(analogRead(A8), 0, 1023, 0, 127);
int n9 = map(analogRead(A9), 0, 1023, 0, 127);
int n10 = map(analogRead(A14), 0, 1023, 0, 127);
int n11 = map(analogRead(A15), 0, 1023, 0, 127);
int n12 = map(analogRead(A16), 0, 1023, 0, 127);
// only transmit MIDI messages if analog input changed
if (n0 != previousA0) {
previousA0 = n0;
if (n0 < 5) n0 = 0;
usbMIDI.sendControlChange(controllerA0, n0, channel);
}
if (n1 != previousA1) {
previousA1 = n1;
if (n1 < 5) n1 = 0;
usbMIDI.sendControlChange(controllerA1, n1, channel);
}
if (n2 != previousA2) {
previousA2 = n2;
if (n2 < 5) n2 = 0;
usbMIDI.sendControlChange(controllerA2, n2, channel);
}
if (n3 != previousA3) {
previousA3 = n3;
if (n3 < 5) n3 = 0;
usbMIDI.sendControlChange(controllerA3, n3, channel);
}
if (n4 != previousA4) {
previousA4 = n4;
if (n4 < 5) n4 = 0;
usbMIDI.sendControlChange(controllerA4, n4, channel);
}
if (n5 != previousA5) {
previousA5 = n5;
if (n5 < 5) n5 = 0;
usbMIDI.sendControlChange(controllerA5, n5, channel);
}
if (n6 != previousA6) {
previousA6 = n6;
if (n6 < 5) n6 = 0;
usbMIDI.sendControlChange(controllerA6, n6, channel);
}
if (n7 != previousA7) {
previousA7 = n7;
if (n7 < 5) n7 = 0;
usbMIDI.sendControlChange(controllerA7, n7, channel);
}
if (n8 != previousA8) {
previousA8 = n8;
if (n8 < 5) n8 = 0;
usbMIDI.sendControlChange(controllerA8, n8, channel);
}
if (n9 != previousA9) {
previousA9 = n9;
if (n9 < 3) n9 = 0;
usbMIDI.sendControlChange(controllerA9, n9, channel);
}
if (n10 != previousA10) {
previousA10 = n10;
if (n10 < 3) n10 = 0;
usbMIDI.sendControlChange(controllerA10, n10, channel);
}
if (n11 != previousA11) {
previousA11 = n11;
if (n11 < 3) n11 = 0;
usbMIDI.sendControlChange(controllerA11, n11, channel);
}
if (n12 != previousA12) {
previousA12 = n12;
if (n12 < 3) n12 = 0;
usbMIDI.sendControlChange(controllerA12, n12, channel);
}
}
// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read()) {
// ignore incoming messages
}
}