// what midi channel we're sending on // ranges from 0-15 #define drumchan 9 // general midi drum notes #define note_bassdrum 35 #define note_snaredrum 38 #define note_hihatclosed 42 #define note_hihatopen 46 #define note_crash 49 #define note_ride 51 // define the pins we use #define switchAPin A1 #define switchBPin A5 #define switchCPin A3 #define switchDPin A2 #define switchEPin A4 //#define AccPin A9 //#define Acc2Pin A4 //#define AccXPin A2 #define ledPin 13 // for midi out status // analog threshold for piezo sensing int switchAState = 0; int switchBState = 0; int switchCState = 0; int switchDState = 0; int switchEState = 0; int currentBaseState = 0; int currentSnareState = 0; int currentHiHatState = 0; int currentCrashState= 0; int currentRideState=0; int volBase; int volSnare; int ACC; int HT; const int numRreadings =10; const int numLreadings =2; const int DBnumDBreadings = 1; int Rreadings[numRreadings]; // the Rreadings from the analog input int Rindex = 0; // the Rindex of the current reading int Rtotal = 0; // the running Rtotal int Raverage = 0; // the Raverage int RinputPin = A3; int Lreadings[numLreadings]; // the Rreadings from the analog input int Lindex = 0; // the Rindex of the current reading int Ltotal = 0; // the running Rtotal int Laverage = 0; // the Raverage int LinputPin = A5; int DBreadings[DBnumDBreadings]; // the DBreadings from the analog input int DBindex = 0; // the DBindex of the current reading int DBtotal = 0; // the running DBtotal int DBaverage = 0; // the DBaverage int DBinputPin = A1 ; //int HTval; int val,t; void setup() { pinMode(switchAPin, INPUT); pinMode(switchBPin, INPUT); pinMode(switchCPin, INPUT); // pinMode(SensorPin, INPUT); // pinMode(7, INPUT); pinMode(A7, INPUT); //analogRead(SensorPin); // turn on internal pullup // digitalWrite(switchBPin, HIGH); // turn on internal pullup // digitalWrite(switchCPin, HIGH); // turn on internal pullup pinMode(ledPin, OUTPUT); Serial.begin(31250); // set MIDI baud rate // initialize all the Rreadings to 0: { for (int thisReading = 0; thisReading < numRreadings; thisReading++) Rreadings[thisReading] = 0; } for (int thisReading = 0; thisReading < numLreadings; thisReading++) Lreadings[thisReading] = 0; for (int thisReading = 0; thisReading < DBnumDBreadings; thisReading++) DBreadings[thisReading] = 0; } void loop() { // calculate the Raverage: Raverage = Rtotal / numRreadings; // send it to the computer (as ASCII digits) // subtract the last reading: Rtotal= Rtotal - Rreadings[Rindex]; // read from the sensor: Rreadings[Rindex] = analogRead(A3); // add the reading to the Rtotal: Rtotal= Rtotal + Rreadings[Rindex]; // advance to the next position in the array: Rindex = Rindex + 1; // if we're at the end of the array... if (Rindex >= numRreadings) // ...wrap around to the beginning: Rindex = 0; // calculate the Raverage: Raverage = Rtotal / numRreadings; // send it to the computer (as ASCII digits) // subtract the last reading: Ltotal= Ltotal - Lreadings[Lindex]; // read from the sensor: Lreadings[Lindex] = analogRead(A5); // add the reading to the Rtotal: Ltotal= Ltotal + Lreadings[Lindex]; // advance to the next position in the array: Lindex = Lindex + 1; // if we're at the end of the array... if (Lindex >= numLreadings) // ...wrap around to the beginning: Lindex = 0; // calculate the Raverage: Laverage = Ltotal / numLreadings; // send it to the computer (as ASCII digits) // subtract the last reading: DBtotal= DBtotal - DBreadings[DBindex]; // read from the sensor: DBreadings[DBindex] = analogRead(A2); // add the reading to the DBtotal: DBtotal= DBtotal + DBreadings[DBindex]; // advance to the next position in the array: DBindex = DBindex + 1; // if we're at the end of the array... if (DBindex >= DBnumDBreadings) // ...wrap around to the beginning: DBindex = 0; // calculate the DBaverage: DBaverage = DBtotal / DBnumDBreadings; // deal with switchA currentBaseState = analogRead(A1); if( currentBaseState > 50 && switchAState < 50) // push noteOn(drumchan, note_bassdrum, 100); //if( currentBaseState < 10 && switchAState > 10)// release noteOff(drumchan, note_bassdrum, 0); switchAState = currentBaseState; currentHiHatState = Laverage; if(currentHiHatState < 250 && switchBState > 250)// push noteOn(drumchan,HT, 100); // if( currentHiHatState > 250 && switchBState < 250)// release noteOff(drumchan,HT, 0); switchBState = currentHiHatState; // deal with switchC currentSnareState = Raverage; if( currentSnareState > 650& switchCState < 650) // push noteOn(drumchan,note_snaredrum, 127); //if( currentSnareState <400 && switchCState >400)// release noteOff(drumchan,note_snaredrum,0); switchCState = currentSnareState; // deal with switchD currentCrashState = analogRead(A2); if(currentCrashState < 20 && switchDState> 20)// push noteOn(drumchan,note_crash, 100); //if( currentSwitchState<420 && switchAState> 420)// release noteOff(drumchan,note_crash, 0); switchDState = currentCrashState; //currentRideState = analogRead(A4); //if(currentRideState > 450 && switchEState< 450)// push //noteOn(drumchan,note_ride, 100); //if( currentSwitchState<420 && switchAState> 420)// release //noteOff(drumchan,note_ride, 0); //`switchEState = currentRideState; } // 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); { if (analogRead(A0)>400) { HT=note_hihatopen; } else { HT=note_hihatclosed; } } }