Sunday, April 10, 2011

Mess Up with Arduino! Digital Vibrate Sensor Part 2

After some experiment, here is the final look of my Arduino Vibrate Sensor:



Here is the code for upload into Arduino chip:


// ranges from 0-15
#define drumchan 1

// general midi drum notes
#define note_bassdrum 36

// define the pins we use
#define switchAPin 3
#define piezoAPin 0
#define ledPin 13 // for midi out status

// analog threshold for piezo sensing
#define PIEZOTHRESHOLD 100

int switchAState = LOW;
int currentSwitchState = LOW;
int val,t;

unsigned char state = 0;

void setup() {
pinMode(switchAPin, INPUT);
digitalWrite(switchAPin, HIGH); // turn on internal pullup

pinMode(ledPin, OUTPUT);
Serial.begin(57600); // set MIDI baud rate
}

void loop() {
// deal with switchA
currentSwitchState = digitalRead(switchAPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(drumchan, note_bassdrum, 100);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(drumchan, note_bassdrum, 0);
switchAState = currentSwitchState;

// deal with first piezo, this is kind of a hack
val = analogRead(piezoAPin);
if( val >= PIEZOTHRESHOLD ) {
t=0;
while(analogRead(piezoAPin) >= PIEZOTHRESHOLD/2) {
t++;
}
}
}

// channel ranges from 0-15
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x99 | channel), note, velocity);
}

// Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
midiMsg( (0x89 | 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 blink()//Interrupts function
{
state++;
}


The problem that the output is piano sound because of the wrong channel of midi sound. This is the piano sound code:


// channel ranges from 0-15
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}


The channel for percussion type instrument sound is 9. So I've modify the code like below:


// channel ranges from 0-15
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x99 | channel), note, velocity);
}


And wohoo! Drum sound is finally comes out! Although the sound is not very nice when played, and there are some codes still need to modify further, this is a big step toward my success!

Reference:
1. http://www.pjb.com.au/muscript/gm.html#perc
2. http://todbot.com/blog/2006/10/29/spooky-arduino-projects-4-and-musical-arduino/
3. http://todbot.com/arduino/sketches/midi_drum_kit/midi_drum_kit.pde

No comments:

Post a Comment