More Drum Kit!
Saturday, October 11th, 2008So here’ s my little version of the drum kit. I’ve just used sample code from arduino.cc, but it’s more than enough to get the project up and running. Expect to see it evolve over time, and if you plan on being at Phreaknic you can play with it there!
Arduino Knock Sensor Drum Kit v1.01 from Morgellon on Vimeo.
- Read how to use a piezo buzzer as a knock sensor
http://arduino.cc/en/Tutorial/Knock
- Read how to make a piezo buzzer play various tones, instead of just one!
http://arduino.cc/en/Tutorial/PlayMelody
Here’s the sample code I used, with a few tweaks added:
int ledPin = 13; // led connected to control pin 13
int knockSensor = 0; // the knock sensor will be plugged at analog pin 0
byte val = 0; // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status, to toggle the light
int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not
// TONES ==========================================
// Start by defining the relationship between
// note, period, & frequency.
#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz
// Define a special note, ‘R’, to represent a rest
#define R 0
// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 9;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(speakerOut, OUTPUT);
Serial.begin(9600); // use the serial port
}
// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[],
// which sets each note’s relative length (higher #, longer note)
int melody[] = { C, b, g, C, b, e, C, c };
int beats[] = { 16, 16, 16, 8, 8, 16, 16, 16 };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int tone = 0;
int beat = 0;
long duration = 0;
// PLAY TONE ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
long elapsed_time = 0;
if (tone > 0) { // if this isn’t a Rest beat, while the tone has
// played less long than ‘duration’, pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);
// Keep track of how long we pulsed
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable “val”
if (val >= THRESHOLD) {
// statePin = !statePin; // toggle the status of the ledPin (this trick doesn’t use time cycles)
digitalWrite(ledPin, HIGH); // turn the led on or off
Serial.println(”Knock!”); // send the string “Knock!” back to the computer, followed by newline
for (int i=0; i<MAX_COUNT; i++) {
tone = melody[i];
beat = beats[i];
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes…
delayMicroseconds(pause);
}
delay(10); // short delay to avoid overloading the serial port
}
digitalWrite(ledPin, LOW);
}



