norman2

Midi drum arduino sketch

Nov 30th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. /*
  2. * MIDI Drum Kit
  3. * -------------
  4. * Convert Arduino to a MIDI controller using various inputs and
  5. * the serial port as a MIDI output.
  6. *
  7. * This sketch is set up to send General MIDI (GM) drum notes
  8. * on MIDI channel 1, but it can be easily reconfigured for other
  9. * notes and channels
  10. *
  11. * It uses switch inputs to send MIDI notes of a fixed velocity with
  12. * note on time determined by duration of keypress and it uses
  13. * piezo buzzer elements as inputs to send MIDI notes of a varying velocity
  14. * & duration, depending on forced of impulse imparted to piezo sensor.
  15. *
  16. * To send MIDI, attach a MIDI out jack (female DIN-5) to Arduino.
  17. * DIN-5 pinout is: _____
  18. * pin 2 - Gnd / \
  19. * pin 4 - 220 ohm resistor to +5V | 3 1 | MIDI jack
  20. * pin 5 - Arduino D1 (TX) | 5 4 |
  21. * all other pins - unconnected \__2__/
  22. * On my midi jack, the color of the wires for the pins are:
  23. * 3 = n/c
  24. * 5 = black (blue)
  25. * 2 = red (red)
  26. * 4 = orange (yellow)
  27. * 1 = brown
  28. *
  29. * Based off of Tom Igoe's work at:
  30. * http://itp.nyu.edu/physcomp/Labs/MIDIOutput
  31. *
  32. * Created 25 October 2006
  33. * copyleft 2006 Tod E. Kurt <[email protected]
  34. * http://todbot.com/
  35. *
  36. * Updates:
  37. * - 2 May 2009 - fixed noteOn() and noteOff()
  38. *
  39. */
  40.  
  41. // what midi channel we're sending on
  42. // ranges from 0-15
  43. #define drumchan 1
  44.  
  45. // general midi drum notes
  46. #define note_bassdrum 35
  47. #define note_snaredrum 38
  48. #define note_hihatclosed 42
  49. #define note_hihatopen 44
  50. #define note_crash 49
  51.  
  52. // define the pins we use
  53. #define switchAPin 7 //stopa
  54. #define switchBPin 6
  55. #define switchCPin 5
  56. #define piezoAPin 2
  57. #define piezoBPin 3
  58. #define piezoCPin 4
  59. #define piezoDPin 5
  60. #define ledPin 13 // for midi out status
  61.  
  62. // analog threshold for piezo sensing
  63. #define PIEZOTHRESHOLD 100
  64.  
  65. int switchAState = LOW;
  66. int switchBState = LOW;
  67. int switchCState = LOW;
  68. int currentSwitchState = LOW;
  69.  
  70. int val,t;
  71.  
  72. void setup() {
  73. pinMode(switchAPin, INPUT);
  74. pinMode(switchBPin, INPUT);
  75. pinMode(switchCPin, INPUT);
  76. digitalWrite(switchAPin, HIGH); // turn on internal pullup
  77. digitalWrite(switchBPin, HIGH); // turn on internal pullup
  78. digitalWrite(switchCPin, HIGH); // turn on internal pullup
  79.  
  80. pinMode(ledPin, OUTPUT);
  81. Serial.begin(115200); // set MIDI baud rate
  82. }
  83.  
  84. void loop() {
  85. do
  86. {
  87. // lcd.setCursor(0,0);
  88. // lcd.Print("TEST MODE LOOP");
  89. for (int note=35; note<82; note++)
  90. {
  91. // lcd.setCursor(0,1);
  92. // lcd.print("Playing note : ");
  93. // lcd.print(note);
  94. noteOn(drumchan, note, 100);
  95. delay(100);
  96. noteOff(drumchan, note, 0);
  97. delay(500);
  98.  
  99. }
  100. }
  101. while(1);
  102. // deal with switchA
  103. currentSwitchState = digitalRead(switchAPin);
  104. if( currentSwitchState == LOW && switchAState == HIGH ) // push
  105. noteOn(drumchan, note_bassdrum, 100);
  106. if( currentSwitchState == HIGH && switchAState == LOW ) // release
  107. noteOff(drumchan, note_bassdrum, 0);
  108. switchAState = currentSwitchState;
  109.  
  110. /* deal with switchB
  111. currentSwitchState = digitalRead(switchBPin);
  112. if( currentSwitchState == LOW && switchBState == HIGH ) // push
  113. noteOn(drumchan, note_snaredrum, 100);
  114. if( currentSwitchState == HIGH && switchBState == LOW ) // release
  115. noteOff(drumchan, note_snaredrum, 0);
  116. switchBState = currentSwitchState;
  117. */
  118. val = analogRead(piezoCPin);
  119. if( val >= PIEZOTHRESHOLD ) {
  120. t=0;
  121. while(analogRead(piezoCPin) >= PIEZOTHRESHOLD/2) {
  122. t++;
  123. }
  124. noteOn(drumchan,note_snaredrum, t*2);
  125. delay(t);
  126. noteOff(drumchan,note_snaredrum,0);
  127. }
  128.  
  129. /* deal with switchC
  130. currentSwitchState = digitalRead(switchCPin);
  131. if( currentSwitchState == LOW && switchCState == HIGH ) // push
  132. noteOn(drumchan, note_hihatclosed, 100);
  133. if( currentSwitchState == HIGH && switchCState == LOW ) // release
  134. noteOff(drumchan, note_hihatclosed, 0);
  135. switchCState = currentSwitchState;
  136. */
  137. val = analogRead(piezoDPin);
  138. if( val >= PIEZOTHRESHOLD ) {
  139. t=0;
  140. while(analogRead(piezoDPin) >= PIEZOTHRESHOLD/2) {
  141. t++;
  142. }
  143. noteOn(drumchan,note_hihatclosed, t*2);
  144. delay(t);
  145. noteOff(drumchan,note_hihatclosed,0);
  146. }
  147. // deal with first piezo, this is kind of a hack
  148. val = analogRead(piezoAPin);
  149. if( val >= PIEZOTHRESHOLD ) {
  150. t=0;
  151. while(analogRead(piezoAPin) >= PIEZOTHRESHOLD/2) {
  152. t++;
  153. }
  154. noteOn(drumchan,note_hihatopen, t*2);
  155. delay(t);
  156. noteOff(drumchan,note_hihatopen,0);
  157. }
  158.  
  159. // deal with second piezos, this is kind of a hack
  160. val = analogRead(piezoBPin);
  161. if( val >= PIEZOTHRESHOLD ) {
  162. t=0;
  163. while(analogRead(piezoBPin) >= PIEZOTHRESHOLD/2) {
  164. t++;
  165. }
  166. noteOn(drumchan,note_crash, t*2);
  167. delay(t);
  168. noteOff(drumchan,note_crash,0);
  169. }
  170. }
  171.  
  172. // Send a MIDI note-on message. Like pressing a piano key
  173. // channel ranges from 0-15
  174. void noteOn(byte channel, byte note, byte velocity) {
  175. midiMsg( (0x90 | channel), note, velocity);
  176. }
  177.  
  178. // Send a MIDI note-off message. Like releasing a piano key
  179. void noteOff(byte channel, byte note, byte velocity) {
  180. midiMsg( (0x80 | channel), note, velocity);
  181. }
  182.  
  183. // Send a general MIDI message
  184. void midiMsg(byte cmd, byte data1, byte data2) {
  185. digitalWrite(ledPin,HIGH); // indicate we're sending MIDI data
  186. Serial.write(cmd);
  187. Serial.write(data1);
  188. Serial.write(data2);
  189. digitalWrite(ledPin,LOW);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment