==========================================================================

Two Sketches that compile and have been uploaded to PIC

==========================================================================

/*

* MIDI Drum Kit

* -------------

* Convert Arduino to a MIDI controller using various inputs and

* the serial port as a MIDI output.

*

* This sketch is set up to send General MIDI (GM) drum notes

* on MIDI channel 1, but it can be easily reconfigured for other

* notes and channels

*

* It uses switch inputs to send MIDI notes of a fixed velocity with

* note on time determined by duration of keypress and it uses

* piezo buzzer elements as inputs to send MIDI notes of a varying velocity

* & duration, depending on forced of impulse imparted to piezo sensor.

*

* To send MIDI, attach a MIDI out jack (female DIN-5) to Arduino.

* DIN-5 pinout is: _____

* pin 2 - Gnd / \

* pin 4 - 220 ohm resistor to +5V | 3 1 | MIDI jack

* pin 5 - Arduino D1 (TX) | 5 4 |

* all other pins - unconnected \__2__/

* On my midi jack, the color of the wires for the pins are:

* 3 = n/c

* 5 = black (blue)

* 2 = red (red)

* 4 = orange (yellow)

* 1 = brown

*

* Based off of Tom Igoe's work at:

* http://itp.nyu.edu/physcomp/Labs/MIDIOutput

*

* Created 25 October 2006

* copyleft 2006 Tod E. Kurt <tod@todbot.com

* http://todbot.com/

*

* Updates:

* - 2 May 2009 - fixed noteOn() and noteOff()

*

*/

// what midi channel we're sending on

// ranges from 0-15

#define drumchan 1

// general midi drum notes

#define note_bassdrum 35

#define note_snaredrum 38

#define note_hihatclosed 42

#define note_hihatopen 44

#define note_crash 49

#define note_59 59

#define note_60 60 //Middle C

#define note_61 61

#define note_62 62

#define note_63 63

#define note_64 64

#define note_65 65

#define note_66 66

#define note_67 67

#define note_68 68

#define note_69 69

#define note_70 70

#define note_71 71

#define note_72 72

#define note_73 73

#define note_74 74

#define note_75 75

#define note_76 76

#define note_77 77

#define note_78 78

#define note_79 79

#define note_80 80

#define note_81 81

#define note_82 82

#define note_83 83

#define note_84 84

#define note_86 85

#define note_86 86

#define note_87 87

#define note_88 88

#define note_89 89

// define the pins we use

#define audinoAnolog0Pin 0

#define audinoAnolog1Pin 1

#define audinoAnolog2Pin 2

#define audinoAnolog3Pin 3

//Mux A

#define readInZero 0

#define readInTwo 0

#define readInFour 0

#define readInSix 0

#define readInSeven 0

#define readInFive 0

#define readInThree 0

#define readInOne 0

//Mux B

#define readInEight 1

#define readInNine 1

#define readInTen 1

#define readInEleven 1

#define readInTweleve 1

#define readInThirteen 1

#define readInFourteen 1

#define readInFifteen 1

//Mux C

#define readInSixteen 2

#define readInSeventeen 2

#define readInEighteen 2

#define readInNineteen 2

#define readInTwenty 2

#define readInTwentyOne 2

#define readInTwentyTwo 2

#define readInTwentyThree 2

//Mux D

#define readInTwentyFour 3

#define readInTwentyFive 3

#define readInTwentySix 3

#define readInTwentySeven 3

#define readInTwentyEight 3

#define readInTwentyNine 3

#define readInThirty 3

#define readInThirtyOne 3

#define midi out pin D1 TX // for midi cable

#define ledPin 13 // for midi out status

// analog threshold for piezo sensing

#define PIEZOTHRESHOLD 100

 

 

int val,t;

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(31250); // set MIDI baud rate

}

void loop() {

// deal with Zero piezo, this is kind of a hack

val = analogRead(readInZero);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInZero) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_59, t*2);

delay(t);

noteOff(drumchan,note_59,0);

// deal with 1st piezo, this is kind of a hack

val = analogRead(readInOne);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInOne) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_60, t*2);

delay(t);

noteOff(drumchan,note_60,0);

}

// deal with 2nd piezo, this is kind of a hack

val = analogRead(readInTwo);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInTwo) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_61, t*2);

delay(t);

noteOff(drumchan,note_61,0);

}

// deal with 3rd piezo, this is kind of a hack

val = analogRead(readInThree);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInThree) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_62, t*2);

delay(t);

noteOff(drumchan,note_62,0);

}

// deal with 4th piezo, this is kind of a hack

val = analogRead(readInFour);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInFour) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_63, t*2);

delay(t);

noteOff(drumchan,note_63,0);

}

// deal with 5th piezo, this is kind of a hack

val = analogRead(readInFive);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInFive) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_64, t*2);

delay(t);

noteOff(drumchan,note_64,0);

}

// deal with 6th piezo, this is kind of a hack

val = analogRead(readInSix);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInSix) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_65, t*2);

delay(t);

noteOff(drumchan,note_65,0);

}

// deal with 7th piezo, this is kind of a hack

val = analogRead(readInSeven);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInSeven) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_66, t*2);

delay(t);

noteOff(drumchan,note_66,0);

}

// deal with 8th piezo, this is kind of a hack

val = analogRead(readInEight);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInEight) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_67, t*2);

delay(t);

noteOff(drumchan,note_67,0);

}

// deal with 9th piezo, this is kind of a hack

val = analogRead(readInNine);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInNine) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_68, t*2);

delay(t);

noteOff(drumchan,note_68,0);

}

// deal with 10th piezo, this is kind of a hack

val = analogRead(readInTen);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInTen) >= PIEZOTHRESHOLD/2)

t++;

}

noteOn(drumchan,note_69, t*2);

delay(t);

noteOff(drumchan,note_69,0);

}

// deal with 11th piezo, this is kind of a hack

val = analogRead(readInEleven);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInEleven) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_70, t*2);

delay(t);

noteOff(drumchan,note_70,0);

}

// deal with 12th piezo, this is kind of a hack

val = analogRead(readInTweleve);

if( val >= PIEZOTHRESHOLD ) {

t=0;

while(analogRead(readInTweleve) >= PIEZOTHRESHOLD/2) {

t++;

}

noteOn(drumchan,note_71, t*2);

delay(t);

noteOff(drumchan,note_71,0); }

}

// Send a MIDI note-on message. Like pressing a piano key

// channel ranges from 0-15

void noteOn(byte channel, byte note, byte velocity) {

midiMsg( (0x90 | channel), note, velocity);

}

// Send a MIDI note-off message. Like releasing a piano key

void noteOff(byte channel, byte note, byte velocity) {

midiMsg( (0x80 | channel), note, velocity);

}

// Send a general MIDI message

void midiMsg(byte cmd, byte data1, byte data2) {

digitalWrite(ledPin,HIGH); // indicate we're sending MIDI data

Serial.print(cmd, BYTE);

Serial.print(data1, BYTE);

Serial.print(data2, BYTE);

digitalWrite(ledPin,LOW);

}

==============================================================

==============================================================

void setup()

{

//4051Mux A B C & D digital control pins // Mux A Part//

pinMode(8, OUTPUT); // s0

pinMode(9, OUTPUT); // s1

pinMode(10, OUTPUT); // s2

pinMode(5, OUTPUT); // s3

pinMode(6, OUTPUT); // s4

pinMode(7, OUTPUT); // s5

pinMode(2, OUTPUT); // s6

pinMode(3, OUTPUT); // s7

pinMode(4, OUTPUT); // s8

pinMode(0, OUTPUT); // s9

pinMode(11, OUTPUT); // s10

pinMode(12, OUTPUT); // s11

Serial.begin(9600);

}

void loop()

{

//Read Value of 4051 analog-in 0 by setting the values of s0,s1 and s2

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

delay(10); //not sure if this delay is strictly necessary

int readInZero = analogRead(0); // read the input pin

Serial.print(readInZero); //use the result

//

digitalWrite(8, LOW);

digitalWrite(9, HIGH);

digitalWrite(10, LOW);

delay(10);

int readInTwo= analogRead(0); // read the input pin

Serial.print(readInTwo); //use the result

//

digitalWrite(8, HIGH);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

delay(10);

int readInFour= analogRead(0); // read the input pin

Serial.print(readInFour); //use the result

//

digitalWrite(8, HIGH);

digitalWrite(9, HIGH);

digitalWrite(10, LOW);

delay(10);

int readInSix= analogRead(0); // read the input pin

Serial.print(readInSix); //use the result

//

digitalWrite(8, HIGH);

digitalWrite(9, HIGH);

digitalWrite(10, HIGH);

delay(10);

int readInSeven= analogRead(0); // read the input pin

Serial.print(readInSeven); //use the result

//

digitalWrite(8, HIGH);

digitalWrite(9, LOW);

digitalWrite(10, HIGH);

delay(10);

int readInFive= analogRead(0); // read the input pin

Serial.print(readInFive); //use the result

//

digitalWrite(8, LOW);

digitalWrite(9, HIGH);

digitalWrite(10, HIGH);

delay(10);

int readInThree= analogRead(0); // read the input pin

Serial.print(readInThree); //use the result

//

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, HIGH);

delay(10);

int readInOne= analogRead(0); // read the input pin

Serial.print(readInOne); //use the result

 

 

//Read Value of 4051 analog-in 1 by setting the values of s3,s4 and s5

digitalWrite(5, LOW);

digitalWrite(6, LOW);

digitalWrite(7, LOW);

delay(10); //not sure if this delay is strictly necessary

int readInEight = analogRead(1); // read the input pin

Serial.print(readInEight); //use the result

//

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

digitalWrite(7, LOW);

delay(10);

int readInTen= analogRead(1);// read the input pin

Serial.print(readInTen); //use the result

//

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

digitalWrite(7, LOW);

delay(10);

int readInTwelve= analogRead(1);// read the input pin

Serial.print(readInTwelve); //use the result

//

digitalWrite(5, HIGH);

digitalWrite(6, HIGH);

digitalWrite(7, LOW);

delay(10);

int readInFourteen= analogRead(1);// read the input pin

Serial.print(readInFourteen); //use the result

//

digitalWrite(5, HIGH);

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

delay(10);

int readInFifteen= analogRead(1);// read the input pin

Serial.print(readInFifteen); //use the result

//

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

digitalWrite(7, HIGH);

delay(10);

int readInThirteen= analogRead(1);// read the input pin

Serial.print(readInThirteen); //use the result

//

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

delay(10);

int readInEleven= analogRead(1);// read the input pin

Serial.print(readInEleven); //use the result

//

digitalWrite(5, LOW);

digitalWrite(6, LOW);

digitalWrite(7, HIGH);

delay(10);

int readInNine= analogRead(1);// read the input pin

Serial.print(readInNine); //use the result

 

 

//Read Value of 4051 analog-in 2 by setting the values of s6,s7 and s8

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

delay(10); //not sure if this delay is strictly necessary

int readInSixteen = analogRead(2); // read the input pin

Serial.print(readInSixteen); //use the result

//

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, LOW);

delay(10);

int readInEighteen= analogRead(2);// read the input pin

Serial.print(readInEighteen); //use the result

//

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

delay(10);

int readInTwenty= analogRead(2);// read the input pin

Serial.print(readInTwenty); //use the result

//

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

digitalWrite(4, LOW);

delay(10);

int readInTwentyTwo= analogRead(2);// read the input pin

Serial.print(readInTwentyTwo); //use the result

//

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

delay(10);

int readInTwentyThree= analogRead(2);// read the input pin

Serial.print(readInTwentyThree); //use the result

//

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

delay(10);

int readInTwentyOne= analogRead(2);// read the input pin

Serial.print(readInTwentyOne); //use the result

//

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

delay(10);

int readInNineteen= analogRead(2);// read the input pin

Serial.print(readInNineteen); //use the result

//

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

delay(10);

int readInSeventeen= analogRead(2);// read the input pin

Serial.print(readInSeventeen); //use the result

 

 

//Read Value of 4051 analog-in 3 by setting the values of s0,s1 and s2

digitalWrite(0, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

delay(10); //not sure if this delay is strictly necessary

int readInTwentyFour = analogRead(3); // read the input pin

Serial.print(readInTwentyFour); //use the result

//

digitalWrite(0, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

delay(10);

int readInTwentySix= analogRead(3);// read the input pin

Serial.print(readInTwentySix); //use the result

//

digitalWrite(0, HIGH);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

delay(10);

int readInTwentyEight= analogRead(3);// read the input pin

Serial.print(readInTwentyEight); //use the result

//

digitalWrite(0, HIGH);

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

delay(10);

int readInThirty= analogRead(3);// read the input pin

Serial.print(readInThirty); //use the result

//

digitalWrite(0, HIGH);

digitalWrite(11, HIGH);

digitalWrite(12, HIGH);

delay(10);

int readInThirtyOne= analogRead(3);// read the input pin

Serial.print(readInThirtyOne); //use the result

//

digitalWrite(0, HIGH);

digitalWrite(11, LOW);

digitalWrite(12, HIGH);

delay(10);

int readInTwentyNine= analogRead(3);// read the input pin

Serial.print(readInTwentyNine); //use the result

//

digitalWrite(0, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, HIGH);

delay(10);

int readInTwentySeven= analogRead(3);// read the input pin

Serial.print(readInTwentySeven); //use the result

//

digitalWrite(0, LOW);

digitalWrite(11, LOW);

digitalWrite(12, HIGH);

delay(10);

int readInTwentyFive= analogRead(3);// read the input pin

Serial.print(readInTwentyFive); //use the result

}

//End Mux Part//

 

 

==============================================================

==============================================================

Comments