Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
149
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. #if defined(MIDI_FIGHTER) && defined(ARDUINO)
  146. #define MIDI_CHANNEL 3
  147. // First note, starting from lower left button
  148. #define NOTE NOTE_C2
  149. // 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.
  150. // Rows are ordered from bottom to top (same as the MIDI Fighter's button layout).
  151. #define SKIP_ROW 2
  152. // This pin order corresponds to the bottom left button being zero, increasing by one as we move from left to right, bottom to top
  153. // 8 9 10 11
  154. // 4 5 6 7
  155. // 0 1 2 3
  156. // This array size must match NUM_DI above.
  157. #define DIGITAL_PIN_ORDER 10, 11, 12, 13, 6, 7, 8, 9, 2, 3, 4, 5
  158. #else
  159. #define MIDI_CHANNEL 1
  160. // First note, starting from upper left button
  161. #define NOTE NOTE_C0
  162. // This pin order corresponds to the top left button being zero, increasing by one as we move from left to right, top to bottom
  163. // 0 1 2 3
  164. // 4 5 6 7
  165. // 8 9 10 11
  166. // This array size must match NUM_DI above.
  167. #if defined(ARDUINO_MEGA)
  168. #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
  169. #elif defined(TEENSY_PLUS_PLUS)
  170. #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
  171. #elif defined(TEENSY_2)
  172. #define DIGITAL_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  173. #elif defined(TEENSY_PLUS_PLUS_2)
  174. #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
  175. #else
  176. #define DIGITAL_PIN_ORDER 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
  177. #endif
  178. #endif
  179.  
  180. #if defined(ARDUINO_MEGA)
  181. #define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  182. #elif defined(TEENSY_PLUS_PLUS)
  183. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7
  184. #elif defined(TEENSY_2)
  185. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  186. #elif defined(TEENSY_PLUS_PLUS_2)
  187. #define ANALOGUE_PIN_ORDER 0, 1, 2, 3, 4, 5, 6, 7
  188. #else
  189. #define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5
  190. #endif
  191.  
  192. #if defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  193. #define LED_PIN PIN_D6
  194. #else
  195. #define LED_PIN 13
  196. #endif
  197.  
  198. #define MIDI_CC MIDI_CC_GENERAL1
  199.  
  200. // Comment this line out to disable button debounce logic.
  201. // See http://arduino.cc/en/Tutorial/Debounce on what debouncing is used for.
  202. #define DEBOUNCE
  203. // Debounce time length in milliseconds
  204. #define DEBOUNCE_LENGTH 2
  205.  
  206. // Comment this line out to disable analogue filtering
  207. #define ANALOGUE_FILTER
  208. // A knob or slider movement must initially exceed this value to be recognised as an input. Note that it is
  209. // for a 7-bit (0-127) MIDI value.
  210. #ifdef FASTADC
  211. #define FILTER_AMOUNT 3
  212. #else
  213. #define FILTER_AMOUNT 2
  214. #endif
  215. // Timeout is in microseconds
  216. #define ANALOGUE_INPUT_CHANGE_TIMEOUT 250000
  217.  
  218. // Array containing a mapping of digital pins to channel index.
  219. byte digitalInputMapping[NUM_DI] = {DIGITAL_PIN_ORDER};
  220.  
  221. // Array containing a mapping of analogue pins to channel index. This array size must match NUM_AI above.
  222. byte analogueInputMapping[NUM_AI] = {ANALOGUE_PIN_ORDER};
  223.  
  224. // Contains the current state of the digital inputs.
  225. byte digitalInputs[NUM_DI];
  226. // Contains the current value of the analogue inputs.
  227. byte analogueInputs[NUM_AI];
  228.  
  229. // Variable to hold temporary digital reads, used for debounce logic.
  230. byte tempDigitalInput;
  231. // Variable to hold temporary analogue values, used for analogue filtering logic.
  232. byte tempAnalogueInput;
  233.  
  234. // Preallocate the for loop index so we don't keep reallocating it for every program iteration.
  235. byte i = 0;
  236. byte digitalOffset = 0;
  237. // Variable to hold difference between current and new analogue input values.
  238. byte analogueDiff = 0;
  239. // This is used as a flag to indicate that an analogue input is changing.
  240. boolean analogueInputChanging[NUM_AI];
  241. // Time the analogue input was last moved
  242. unsigned long analogueInputTimer[NUM_AI];
  243.  
  244. #ifdef DEBUG
  245. unsigned long loopTime = 0;
  246. unsigned long serialSendTime = 0;
  247. #endif
  248.  
  249. void setup()
  250. {
  251. // Taken from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11
  252. #ifdef FASTADC
  253. // set prescale to 16
  254. sbi(ADCSRA,ADPS2) ;
  255. cbi(ADCSRA,ADPS1) ;
  256. cbi(ADCSRA,ADPS0) ;
  257. #endif
  258.  
  259. // 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.
  260. #if defined(ARDUINO) || defined(ARDUINO_MEGA) || defined(DEBUG)
  261. // Enable serial I/O at 115200 kbps. This is faster than the standard MIDI rate of 31250 kbps.
  262. // The PC application which we connect to will automatically take the higher sample rate and send MIDI
  263. // messages out at the correct rate. We only send things faster in case there is any latency.
  264. Serial.begin(115200);
  265. #endif
  266.  
  267. // Initialise each digital input channel.
  268. for (i = 0; i < NUM_DI; i++)
  269. {
  270. // Set the pin direction to input.
  271. pinMode(digitalInputMapping[i], INPUT);
  272.  
  273. // Don't enable pullup resistor on LED_PIN, as the LED and resistor will always pull it low, meaning the input won't work.
  274. // Instead an external pulldown resistor must be used on LED_PIN.
  275. // NOTE: This will cause all of the high/low logic for LED_PIN to be inverted.
  276. if (digitalInputMapping[i] != LED_PIN)
  277. {
  278. // Enable the pull-up resistor. This call must come after the above pinMode call.
  279. digitalWrite(digitalInputMapping[i], HIGH);
  280. }
  281.  
  282. // Initialise the digital state with a read to the input pin.
  283. digitalInputs[i] = digitalRead(digitalInputMapping[i]);
  284. }
  285.  
  286. // Initialise each analogue input channel.
  287. for (i = 0; i < NUM_AI; i++)
  288. {
  289. // Set the pin direction to input.
  290. pinMode(analogueInputMapping[i], INPUT);
  291.  
  292. // Initialise the analogue value with a read to the input pin.
  293. analogueInputs[i] = analogRead(analogueInputMapping[i]);
  294.  
  295. // Assume no analogue inputs are active
  296. analogueInputChanging[i] = false;
  297. analogueInputTimer[i] = 0;
  298. }
  299.  
  300. #ifdef DEBUG
  301. serialSendTime = millis();
  302. #endif
  303. }
  304.  
  305.  
  306. void loop()
  307. {
  308. #ifdef DEBUG
  309. loopTime = micros();
  310. #endif
  311.  
  312. for (i = 0; i < NUM_DI; i++)
  313. {
  314. #ifdef MIDI_FIGHTER
  315. if (i >= SKIP_ROW * 4)
  316. {
  317. digitalOffset = i + 4;
  318. }
  319. else
  320. {
  321. #endif
  322.  
  323. digitalOffset = i;
  324.  
  325. #ifdef MIDI_FIGHTER
  326. }
  327. #endif
  328.  
  329. // Read the current state of the digital input and store it temporarily.
  330. tempDigitalInput = digitalRead(digitalInputMapping[i]);
  331.  
  332. // Check if the last state is different to the current state.
  333. if (digitalInputs[i] != tempDigitalInput)
  334. {
  335. #ifdef DEBOUNCE
  336. // Wait for a short period of time, and then take a second reading from the input pin.
  337. delay(DEBOUNCE_LENGTH);
  338. // If the second reading is the same as the initial reading, assume it must be true.
  339. if (tempDigitalInput == digitalRead(digitalInputMapping[i]))
  340. {
  341. #endif
  342. // Record the new digital input state.
  343. digitalInputs[i] = tempDigitalInput;
  344.  
  345. // Moved from HIGH to LOW (button pressed)
  346. if (digitalInputs[i] == 0)
  347. {
  348. // All the digital inputs use pullup resistors, except LED_PIN so the logic is inverted
  349. if (digitalInputMapping[i] != LED_PIN)
  350. {
  351. noteOn(MIDI_CHANNEL, NOTE + digitalOffset, 0x7F); // Channel 1, middle C, maximum velocity
  352. }
  353. else
  354. {
  355. noteOff(MIDI_CHANNEL, NOTE + digitalOffset); // Channel 1, middle C
  356. }
  357. }
  358. // Moved from LOW to HIGH (button released)
  359. else
  360. {
  361. // All the digital inputs use pullup resistors, except LED_PIN so the logic is inverted
  362. if (digitalInputMapping[i] != LED_PIN)
  363. {
  364. noteOff(MIDI_CHANNEL, NOTE + digitalOffset); // Channel 1, middle C
  365. }
  366. else
  367. {
  368. noteOn(MIDI_CHANNEL, NOTE + digitalOffset, 0x7F); // Channel 1, middle C, maximum velocity
  369. }
  370. }
  371. #ifdef DEBOUNCE
  372. }
  373. #endif
  374. }
  375. }
  376.  
  377. /*
  378. * Analogue input logic:
  379. * The Arduino uses a 10-bit (0-1023) analogue to digital converter (ADC) on each of its analogue inputs.
  380. * The ADC isn't very high resolution, so if a pot is in a position such that the output voltage is 'between'
  381. * what it can detect (say 2.505V or about 512.5 on a scale of 0-1023) then the value read will constantly
  382. * fluctuate between two integers (in this case 512 and 513).
  383. *
  384. * If we're simply looking for a change in the analogue input value like in the digital case above, then
  385. * there will be cases where the value is always changing, even though the physical input isn't being moved.
  386. * This will in turn send out a constant stream of MIDI messages to the connected software which may be problematic.
  387. *
  388. * To combat this, we require that the analogue input value must change by a certain threshold amount before
  389. * we register that it is actually changing. This is good in avoiding a constantly fluctuating value, but has
  390. * the negative effect of a reduced input resolution. For example if the threshold amount was 2 and we slowly moved
  391. * a slider through it's full range, we would only detect every second value as a change, in effect reducing the
  392. * already small 7-bit MIDI value to a 6-bit MIDI value.
  393. *
  394. * To get around this problem but still use the threshold logic, a timer is used. Initially the analogue input
  395. * must exceed the threshold to be detected as an input. Once this occurs, we then read every value coming from the
  396. * analogue input (not just those exceeding a threshold) giving us full 7-bit resolution. At the same time the
  397. * timer is started. This timer is used to keep track of whether an input hasn't been moved for a certain time
  398. * period. If it has been moved, the timer is restarted. If no movement occurs the timer is just left to run. When
  399. * the timer expires the analogue input is assumed to be no longer moving. Subsequent movements must exceed the
  400. * threshold amount.
  401. */
  402. for (i = 0; i < NUM_AI; i++)
  403. {
  404. // 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).
  405. tempAnalogueInput = analogRead(analogueInputMapping[i]) / 8;
  406.  
  407. #ifdef ANALOGUE_FILTER
  408. // Take the absolute value of the difference between the curent and new values
  409. analogueDiff = abs(tempAnalogueInput - analogueInputs[i]);
  410. // Only continue if the threshold was exceeded, or the input was already changing
  411. if ((analogueDiff > 0 && analogueInputChanging[i] == true) || analogueDiff >= FILTER_AMOUNT)
  412. {
  413. // Only restart the timer if we're sure the input isn't 'between' a value
  414. // ie. It's moved more than FILTER_AMOUNT
  415. if (analogueInputChanging[i] == false || analogueDiff >= FILTER_AMOUNT)
  416. {
  417. // Reset the last time the input was moved
  418. analogueInputTimer[i] = micros();
  419.  
  420. // The analogue input is moving
  421. analogueInputChanging[i] = true;
  422. }
  423. else if (micros() - analogueInputTimer[i] > ANALOGUE_INPUT_CHANGE_TIMEOUT)
  424. {
  425. analogueInputChanging[i] = false;
  426. }
  427.  
  428. // Only send data if we know the analogue input is moving
  429. if (analogueInputChanging[i] == true)
  430. {
  431. // Record the new analogue value
  432. analogueInputs[i] = tempAnalogueInput;
  433.  
  434. // Send the analogue value out on the general MIDI CC (see definitions at beginning of this file)
  435. controlChange(MIDI_CHANNEL, MIDI_CC + i, analogueInputs[i]);
  436. }
  437. }
  438. #else
  439. if (analogueInputs[i] != tempAnalogueInput)
  440. {
  441. // Record the new analogue value
  442. analogueInputs[i] = tempAnalogueInput;
  443.  
  444. // Send the analogue value out on the general MIDI CC (see definitions at beginning of this file)
  445. controlChange(MIDI_CHANNEL, MIDI_CC + i, analogueInputs[i]);
  446. }
  447. #endif
  448. }
  449.  
  450. #ifdef DEBUG
  451. loopTime = micros() - loopTime;
  452.  
  453. // Print the loop execution time once per second
  454. if (millis() - serialSendTime > 1000)
  455. {
  456. Serial.print("Loop execution time (us): ");
  457. Serial.println(loopTime);
  458.  
  459. serialSendTime = millis();
  460. }
  461. #endif
  462. }
  463.  
  464. // Send a MIDI note on message
  465. void noteOn(byte channel, byte pitch, byte velocity)
  466. {
  467. // 0x90 is the first of 16 note on channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  468. channel += 0x90 - 1;
  469.  
  470. // Ensure we're between channels 1 and 16 for a note on message
  471. if (channel >= 0x90 && channel <= 0x9F)
  472. {
  473. #ifdef DEBUG
  474. Serial.print("Button pressed: ");
  475. Serial.println(pitch);
  476. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  477. usbMIDI.sendNoteOn(pitch, velocity, channel);
  478. #else
  479. Serial.write(channel);
  480. Serial.write(pitch);
  481. Serial.write(velocity);
  482. #endif
  483. }
  484. }
  485.  
  486. // Send a MIDI note off message
  487. void noteOff(byte channel, byte pitch)
  488. {
  489. // 0x80 is the first of 16 note off channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  490. channel += 0x80 - 1;
  491.  
  492. // Ensure we're between channels 1 and 16 for a note off message
  493. if (channel >= 0x80 && channel <= 0x8F)
  494. {
  495. #ifdef DEBUG
  496. Serial.print("Button released: ");
  497. Serial.println(pitch);
  498. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  499. usbMIDI.sendNoteOff(pitch, 0x00, channel);
  500. #else
  501. Serial.write(channel);
  502. Serial.write(pitch);
  503. Serial.write((byte)0x00);
  504. #endif
  505. }
  506. }
  507.  
  508. // Send a MIDI control change message
  509. void controlChange(byte channel, byte control, byte value)
  510. {
  511. // 0xB0 is the first of 16 control change channels. Subtract one to go from MIDI's 1-16 channels to 0-15
  512. channel += 0xB0 - 1;
  513.  
  514. // Ensure we're between channels 1 and 16 for a CC message
  515. if (channel >= 0xB0 && channel <= 0xBF)
  516. {
  517. #ifdef DEBUG
  518. Serial.print(control - MIDI_CC);
  519. Serial.print(": ");
  520. Serial.println(value);
  521. #elif defined(TEENSY_PLUS_PLUS) || defined(TEENSY_2) || defined(TEENSY_PLUS_PLUS_2)
  522. usbMIDI.sendControlChange(control, value, channel);
  523. #else
  524. Serial.write(channel);
  525. Serial.write(control);
  526. Serial.write(value);
  527. #endif
  528. }
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement