Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Multi-channel MIDI Controller
- Using Atmel Atmega168 Microcontroller
- 4051 Multiplexers
- Expandable by adding additional 4051 multiplexers or other shift register/multiplexer chips
- Jon Marler <[email protected]> - December 2008
- */
- int debug_on = 0; // Set to 1 to enable debug serial output at 38400 baud
- byte status_byte = 0xB0; // Continuous Controler Message
- byte note_on = 0x90; // Note On Message
- byte note_off = 0x80; // Note Off Message
- byte note_on_v = 0x40; // Note on velocity
- byte note_off_v = 0x00; // Note off velocity
- byte note_v = 0x00; // Current note velocity
- byte note_msg = 0x00; // Current note message
- byte ana_cc_a[] = { 20, 21, 22, 23, 24, 25, 26, 27 }; // Controller numbers for the first set of pots
- byte ana_cc_b[] = { 28, 29, 30, 31, 32, 33, 34, 35 }; // Controller numbers for the second set of pots
- byte dig_cc_a[] = { 36, 37, 38, 39, 40, 41, 42, 43 }; // Set of notes for the digital buttons
- byte data_ana_cc_a[] = { 255, 255, 255, 255, 255, 255, 255, 255 }; // Array to store first set of pot values
- byte data_ana_cc_b[] = { 255, 255, 255, 255, 255, 255, 255, 255 }; // Array to store second set of pot values
- byte data_dig_cc_a[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Array to store current button values
- int r0 = 0; // 4051 multiplexers s0 pin(s) value
- int r1 = 0; // 4051 multiplexers s1 pin(s) value
- int r2 = 0; // 4051 multiplexers s2 pin(s) value
- int row = 0; // Binary value for setting the 4051 s* pins
- int bin [] = {000, 1, 10, 11, 100, 101, 110, 111}; // binary array for setting 4051 s* pins
- int s_delay = 5; // Microseconds of delay between sending serial messages
- byte data = 0; // Temporary holder for input values
- int LEDpin = 13; // Debug LED pin
- int s0pin = 2; // s0 pin for the 4051 chips
- int s1pin = 3; // s1 pin for the 4051 chips
- int s2pin = 4; // s2 pin for the 4051 chips
- int zpin1 = 0; // set the analog Z pin for the first 4051 chip
- int zpin2 = 1; // set the analog Z pin for the second 4051 chip
- int zpin3 = 7; // set the digital Z pin for the third 4051 chip
- void setup() {
- if (debug_on == 0) {
- Serial.begin(31250); // MIDI Serial Output
- } else {
- Serial.begin(38400); // Debugging Serial Output
- }
- pinMode(s0pin,OUTPUT); // Enable the s0 output pin on the Atmega
- pinMode(s1pin,OUTPUT); // Enable the s1 output pin on the Atmega
- pinMode(s2pin,OUTPUT); // Enable the s2 output pin on the Atmega
- }
- void loop() {
- delayMicroseconds(20); // Small 20ns delay to let 4051 chips catch up
- for(byte i = 0; i <= 7; i++) { // Loop to iterate through all 8 input pins on the 4051
- // Setup the 4051 multiplexers to read the pins we want
- row = bin[i];
- r0 = row & 0x01;
- r1 = (row>>1) & 0x01;
- r2 = (row>>2) & 0x01;
- digitalWrite(s0pin, r0); // Set the s0 pin(s)
- digitalWrite(s1pin, r1); // Set the s1 pin(s)
- digitalWrite(s2pin, r2); // Set the s2 pin(s)
- // Read in the pot value from the first 4051
- data = analogRead(zpin1)/8;
- if(data_ana_cc_a[i] != data) {
- sendmsg(status_byte, ana_cc_a[i], data);
- data_ana_cc_a[i] = data;
- blink();
- }
- // Read in the pot value from the second 4051
- data = analogRead(zpin2)/8;
- if(data_ana_cc_b[i] != data) {
- sendmsg(status_byte, ana_cc_b[i], data);
- data_ana_cc_b[i] = data;
- blink();
- }
- // Read in the button value from the third 4051
- data = digitalRead(zpin3);
- if(data_dig_cc_a[i] != data) {
- if (data == HIGH) {
- note_msg = note_on;
- note_v = note_on_v;
- } else {
- note_msg = note_off;
- note_v = note_off_v;
- }
- sendmsg(note_msg, dig_cc_a[i], note_v);
- data_dig_cc_a[i] = data;
- blink();
- }
- }
- }
- // Sends MIDI messages with a command byte and two data bytes
- void sendmsg(byte command, byte data1, byte data2) {
- if (debug_on == 0) {
- Serial.print(command);
- delayMicroseconds(s_delay);
- Serial.print(data1);
- delayMicroseconds(s_delay);
- Serial.print(data2);
- delayMicroseconds(s_delay);
- } else {
- if (command == status_byte) {
- Serial.print ("Controller Message: ");
- Serial.print (data1, DEC);
- Serial.print (" - ");
- Serial.println (data2, DEC);
- } else if (command == note_on) {
- Serial.print ("Note on: ");
- Serial.println (data1, DEC);
- } else if (command == note_off) {
- Serial.print ("Note off: ");
- Serial.println (data1, DEC);
- } else {
- Serial.println ("BORK!");
- }
- }
- }
- // Blink the LED light once
- void blink() {
- digitalWrite(LEDpin, HIGH);
- delay(50);
- digitalWrite(LEDpin, LOW);
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement