Advertisement
Guest User

Untitled

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