Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.76 KB | None | 0 0
  1. // Basic MIDI Controller code for reading all of the Arduino's digital and analogue inputs
  2. // and sending them as MIDI messages to the host PC.
  3. //
  4. // Authors: Michael Balzer
  5. // Teensy USB-MIDI edit: Tim Crawford
  6. //
  7. // Revision History:
  8. // Date | Author | Change
  9. // ---------------------------------------------------
  10. // 2011-02-22 | MSB | Initial Release
  11. // 2011-03-01 | MSB | Updated MIDI output to send same MIDI signals as official MIDI Fighter
  12. // | | Reduced debounce length from 5ms to 2ms
  13. // 2011-03-14 | MSB | Modified analogue input logic so only pins moved within the timer
  14. // | | period are updated, not all of them.
  15. // | | Experimental code added for higher speed (but less accurate) analogue reads
  16. // | | Reduced analogue timer length from 1000ms to 250ms
  17. // 2011-03-21 | MSB | Removed TimerOne library. Each analogue pin now maintains its own time
  18. // | | since it was last moved, rather than one timer for all pins. This stops
  19. // | | sending jittery movements on analogue inputs which haven't been touched.
  20. // 2011-04-11 | TC | Teensy USB code added.
  21. // | MSB | Updated with #defines for Arduino Mega and Teensy USB for easy compilation
  22. // 2011-10-23 | MSB | Added default #defines for Teensy 2.0 and Teensy++ 2.0 digital pins
  23. // | | Removed #defines for Teensy 1.0 as usbMIDI is not supported
  24. // 2012-01-20 | MSB | Updated to support Arduino 1.0 (updated Serial.print to Serial.write)
  25. //
  26. //
  27. // This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  28. // See http://creativecommons.org/licenses/by-nc-sa/3.0/ for license details.
  29.  
  30.  
  31. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  32. #define ARDUINO_MEGA
  33. #elif defined(__AVR_AT90USB646__)
  34. #define TEENSY_PLUS_PLUS
  35. #elif defined(__AVR_ATmega32U4__)
  36. #define TEENSY_2
  37. #elif defined(__AVR_AT90USB1286__)
  38. #define TEENSY_PLUS_PLUS_2
  39. #else
  40. #define ARDUINO
  41. #endif
  42.  
  43.  
  44. // Uncomment this line to send debug messages to the serial monitor
  45. //#define DEBUG
  46.  
  47. // Uncomment this line to enable outputs corresponding to the MIDI Fighter so MF mappings can be used in Traktor.
  48. //#define MIDI_FIGHTER
  49.  
  50. //#define FASTADC
  51. // defines for setting and clearing register bits
  52. #ifndef cbi
  53. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  54. #endif
  55. #ifndef sbi
  56. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  57. #endif
  58.  
  59. // MIDI mapping taken from http://www.nortonmusic.com/midi_cc.html
  60. #define MIDI_CC_MODULATION 0x01
  61. #define MIDI_CC_BREATH 0x02
  62. #define MIDI_CC_VOLUME 0x07
  63. #define MIDI_CC_BALANCE 0x08
  64. #define MIDI_CC_PAN 0x0A
  65. #define MIDI_CC_EXPRESSION 0x0B
  66. #define MIDI_CC_EFFECT1 0x0C
  67. #define MIDI_CC_EFFECT2 0x0D
  68.  
  69. #define MIDI_CC_GENERAL1 0x0E
  70. #define MIDI_CC_GENERAL2 0x0F
  71. #define MIDI_CC_GENERAL3 0x10
  72. #define MIDI_CC_GENERAL4 0x11
  73. #define MIDI_CC_GENERAL5 0x12
  74. #define MIDI_CC_GENERAL6 0x13
  75. #define MIDI_CC_GENERAL7 0x14
  76. #define MIDI_CC_GENERAL8 0x15
  77. #define MIDI_CC_GENERAL9 0x16
  78. #define MIDI_CC_GENERAL10 0x17
  79. #define MIDI_CC_GENERAL11 0x18
  80. #define MIDI_CC_GENERAL12 0x19
  81. #define MIDI_CC_GENERAL13 0x1A
  82. #define MIDI_CC_GENERAL14 0x1B
  83. #define MIDI_CC_GENERAL15 0x1C
  84. #define MIDI_CC_GENERAL16 0x1D
  85. #define MIDI_CC_GENERAL17 0x1E
  86. #define MIDI_CC_GENERAL18 0x1F
  87.  
  88. #define MIDI_CC_GENERAL1_FINE 0x2E
  89. #define MIDI_CC_GENERAL2_FINE 0x2F
  90. #define MIDI_CC_GENERAL3_FINE 0x30
  91. #define MIDI_CC_GENERAL4_FINE 0x31
  92. #define MIDI_CC_GENERAL5_FINE 0x32
  93. #define MIDI_CC_GENERAL6_FINE 0x33
  94. #define MIDI_CC_GENERAL7_FINE 0x34
  95. #define MIDI_CC_GENERAL8_FINE 0x35
  96. #define MIDI_CC_GENERAL9_FINE 0x36
  97. #define MIDI_CC_GENERAL10_FINE 0x37
  98. #define MIDI_CC_GENERAL11_FINE 0x38
  99. #define MIDI_CC_GENERAL12_FINE 0x39
  100. #define MIDI_CC_GENERAL13_FINE 0x3A
  101. #define MIDI_CC_GENERAL14_FINE 0x3B
  102. #define MIDI_CC_GENERAL15_FINE 0x3C
  103. #define MIDI_CC_GENERAL16_FINE 0x3D
  104. #define MIDI_CC_GENERAL17_FINE 0x3E
  105. #define MIDI_CC_GENERAL18_FINE 0x3F
  106.  
  107. #define MIDI_CC_SUSTAIN 0x40
  108. #define MIDI_CC_REVERB 0x5B
  109. #define MIDI_CC_CHORUS 0x5D
  110. #define MIDI_CC_CONTROL_OFF 0x79
  111. #define MIDI_CC_NOTES_OFF 0x78
  112.  
  113. #define NOTE_C0 0x00 // 0
  114. #define NOTE_C1 0x12 // 18
  115. #define NOTE_C2 0x24 // 36
  116.  
  117. #if defined(ARDUINO_MEGA)
  118. // Number of digital inputs. Can be anywhere from 0 to 68.
  119. #define NUM_DI 52
  120. // Number of analogue inputs. Can be anywhere from 0 to 16.
  121. #define NUM_AI 16
  122. #elif defined(TEENSY_PLUS_PLUS)
  123. // Number of digital inputs. Can be anywhere from 0 to 46.
  124. #define NUM_DI 38
  125. // Number of analogue inputs. Can be anywhere from 0 to 8.
  126. #define NUM_AI 8
  127. #elif defined(TEENSY_2)
  128. // Number of digital inputs. Can be anywhere from 0 to 25.
  129. #define NUM_DI 13
  130. // Number of analogue inputs. Can be anywhere from 0 to 12.
  131. #define NUM_AI 12
  132. #elif defined(TEENSY_PLUS_PLUS_2)
  133. // Number of digital inputs. Can be anywhere from 0 to 46.
  134. #define NUM_DI 38
  135. // Number of analogue inputs. Can be anywhere from 0 to 8.
  136. #define NUM_AI 8
  137. #else
  138. // Number of digital inputs. Can be anywhere from 0 to 18.
  139. #define NUM_DI 12
  140. // Number of analogue inputs. Can be anywhere from 0 to 6.
  141. #define NUM_AI 6
  142. #endif
  143.  
  144.  
  145.  
  146. #if defined(MIDI_FIGHTER) && defined(ARDUINO)
  147. #define MIDI_CHANNEL 3
  148. // First note, starting from lower left button
  149. #define NOTE NOTE_C2
  150. // When mapping to a MIDI Fighter we need to skip a row of buttons. Set this from 0-3 to define which row to skip.
  151. // Rows are ordered from bottom to top (same as the MIDI Fighter's button layout).
  152. #define SKIP_ROW 2
  153. // This pin order corresponds to the bottom left button being zero, increasing by one as we move from left to right, bottom to top
  154. // 8 9 10 11
  155. // 4 5 6 7
  156. // 0 1 2 3
  157. // This array size must match NUM_DI above.
  158. #define DIGITAL_PIN_ORDER 10, 11, 12, 13, 6, 7, 8, 9, 2, 3, 4, 5
  159. #else
  160. #define MIDI_CHANNEL 1
  161. // First note, starting from upper left button
  162. #define NOTE NOTE_C0
  163. // This pin order corresponds to the top left button being zero, increasing by one as we move from left to right, top to bottom
  164. // 0 1 2 3
  165. // 4 5 6 7
  166. // 8 9 10 11
  167. // This array size must match NUM_DI above.
  168. #if defined(ARDUINO_MEGA)
  169. #define DIGITAL_PIN_ORDER 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53
  170. #elif defined(TEENSY_PLUS_PLUS)
  171. #define DIGITAL_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37
  172. #elif defined(TEENSY_2)
  173. #define DIGITAL_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  174. #elif defined(TEENSY_PLUS_PLUS_2)
  175. #define DIGITAL_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37
  176. #else
  177. #define DIGITAL_PIN_ORDER 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
  178. #endif
  179. #endif
  180.  
  181. #if defined(ARDUINO_MEGA)
  182. #define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  183. #elif defined(TEENSY_PLUS_PLUS)
  184. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7
  185. #elif defined(TEENSY_2)
  186. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  187. #elif defined(TEENSY_PLUS_PLUS_2)
  188. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7
  189. #else
  190. #define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5
  191. #endif
  192.  
  193. #if defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  194. #define LED_PIN PIN_D6
  195. #else
  196. #define LED_PIN 13
  197. #endif
  198.  
  199. #define MIDI_CC MIDI_CC_GENERAL1
  200.  
  201. // Comment this line out to disable button debounce logic.
  202. // See http://arduino.cc/en/Tutorial/Debounce on what debouncing is used for.
  203. #define DEBOUNCE
  204. // Debounce time length in milliseconds
  205. #define DEBOUNCE_LENGTH 2
  206.  
  207. // Comment this line out to disable analogue filtering
  208. #define ANALOGUE_FILTER
  209. // A knob or slider movement must initially exceed this value to be recognised as an input. Note that it is
  210. // for a 7-bit (0-127) MIDI value.
  211. #ifdef FASTADC
  212. #define FILTER_AMOUNT 3
  213. #else
  214. #define FILTER_AMOUNT 2
  215. #endif
  216. // Timeout is in microseconds
  217. #define ANALOGUE_INPUT_CHANGE_TIMEOUT 250000
  218.  
  219. // Array containing a mapping of digital pins to channel index.
  220. byte digitalInputMapping[NUM_DI] = {DIGITAL_PIN_ORDER};
  221.  
  222. // Array containing a mapping of analogue pins to channel index. This array size must match NUM_AI above.
  223. byte analogueInputMapping[NUM_AI] = {ANALOGUE_PIN_ORDER};
  224.  
  225. // Contains the current state of the digital inputs.
  226. byte digitalInputs[NUM_DI];
  227. // Contains the current value of the analogue inputs.
  228. byte analogueInputs[NUM_AI];
  229.  
  230. // Variable to hold temporary digital reads, used for debounce logic.
  231. byte tempDigitalInput;
  232. // Variable to hold temporary analogue values, used for analogue filtering logic.
  233. byte tempAnalogueInput;
  234.  
  235. // Preallocate the for loop index so we don't keep reallocating it for every program iteration.
  236. byte i = 0;
  237. byte digitalOffset = 0;
  238. // Variable to hold difference between current and new analogue input values.
  239. byte analogueDiff = 0;
  240. // This is used as a flag to indicate that an analogue input is changing.
  241. boolean analogueInputChanging[NUM_AI];
  242. // Time the analogue input was last moved
  243. unsigned long analogueInputTimer[NUM_AI];
  244.  
  245. #ifdef DEBUG
  246. unsigned long loopTime = 0;
  247. unsigned long serialSendTime = 0;
  248. #endif
  249.  
  250. void setup()
  251. {
  252. // Taken from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11
  253. #ifdef FASTADC
  254. // set prescale to 16
  255. sbi(ADCSRA,ADPS2) ;
  256. cbi(ADCSRA,ADPS1) ;
  257. cbi(ADCSRA,ADPS0) ;
  258. #endif
  259.  
  260. // Only enable serial on the Arduino or when debugging. The Teensy board should be set as a usb-midi device so serial is not needed.
  261. #if defined(ARDUINO) || defined(ARDUINO_MEGA) || defined(DEBUG)
  262. // Enable serial I/O at 115200 kbps. This is faster than the standard MIDI rate of 31250 kbps.
  263. // The PC application which we connect to will automatically take the higher sample rate and send MIDI
  264. // messages out at the correct rate. We only send things faster in case there is any latency.
  265. Serial.begin(115200);
  266. #endif
  267.  
  268. // Initialise each digital input channel.
  269. for (i = 0; i < NUM_DI; i++)
  270. {
  271. // Set the pin direction to input.
  272. pinMode(digitalInputMapping[i], INPUT);
  273.  
  274. // Don't enable pullup resistor on LED_PIN, as the LED and resistor will always pull it low, meaning the input won't work.
  275. // Instead an external pulldown resistor must be used on LED_PIN.
  276. // NOTE: This will cause all of the high/low logic for LED_PIN to be inverted.
  277. if (digitalInputMapping[i] != LED_PIN)
  278. {
  279. // Enable the pull-up resistor. This call must come after the above pinMode call.
  280. digitalWrite(digitalInputMapping[i], HIGH);
  281. }
  282.  
  283. // Initialise the digital state with a read to the input pin.
  284. digitalInputs[i] = digitalRead(digitalInputMapping[i]);
  285. }
  286.  
  287. // Initialise each analogue input channel.
  288. for (i = 0; i < NUM_AI; i++)
  289. {
  290. // Set the pin direction to input.
  291. pinMode(analogueInputMapping[i], INPUT);
  292.  
  293. // Initialise the analogue value with a read to the input pin.
  294. analogueInputs[i] = analogRead(analogueInputMapping[i]);
  295.  
  296. // Assume no analogue inputs are active
  297. analogueInputChanging[i] = false;
  298. analogueInputTimer[i] = 0;
  299. }
  300.  
  301. #ifdef DEBUG
  302. serialSendTime = millis();
  303. #endif
  304. }
  305.  
  306.  
  307. void loop()
  308. {
  309. #ifdef DEBUG
  310. loopTime = micros();
  311. #endif
  312.  
  313. for (i = 0; i < NUM_DI; i++)
  314. {
  315. #ifdef MIDI_FIGHTER
  316. if (i >= SKIP_ROW * 4)
  317. {
  318. digitalOffset = i + 4;
  319. }
  320. else
  321. {
  322. #endif
  323.  
  324. digitalOffset = i;
  325.  
  326. #ifdef MIDI_FIGHTER
  327. }
  328. #endif
  329.  
  330. // Read the current state of the digital input and store it temporarily.
  331. tempDigitalInput = digitalRead(digitalInputMapping[i]);
  332.  
  333. // Check if the last state is different to the current state.
  334. if (digitalInputs[i] != tempDigitalInput)
  335. {
  336. #ifdef DEBOUNCE
  337. // Wait for a short period of time, and then take a second reading from the input pin.
  338. delay(DEBOUNCE_LENGTH);
  339. // If the second reading is the same as the initial reading, assume it must be true.
  340. if (tempDigitalInput == digitalRead(digitalInputMapping[i]))
  341. {
  342. #endif
  343. // Record the new digital input state.
  344. digitalInputs[i] = tempDigitalInput;
  345.  
  346. // Moved from HIGH to LOW (button pressed)
  347. if (digitalInputs[i] == 0)
  348. {
  349. // All the digital inputs use pullup resistors, except LED_PIN so the logic is inverted
  350. if (digitalInputMapping[i] != LED_PIN)
  351. {
  352. noteOn(MIDI_CHANNEL, NOTE + digitalOffset, 0x7F); // Channel 1, middle C, maximum velocity
  353. }
  354. else
  355. {
  356. noteOff(MIDI_CHANNEL, NOTE + digitalOffset); // Channel 1, middle C
  357. }
  358. }
  359. // Moved from LOW to HIGH (button released)
  360. else
  361. {
  362. // All the digital inputs use pullup resistors, except LED_PIN so the logic is inverted
  363. if (digitalInputMapping[i] != LED_PIN)
  364. {
  365. noteOff(MIDI_CHANNEL, NOTE + digitalOffset); // Channel 1, middle C
  366. }
  367. else
  368. {
  369. noteOn(MIDI_CHANNEL, NOTE + digitalOffset, 0x7F); // Channel 1, middle C, maximum velocity
  370. }
  371. }
  372. #ifdef DEBOUNCE
  373. }
  374. #endif
  375. }
  376. }
  377.  
  378. /*
  379. * Analogue input logic:
  380. * The Arduino uses a 10-bit (0-1023) analogue to digital converter (ADC) on each of its analogue inputs.
  381. * The ADC isn't very high resolution, so if a pot is in a position such that the output voltage is 'between'
  382. * what it can detect (say 2.505V or about 512.5 on a scale of 0-1023) then the value read will constantly
  383. * fluctuate between two integers (in this case 512 and 513).
  384. *
  385. * If we're simply looking for a change in the analogue input value like in the digital case above, then
  386. * there will be cases where the value is always changing, even though the physical input isn't being moved.
  387. * This will in turn send out a constant stream of MIDI messages to the connected software which may be problematic.
  388. *
  389. * To combat this, we require that the analogue input value must change by a certain threshold amount before
  390. * we register that it is actually changing. This is good in avoiding a constantly fluctuating value, but has
  391. * the negative effect of a reduced input resolution. For example if the threshold amount was 2 and we slowly moved
  392. * a slider through it's full range, we would only detect every second value as a change, in effect reducing the
  393. * already small 7-bit MIDI value to a 6-bit MIDI value.
  394. *
  395. * To get around this problem but still use the threshold logic, a timer is used. Initially the analogue input
  396. * must exceed the threshold to be detected as an input. Once this occurs, we then read every value coming from the
  397. * analogue input (not just those exceeding a threshold) giving us full 7-bit resolution. At the same time the
  398. * timer is started. This timer is used to keep track of whether an input hasn't been moved for a certain time
  399. * period. If it has been moved, the timer is restarted. If no movement occurs the timer is just left to run. When
  400. * the timer expires the analogue input is assumed to be no longer moving. Subsequent movements must exceed the
  401. * threshold amount.
  402. */
  403. for (i = 0; i < NUM_AI; i++)
  404. {
  405. // Read the analogue input pin, dividing it by 8 so the 10-bit ADC value (0-1023) is converted to a 7-bit MIDI value (0-127).
  406. tempAnalogueInput = analogRead(analogueInputMapping[i]) / 8;
  407.  
  408. #ifdef ANALOGUE_FILTER
  409. // Take the absolute value of the difference between the curent and new values
  410. analogueDiff = abs(tempAnalogueInput - analogueInputs[i]);
  411. // Only continue if the threshold was exceeded, or the input was already changing
  412. if ((analogueDiff > 0 && analogueInputChanging[i] == true) || analogueDiff >= FILTER_AMOUNT)
  413. {
  414. // Only restart the timer if we're sure the input isn't 'between' a value
  415. // ie. It's moved more than FILTER_AMOUNT
  416. if (analogueInputChanging[i] == false || analogueDiff >= FILTER_AMOUNT)
  417. {
  418. // Reset the last time the input was moved
  419. analogueInputTimer[i] = micros();
  420.  
  421. // The analogue input is moving
  422. analogueInputChanging[i] = true;
  423. }
  424. else if (micros() - analogueInputTimer[i] > ANALOGUE_INPUT_CHANGE_TIMEOUT)
  425. {
  426. analogueInputChanging[i] = false;
  427. }
  428.  
  429. // Only send data if we know the analogue input is moving
  430. if (analogueInputChanging[i] == true)
  431. {
  432. // Record the new analogue value
  433. analogueInputs[i] = tempAnalogueInput;
  434.  
  435. // Send the analogue value out on the general MIDI CC (see definitions at beginning of this file)
  436. controlChange(MIDI_CHANNEL, MIDI_CC + i, analogueInputs[i]);
  437. }
  438. }
  439. #else
  440. if (analogueInputs[i] != tempAnalogueInput)
  441. {
  442. // Record the new analogue value
  443. analogueInputs[i] = tempAnalogueInput;
  444.  
  445. // Send the analogue value out on the general MIDI CC (see definitions at beginning of this file)
  446. controlChange(MIDI_CHANNEL, MIDI_CC + i, analogueInputs[i]);
  447. }
  448. #endif
  449. }
  450.  
  451. #ifdef DEBUG
  452. loopTime = micros() - loopTime;
  453.  
  454. // Print the loop execution time once per second
  455. if (millis() - serialSendTime > 1000)
  456. {
  457. Serial.print("Loop execution time (us): ");
  458. Serial.println(loopTime);
  459.  
  460. serialSendTime = millis();
  461. }
  462. #endif
  463. }
  464.  
  465. // Send a MIDI note on message
  466. void noteOn(byte channel, byte pitch, byte velocity)
  467. {
  468. // 0x90 is the first of 16 note on channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  469. channel += 0x90 - 1;
  470.  
  471. // Ensure we're between channels 1 and 16 for a note on message
  472. if (channel >= 0x90 && channel <= 0x9F)
  473. {
  474. #ifdef DEBUG
  475. Serial.print("Button pressed: ");
  476. Serial.println(pitch);
  477. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  478. usbMIDI.sendNoteOn(pitch, velocity, channel);
  479. #else
  480. Serial.write(channel);
  481. Serial.write(pitch);
  482. Serial.write(velocity);
  483. #endif
  484. }
  485. }
  486.  
  487. // Send a MIDI note off message
  488. void noteOff(byte channel, byte pitch)
  489. {
  490. // 0x80 is the first of 16 note off channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  491. channel += 0x80 - 1;
  492.  
  493. // Ensure we're between channels 1 and 16 for a note off message
  494. if (channel >= 0x80 && channel <= 0x8F)
  495. {
  496. #ifdef DEBUG
  497. Serial.print("Button released: ");
  498. Serial.println(pitch);
  499. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  500. usbMIDI.sendNoteOff(pitch, 0x00, channel);
  501. #else
  502. Serial.write(channel);
  503. Serial.write(pitch);
  504. Serial.write((byte)0x00);
  505. #endif
  506. }
  507. }
  508.  
  509. // Send a MIDI control change message
  510. void controlChange(byte channel, byte control, byte value)
  511. {
  512. // 0xB0 is the first of 16 control change channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  513. channel += 0xB0 - 1;
  514.  
  515. // Ensure we're between channels 1 and 16 for a CC message
  516. if (channel >= 0xB0 && channel <= 0xBF)
  517. {
  518. #ifdef DEBUG
  519. Serial.print(control - MIDI_CC);
  520. Serial.print(": ");
  521. Serial.println(value);
  522. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  523. usbMIDI.sendControlChange(control, value, channel);
  524. #else
  525. Serial.write(channel);
  526. Serial.write(control);
  527. Serial.write(value);
  528. #endif
  529. }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement