Advertisement
jmarler

Arduino MIDI Controller

Nov 6th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. /*
  2.   Multi-channel MIDI Controller
  3.   Using Atmel Atmega168 Microcontroller
  4.   4051 Multiplexers
  5.    
  6.   Expandable by adding additional 4051 multiplexers or other shift register/multiplexer chips
  7.  
  8.   Jon Marler <[email protected]> - December 2008
  9. */
  10.  
  11. int debug_on     = 0;    // Set to 1 to enable debug serial output at 38400 baud
  12.  
  13. byte status_byte = 0xB0; // Continuous Controler Message
  14. byte note_on     = 0x90; // Note On Message
  15. byte note_off    = 0x80; // Note Off Message
  16. byte note_on_v   = 0x40; // Note on velocity
  17. byte note_off_v  = 0x00; // Note off velocity
  18.  
  19. byte note_v      = 0x00; // Current note velocity
  20. byte note_msg    = 0x00; // Current note message
  21.  
  22. byte ana_cc_a[] = { 20, 21, 22, 23, 24, 25, 26, 27 }; // Controller numbers for the first set of pots
  23. byte ana_cc_b[] = { 28, 29, 30, 31, 32, 33, 34, 35 }; // Controller numbers for the second set of pots
  24. byte dig_cc_a[] = { 36, 37, 38, 39, 40, 41, 42, 43 }; // Set of notes for the digital buttons
  25.  
  26. byte data_ana_cc_a[] = { 255, 255, 255, 255, 255, 255, 255, 255 }; // Array to store first set of pot values
  27. byte data_ana_cc_b[] = { 255, 255, 255, 255, 255, 255, 255, 255 }; // Array to store second set of pot values
  28. byte data_dig_cc_a[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Array to store current button values
  29.  
  30. int r0 = 0;    // 4051 multiplexers s0 pin(s) value
  31. int r1 = 0;    // 4051 multiplexers s1 pin(s) value
  32. int r2 = 0;    // 4051 multiplexers s2 pin(s) value
  33. int row = 0;   // Binary value for setting the 4051 s* pins
  34. int bin [] = {000, 1, 10, 11, 100, 101, 110, 111}; // binary array for setting 4051 s* pins
  35. int s_delay = 5; // Microseconds of delay between sending serial messages
  36.  
  37. byte data = 0; // Temporary holder for input values
  38.  
  39. int LEDpin = 13; // Debug LED pin
  40.  
  41. int s0pin = 2; // s0 pin for the 4051 chips
  42. int s1pin = 3; // s1 pin for the 4051 chips
  43. int s2pin = 4; // s2 pin for the 4051 chips
  44. int zpin1 = 0; // set the analog Z pin for the first 4051 chip
  45. int zpin2 = 1; // set the analog Z pin for the second 4051 chip
  46. int zpin3 = 7; // set the digital Z pin for the third 4051 chip
  47.  
  48. void setup() {
  49.   if (debug_on == 0) {
  50.     Serial.begin(31250); // MIDI Serial Output
  51.   } else {
  52.     Serial.begin(38400); // Debugging Serial Output
  53.   }
  54.   pinMode(s0pin,OUTPUT); // Enable the s0 output pin on the Atmega
  55.   pinMode(s1pin,OUTPUT); // Enable the s1 output pin on the Atmega
  56.   pinMode(s2pin,OUTPUT); // Enable the s2 output pin on the Atmega
  57. }
  58.  
  59. void loop() {
  60.   delayMicroseconds(20);  // Small 20ns delay to let 4051 chips catch up
  61.  
  62.   for(byte i = 0; i <= 7; i++) {  // Loop to iterate through all 8 input pins on the 4051
  63.     // Setup the 4051 multiplexers to read the pins we want
  64.     row = bin[i];
  65.     r0  = row & 0x01;
  66.     r1  = (row>>1) & 0x01;
  67.     r2  = (row>>2) & 0x01;
  68.     digitalWrite(s0pin, r0); // Set the s0 pin(s)
  69.     digitalWrite(s1pin, r1); // Set the s1 pin(s)
  70.     digitalWrite(s2pin, r2); // Set the s2 pin(s)
  71.  
  72.     // Read in the pot value from the first 4051
  73.     data = analogRead(zpin1)/8;
  74.     if(data_ana_cc_a[i] != data) {
  75.       sendmsg(status_byte, ana_cc_a[i], data);
  76.       data_ana_cc_a[i] = data;
  77.       blink();
  78.     }
  79.  
  80.     // Read in the pot value from the second 4051
  81.     data = analogRead(zpin2)/8;
  82.     if(data_ana_cc_b[i] != data) {
  83.       sendmsg(status_byte, ana_cc_b[i], data);
  84.       data_ana_cc_b[i] = data;
  85.       blink();
  86.     }
  87.    
  88.     // Read in the button value from the third 4051
  89.     data = digitalRead(zpin3);
  90.     if(data_dig_cc_a[i] != data) {
  91.       if (data == HIGH) {
  92.         note_msg = note_on;
  93.         note_v   = note_on_v;
  94.       } else {
  95.         note_msg = note_off;
  96.         note_v   = note_off_v;
  97.       }
  98.       sendmsg(note_msg, dig_cc_a[i], note_v);
  99.       data_dig_cc_a[i] = data;
  100.       blink();
  101.     }    
  102.   }
  103. }
  104.  
  105. // Sends MIDI messages with a command byte and two data bytes
  106. void sendmsg(byte command, byte data1, byte data2) {
  107.   if (debug_on == 0) {
  108.     Serial.print(command);
  109.     delayMicroseconds(s_delay);
  110.     Serial.print(data1);
  111.     delayMicroseconds(s_delay);
  112.     Serial.print(data2);
  113.     delayMicroseconds(s_delay);
  114.   } else {
  115.     if (command == status_byte) {
  116.       Serial.print ("Controller Message: ");
  117.       Serial.print (data1, DEC);
  118.       Serial.print (" - ");
  119.       Serial.println (data2, DEC);
  120.     } else if (command == note_on) {
  121.       Serial.print ("Note on: ");
  122.       Serial.println (data1, DEC);
  123.     } else if (command == note_off) {
  124.       Serial.print ("Note off: ");
  125.       Serial.println (data1, DEC);
  126.     } else {
  127.       Serial.println ("BORK!");
  128.     }
  129.   }
  130. }
  131.  
  132. // Blink the LED light once
  133. void blink() {
  134.       digitalWrite(LEDpin, HIGH);
  135.       delay(50);
  136.       digitalWrite(LEDpin, LOW);
  137.       delay(50);
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement