0% found this document useful (0 votes)
39 views

Projectsprogrammingarduinodrums

This document contains code for a MIDI drum pad controller that uses piezo sensors. It defines variables for the number of pads, their MIDI note assignments, and analog reading thresholds. It also contains code to read the analog sensor values, detect peaks, determine when notes should trigger, and send MIDI note on/off messages accordingly. The code also includes logic to change the closed hi-hat MIDI note when a foot switch is pressed.

Uploaded by

chivas1111
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Projectsprogrammingarduinodrums

This document contains code for a MIDI drum pad controller that uses piezo sensors. It defines variables for the number of pads, their MIDI note assignments, and analog reading thresholds. It also contains code to read the analog sensor values, detect peaks, determine when notes should trigger, and send MIDI note on/off messages accordingly. The code also includes logic to change the closed hi-hat MIDI note when a foot switch is pressed.

Uploaded by

chivas1111
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//

***********************************************************************************
******************************
// *
*
// * SpikenzieLabs.com
*
// * StanleyProjects.com
*
// *
*
//
***********************************************************************************
******************************
//
// The Hairless MIDI supported
// momentary switch as a hi-hat pedal

byte PadNote[6] = {52, 16, 66, 63, 40, 65}; // MIDI notes from 0 to 127
(Mid C = 60)

int pedal[3] = {2, 5, 70}; // pedal input pin / open


hi-hat piezo pin / closed hi-hat midi value

int PadCutOff[6] = {400, 400, 400, 400, 400, 400}; // 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")

boolean VelocityFlag = false; // Velocity ON (true) or


OFF (false)

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

byte status;

int pin = 0;
int hitavg = 0;

void setup()
{
pinMode(pedal[0], INPUT_PULLUP);
Serial.begin(57600); // connect to the serial
port 115200
}

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

if ((hitavg > PadCutOff[pin]))


{
if ((activePad[pin] == false))
{
if (VelocityFlag == true)
{
hitavg = (hitavg / 3) - 1 ;
}
else
{
hitavg = 127;
}

if (pedal[1] == pin && digitalRead(pedal[0]) == LOW)


{
MIDI_TX(144, pedal[2], hitavg);
}
else
{
MIDI_TX(144, 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;
if (pedal[1] == pin && digitalRead(pedal[0]) == LOW)
{
MIDI_TX(128, pedal[2], hitavg);
}
else
{
MIDI_TX(128, PadNote[pin], 127);
}
}
}
}
}

void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY)


{
status = MESSAGE + midichannel;
Serial.write(status);
Serial.write(PITCH);
Serial.write(VELOCITY);
}

------------------------------------------------------------

//Piezo defines
#define NUM_PIEZOS 8
#define PAD1_THRESHOLD 10
#define PAD2_THRESHOLD 10
#define PAD3_THRESHOLD 10
#define PAD4_THRESHOLD 10
#define PAD5_THRESHOLD 10 //MIN/MAX VELOCITIES/sensitivity...
#define PAD6_THRESHOLD 10
#define PAD7_THRESHOLD 10
#define PAD8_THRESHOLD 10

#define START_SLOT 0 //0 START ANALOG INPUTS

//MIDI note defines for each trigger


#define PAD1_NOTE 34 //pad1 kick + // <these are="" the="" assigned="" midi=""
notes,="" change="" to="" suit.="" #define="" pad2_note="" 38="" pad2="" snare=""
+="" #define="" pad3_note="" 49="" pad3="" crash="" 1="" #define="" pad4_note=""
17="" pad4="" hat="" switches="" midi="" notes="" between="" 17="" 22.(closed=""
open="" hat)="" #define="" pad5_note="" 53="" pad5="" ride="" bell="" +="" (see=""
lines="" 129&231="22)TO" change="" closed="" hat="" sound="" #define=""
pad6_note="" 91="" pad6="" ride="" cymbal="" +="" midi="" note="" numbers..=""
#define="" pad7_note="" 47="" pad7="" mid="" tom="" #define="" pad8_note="" 41=""
pad8="" lo="" floor="" tom="" midi="" defines="" #define="" note_on_cmd="" 0x90=""
#define="" note_off_cmd="" 0x80="" #define="" max_midi_velocity="" 127="" midi=""
baud="" rate="" #define="" serial_rate="" 31250="" program="" defines="" all=""
time="" measured="" in="" milliseconds="" #define="" signal_buffer_size="" 50=""
#define="" peak_buffer_size="" 10="" #define="" max_time_between_peaks="" 10=""
#define="" min_time_between_notes="" 10="" map="" that="" holds="" the="" mux=""
slots="" of="" the="" piezos="" unsigned="" short="" slotmap[num_piezos];="" map=""
that="" holds="" the="" respective="" note="" to="" each="" piezo="" unsigned=""
short="" notemap[num_piezos];="" map="" that="" holds="" the="" respective=""
threshold="" to="" each="" piezo="" unsigned="" short=""
thresholdmap[num_piezos];="" ring="" buffers="" to="" store="" analog="" signal=""
and="" peaks="" short="" currentsignalindex[num_piezos];="" short=""
currentpeakindex[num_piezos];="" unsigned="" short="" signalbuffer[num_piezos]
[signal_buffer_size];="" unsigned="" short="" peakbuffer[num_piezos]
[peak_buffer_size];="" boolean="" noteready[num_piezos];="" unsigned="" short=""
notereadyvelocity[num_piezos];="" boolean="" islastpeakzeroed[num_piezos];=""
unsigned="" long="" lastpeaktime[num_piezos];="" unsigned="" long=""
lastnotetime[num_piezos];="" variables="" used="" for="" switch="" mod="" byte=""
hihat_switch="0;" unsigned="" long="" switch_pressed="0;" void="" setup()="" {=""
serial.begin(serial_rate);="" initialize="" globals="" for(short="" i="0;"
i<num_piezos;="" ++i)="" {="" currentsignalindex[i]="0;" currentpeakindex[i]="0;"
memset(signalbuffer[i],0,sizeof(signalbuffer[i]));=""
memset(peakbuffer[i],0,sizeof(peakbuffer[i]));="" noteready[i]="false;"
notereadyvelocity[i]="0;" islastpeakzeroed[i]="true;" lastpeaktime[i]="0;"
lastnotetime[i]="0;" slotmap[i]="START_SLOT" +="" i;="" }=""
thresholdmap[0]="PAD1_THRESHOLD;" thresholdmap[1]="PAD2_THRESHOLD;"
thresholdmap[2]="PAD3_THRESHOLD;" thresholdmap[3]="PAD4_THRESHOLD;" hi="" hat=""
switch="" controlled="" thresholdmap[4]="PAD5_THRESHOLD;"
thresholdmap[5]="PAD6_THRESHOLD;" thresholdmap[6]="PAD7_THRESHOLD;"
thresholdmap[7]="PAD8_THRESHOLD;" notemap[0]="PAD1_NOTE;" notemap[1]="PAD2_NOTE;"
notemap[2]="PAD3_NOTE;" notemap[3]="PAD4_NOTE;" hi="" hat="" switch=""
controlled="" notemap[4]="PAD5_NOTE;" notemap[5]="PAD6_NOTE;"
notemap[6]="PAD7_NOTE;" notemap[7]="PAD8_NOTE;" }="" void="" loop()="" {=""
unsigned="" long="" currenttime="millis();" if(currenttime=""> (switch_pressed +
2))
{
hihat_switch = (((hihat_switch << 1) | digitalRead(3)) & 3);
if(hihat_switch == 2){
noteFire(22, 127); // <change this="" number="" '22'="" for="" 'open="" hat'=""
sound.="" switch_pressed="currentTime;" }="" }="" for(short="" i="0;"
i<num_piezos;="" ++i)="" {="" get="" a="" new="" signal="" from="" analog=""
read="" unsigned="" short="" newsignal="analogRead(slotMap[i]);" signalbuffer[i]
[currentsignalindex[i]]="newSignal;" if="" new="" signal="" is="" 0=""
if(newsignal="" <="" thresholdmap[i])="" {="" if(!islastpeakzeroed[i]="" &&=""
(currenttime="" -="" lastpeaktime[i])=""> MAX_TIME_BETWEEN_PEAKS)
{
recordNewPeak(i,0);
}
else
{
//get previous signal
short prevSignalIndex = currentSignalIndex[i]-1;
if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;
unsigned short prevSignal = signalBuffer[i][prevSignalIndex];
unsigned short newPeak = 0;
//find the wave peak if previous signal was not 0 by going
//through previous signal values until another 0 is reached
while(prevSignal >= thresholdMap[i])
{
if(signalBuffer[i][prevSignalIndex] > newPeak)
{
newPeak = signalBuffer[i][prevSignalIndex];
}
//decrement previous signal index, and get previous signal
prevSignalIndex--;
if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;
prevSignal = signalBuffer[i][prevSignalIndex];
}
if(newPeak > 0)
{
recordNewPeak(i, newPeak);
}
}
}
currentSignalIndex[i]++;
if(currentSignalIndex[i] == SIGNAL_BUFFER_SIZE) currentSignalIndex[i] = 0;
}
}

void recordNewPeak(short slot, short newPeak)


{
isLastPeakZeroed[slot] = (newPeak == 0);
unsigned long currentTime = millis();
lastPeakTime[slot] = currentTime;
//new peak recorded (newPeak)
peakBuffer[slot][currentPeakIndex[slot]] = newPeak;
//1 of 3 cases can happen:
// 1) note ready - if new peak >= previous peak
// 2) note fire - if new peak < previous peak and previous peak was a note ready
// 3) no note - if new peak < previous peak and previous peak was NOT note ready
//get previous peak
short prevPeakIndex = currentPeakIndex[slot]-1;
if(prevPeakIndex < 0) prevPeakIndex = PEAK_BUFFER_SIZE-1;
unsigned short prevPeak = peakBuffer[slot][prevPeakIndex];
if(newPeak > prevPeak && (currentTime - lastNoteTime[slot])>MIN_TIME_BETWEEN_NOTES)
{
noteReady[slot] = true;
if(newPeak > noteReadyVelocity[slot])
noteReadyVelocity[slot] = newPeak;
}
else if(newPeak < prevPeak && noteReady[slot])
{
noteFire(noteMap[slot], noteReadyVelocity[slot]);
noteReady[slot] = false;
noteReadyVelocity[slot] = 0;
lastNoteTime[slot] = currentTime;
}
currentPeakIndex[slot]++;
if(currentPeakIndex[slot] == PEAK_BUFFER_SIZE) currentPeakIndex[slot] = 0;
}

void noteFire(unsigned short note, unsigned short velocity)


{
if(velocity > MAX_MIDI_VELOCITY)
velocity = MAX_MIDI_VELOCITY;
if((note == 17) && (hihat_switch == 0)) //if pad 4 is hit (note 17) and switch is
pressed change note to 22 //
{ //
note = 22; // <change this="" number="" '22'="" for="" 'open="" hat'=""
sound.="" }="" midinoteon(note,="" velocity);="" midinoteoff(note,="" velocity);=""
}="" void="" midinoteon(byte="" note,="" byte="" midivelocity)="" {=""
serial.write(note_on_cmd);="" serial.write(note);="" serial.write(midivelocity);=""
}="" void="" midinoteoff(byte="" note,="" byte="" midivelocity)="" {=""
serial.write(note_off_cmd);="" serial.write(note);=""
serial.write(midivelocity);="" }="">

You might also like