Go Down

Topic: Electronical drum kit (Arduino, Linux, Hydrogen) (Read 3845 times) previous topic - next topic

Heis

Jul 13, 2010, 11:55 pm Last Edit: Jul 14, 2010, 12:18 am by Heis Reason: 1
Hello Arduino board users!

I have wanted for a long time to make my own electrical drum kit, but before i stumbled upon Arduino i used to think i had to buy some expensive MIDI brain for it to work.

Upon discovering arduino and its ability to control MIDI softsynths, I decided to try to build one that would interface with hydrogen through a JACK connection on my legacy laptop with Ubuntu studio installed.

I do not want the signal to travel via an external midi device, but rather via the USB port. To do this, i have found ttymidi to be a great tool. I have written some code to make a button that triggers a midi note to test the feasibility of the project, and so far so good.

I have now ordered the "SpikenzieLabs Drum Kit - Kit" with two extra piezos and hope to be able to write most of the code myself. I am a total rookie to Arduino, and a moderate level MATLAB programmer and Linux user.

Code for the button test:
Code: [Select]
//Drum Button Code
//needs arumidi.h which is supplied with ttymidi

#include <ardumidi.h>      //Midi library from ttymidi

int ledPin = 13;           //want the internal LED to light when note is on
int buttonPort = 2;        //button connected to digital input 2
int value = 0;             //definition of the value to decide whether to giv note on signal or not
int rplay = 0;             //define a variable to tell if the note status is already on
int rstop = 0;             //define a variable to tell if the note status is already off

void setup()
{
 Serial.begin(115200);            
 pinMode(ledPin, OUTPUT);         //LED is lit when button is pushed
 pinMode(buttonPort, INPUT);      //The button is used as input to play the midi note
}

void loop()
{
 value = digitalRead(buttonPort);  //Is the button pushed?
 if (value == HIGH){               //if YES
   if (rplay != 1){                //if note is not already on
     midi_note_on(0, 36, 127);     //play note 36, which corresponds to first drum in hydrogen list
     rplay = 1;                    //set last ON/OFF action to ON
     rstop = 0;                    //
   }
   digitalWrite(ledPin, HIGH);     //light up LED
 }
 else{                             //If button is not pushed
   if (rstop != 1){                //If last ON/OFF action is ON
     midi_note_off(0, 36, 127);    //Turn off the midi note
     rplay = 0;                    //Set last ON/OFF action to OFF
     rstop = 1;                    //
   }
   digitalWrite(ledPin, LOW);      //turn off LED
 }
}


I am usually very bad at commenting my code, but i made an effort this time.

It might be very obvious through the button placement and stuff like this that I learned what little I know of Arduino programming from the webcast tutorial of Button controlled LED, and the buttons circuit is set up in the same way as in that tutorial.

Hoping for feedback on the code and ideas for the project.

Heis

#1
Jul 15, 2010, 01:05 am Last Edit: Jul 15, 2010, 01:13 am by Heis Reason: 1
Ok. I am still waiting for the stuff to arrive. Meanwhile i have been looking at the arduino code SpikenzieLabs provide with this kit, and modified it in such a way that it to the best of my understanding now should send a signal via ttymidi each time a piezo is being struck above its sensitivity threshold and the note is not already playing.  

Sensitivity adjustments and playtime limit must be figured out after assembly.

Also it would be interesting to have the note value output  of one of the instruments change as a response to a digital button input so that it would be possible to have both open and closed hi-hat on the same drum, controlled with a pedal, like real drums work. This should be a piece of cake.

The code after first glance and slight modifications:
Code: [Select]
#include <ardumidi.h>                                   //Midi library from ttymidi

//*******************************************************************************************************************
// User settable variables                  
//*******************************************************************************************************************

unsigned char PadNote[6] = {36,37,38,39,40,41};         // MIDI notes from 36 to 41 (the six first drums of Hydrogen)

int PadCutOff[6] = {600,600,600,600,600,600};           // Minimum Analog value to cause a drum hit

int MaxPlayTime[6] = {90,90,90,90,90,90};               // Cycles before a 2nd hit is allowed

#define  midichannel      0;                              // MIDI channel from 0 to 15 (+1 in "real world")

//*******************************************************************************************************************
// Internal Use Variables                  
//*******************************************************************************************************************

boolean activePad[6] = {0,0,0,0,0,0};                   // Array of flags of pad currently playing
int PinPlayTime[6] = {0,0,0,0,0,0};                     // Counter since pad started to play

unsigned char status;

int pin = 0;    
int hitavg = 0;

//*******************************************************************************************************************
// Setup                  
//*******************************************************************************************************************

void setup()
{
 Serial.begin(115200);                                  // connect to the serial port 115200
}

//*******************************************************************************************************************
// Main Program                  
//*******************************************************************************************************************

void loop()
{
 for(int pin=0; pin < 6; pin++)
 {
   hitavg = analogRead(pin);                              // read the input pin

   if((hitavg > PadCutOff[pin]))
   {
     if((activePad[pin] == false))
     {
//          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
         hitavg = (hitavg / 8) -1 ;                                                 // Upper range
       

       midi_note_on(0,PadNote[pin],hitavg);
       PinPlayTime[pin] = 0;
       activePad[pin] = true;
     }
     else
     {
       PinPlayTime[pin] = PinPlayTime[pin] + 1;
     }
   }
   else if((activePad[pin] == true))
   {
     PinPlayTime[pin] = PinPlayTime[pin] + 1;
     
     if(PinPlayTime[pin] > MaxPlayTime[pin])
     {
       activePad[pin] = false;
       midi_note_off(0,PadNote[pin],127);
     }
   }
 }
}


I was utterly pleased after noticing how easy it was to get the button to control Hydrogen output yesterday. Huge thanks are due to the publisher of ttymidi!

If this works well, I would also be intrigued about adding more drums to the equation, but i would require more analog input pins. Is there a way to expand the Duemilanove, or will i have to buy a Mega?

ghendrix

Hi,
Thanks for your code. As a beginner myself It came in really handy.
Any new developments? Pictures?
Thanks again.

Go Up