/** $10 Drum machine (sequencer) Jun 09 By A. Stein Released under GPL v3 Description: Simple sequencer machine which uses Capacitative Sensing Code for Arduinos. This is a combination drumpad and sequencer. It has just two modes, record, and playback, and needs very few components; just 3 resistors and a piezo speaker. If you're feeling decadent, you can add an LED (with a resistor) for more "ooomph". Full info at corticalcafe.com **/ //some definitions #define NOBUTTON_VAL 0 #define BUTTON1_VAL 1 #define BUTTON2_VAL 2 #define BUTTON3_VAL 3 #define MAXSAMPS 500 //max number of samples //variable declarations int capSenseThresh=5; //initial cap sense threshold. Will be changed on first call to initCapSensor. //set pins int button1Pin=8; //drum pad pin definitions int button2Pin=7; int button3Pin=6; int buttonGndPin=2; //common gnd for all buttons, connect a 1M ohm resistor from here to each button in use int ledPin=4; //LED pin int piezoPin=3; //speaker pin char beat[MAXSAMPS]; //store our data here int sampRate=10; //sample rate (msec) int numSamps=MAXSAMPS; //number of currently stored samples int freqs[]={2093, 2794, 3136}; //frequencies for the drum pads (check freq out to choose nice ones, like C, F, and G //our obligatory setup routine void setup() { flashLED(ledPin, 1); // beepPiezo(piezoPin, 1); initCapSensor(buttonGndPin, button1Pin); //at the sketch start, initialize on an untouched button flashLED(ledPin, 1); // beepPiezo(piezoPin, 1); Serial.begin(9600); } //our obligatory main routine void loop () { long tmpTime; flashLED(ledPin, 3); playTone(3000, 100); //tell user we're recording tmpTime=millis(); recordSamples(); tmpTime=millis()-tmpTime; Serial.print("Recording Time: "); Serial.print(tmpTime,DEC); Serial.print(" ms.\n"); numSamps=getNumSamples(); //find where stored samples end (and loop repeats) printSamples(); //visualize samples modMedianFilter(); //filter printSamples(); //visualize samples Serial.print("Num Samples: "); Serial.print(numSamps, DEC); Serial.println(); do{ tmpTime=millis(); playSamples(); tmpTime=millis()-tmpTime; Serial.print("Play Time: "); Serial.print(tmpTime,DEC); Serial.print(" ms.\n"); } while(getButtonTouched()==NOBUTTON_VAL); //loop playback until user is ready to record again } /** returns the value of the button which was touched. Assumes only 1 sensor is touched at a time. **/ int getButtonTouched(){ if(getTouched(getRawCapSense(buttonGndPin, button1Pin))) return(BUTTON1_VAL); else if(getTouched(getRawCapSense(buttonGndPin, button2Pin))) return(BUTTON2_VAL); else if(getTouched(getRawCapSense(buttonGndPin, button3Pin))) return(BUTTON3_VAL); else return(NOBUTTON_VAL); } /* record samples from touch pads */ void recordSamples(){ long time; Serial.print("Recording...\t"); //don't start recording until a button is pressed while(getButtonTouched()==NOBUTTON_VAL) { delay(10); } for(int i=0;i=capSenseThresh); } /* Sets capSenseThresh with a reasonable value by averaging a handful of rawCapSense values. The button should be UNTOUCHED when this routine is running (usually at the sketch start). */ void initCapSensor(int txPin, int rxPin) { int tmpCap=0; int numSamples=10; for (int i=0; i