Advertisement
Guest User

Untitled

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