피에조 드럼 패드 코드 수정본

낮에 보내드린 소스코드 수정본입니다!!!
(아래 코드 본문에 마우스를 대면 우측상단에 붙여넣기 아이콘이 나옵니다. 붙여넣기 편리해요~)

//////////////////////////////////////////////////////////////////////////

//PIEZO DRUM PAD
//reference: http://spikenzielabs.com/SpikenzieLabs/DrumKitKit.html
//piezo 6: A0~A5
//pot 2: A8, A9
//effectbutton 4: digital 0~3 
//footswitch 1: digital 4
//LED 6: digital 5~10

//////////////////////////////////////////////////////////////////////////

////////////
// piezo
////////////

unsigned char PadNote[6] = {60,62,64,65,66,68};         // MIDI notes from 0 to 127 (Mid C = 60 // C3, D3, E3, F3, F#3, G#3)
int PadCutOff[6] = {300,300,300,300,300,300};           // Minimum Analog value to cause a drum hit
long piezoNoteOnTime=0;
int MaxPlayTime[6] = {10,10,10,10,10};                  // Cycles before a 2nd hit is allowed
boolean VelocityFlag  = true;    

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 piezoPin = 0;     
int piezoVal = 0;

////////////
// pot 
////////////

int analogPin1 = A8;//Modula
int analogPin2 = A9;//Modula
int mod = 0; int oldmod =0; int oldermod =0;int olderermod =0; // modulation wheel
int mod1 = 0; int oldmod1 =0; int oldermod1 =0;int olderermod1 =0; // modulation wheel

////////////
// effect button
////////////

int effectPin[4] = {0,1,2,3};              
int effectState[4] = {0,0,0,0};                
int effectFlag[4]  = {0,0,0,0};
int midistate = 0;

////////////
// footswitch
////////////

int switchPin = 4;              // footswitch is connected to pin 4
int midiChannel = 1;            // MIDI channel from 1 to 6
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int switchState;                // variable to hold the button state

////////////
// led
////////////

int ledPin1 = 5;
int ledPin2 = 6;
int ledPin3 = 7;
int ledPin4 = 8;
int ledPin5 = 9;
int ledPin6 = 10;

int piezo1 = A0;
int piezo2 = A1;
int piezo3 = A2;
int piezo4 = A3;
int piezo5 = A4;
int piezo6 = A5;

int threshold = 50;  

int piezoValue1=0;
int piezoValue2=0; 
int piezoValue3=0;
int piezoValue4=0; 
int piezoValue5=0;
int piezoValue6=0; 
int ledState = LOW;        



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



void setup() 
{
  Serial.begin(9600);                                 
  
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
}



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

////////////
// piezo
////////////


void loop() 


{
  for(int piezoPin=0; piezoPin < 6; piezoPin++)
  {
    piezoVal = analogRead(piezoPin);                              
    if((piezoVal > PadCutOff[piezoPin]))
    {
      if((activePad[piezoPin] == false))
      {
        if(VelocityFlag == true) piezoVal = (piezoVal / 8) - 1 ;
        else piezoVal = 127;
        
        usbMIDI.sendNoteOn(PadNote[piezoPin],piezoVal,midiChannel);
        PinPlayTime[piezoPin] = 0;
        activePad[piezoPin] = true;
      }
      else
      {
        PinPlayTime[piezoPin] = PinPlayTime[piezoPin] + 1;
      }
    }
    else if((activePad[piezoPin] == true))
    {
      PinPlayTime[piezoPin] = PinPlayTime[piezoPin] + 1;
      
      if(PinPlayTime[piezoPin] > MaxPlayTime[piezoPin])
      {
        activePad[piezoPin] = false;
        usbMIDI.sendNoteOff(PadNote[piezoPin],127,midiChannel);	
      }
    }
  } 
  
////////////
// pot 
////////////

mod = (analogRead(analogPin1)/8); // read the analog input (0~1023 -> 0~127)
mod1 = (analogRead(analogPin2)/8); // read the analog input (0~1023 -> 0~127)

if (mod != oldmod && mod != oldermod && mod != olderermod) // modulation wheel
{
usbMIDI.sendControlChange(15, (mod), 1 );// 
// Serial.print("mod"); Serial.println(mod);
olderermod = oldermod;
oldermod = oldmod;
oldmod = mod; 
}



if ( mod1 !=oldmod1 && mod1 != oldermod1 && mod1 != olderermod1) // modulation wheel
{
usbMIDI.sendControlChange(16, (mod1), 1 );// 
// Serial.print("mod"); Serial.println(mod);
olderermod1 = oldermod1;
oldermod1 = oldmod1;
oldmod1 = mod1; 
}

////////////
// effect button
////////////
  
   for(int i=0; i<4; i++){
   effectState[i] = digitalRead(effectPin[i]);
 
 if (effectState[i] == HIGH && effectFlag[i] == 0)
 {
   if (midistate == 0)
   {
    usbMIDI.sendControlChange(i, 80, midiChannel);
    midistate = 1;
    }
    
else if (midistate == 1)
    {
     usbMIDI.sendControlChange(i, 0, midiChannel);
     midistate = 0;
    }
    
   effectFlag[i] = 1;
 }
 
 if (effectState[i] == LOW)
 {
  effectFlag[i] = 0; 
 }
}

////////////////
// footswitch //
////////////////
 
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2)
  {                 // make sure we got 2 consistant readings!
    if (val != switchState)
    {          // the button state has changed!
      if (val == LOW)
      {                // check if the button is pressed
        if (midiChannel == 1) midiChannel = 2;
        else if(midiChannel == 2) midiChannel = 3;
        else if(midiChannel == 3) midiChannel = 4;
        else if(midiChannel == 4) midiChannel = 5;
        else if(midiChannel == 5) midiChannel = 6;
        else if(midiChannel == 6) midiChannel = 1;
       }
    }
    switchState = val;                 // save the new state in our variable
  }


////////////
// led
////////////


   piezoValue1 = analogRead(piezo1)/4;  
   piezoValue2 = analogRead(piezo2)/4;  
   piezoValue3 = analogRead(piezo3)/4;  
   piezoValue4 = analogRead(piezo4)/4;
   piezoValue5 = analogRead(piezo5)/4;  
   piezoValue6 = analogRead(piezo6)/4;      
      
    if ( piezoValue1 >= threshold) digitalWrite(ledPin1,HIGH);
    else digitalWrite(ledPin1, LOW);
    
    if ( piezoValue2 >= threshold) digitalWrite(ledPin2,HIGH);
    else digitalWrite(ledPin2, LOW);
    
    if ( piezoValue3 >= threshold) digitalWrite(ledPin3,HIGH);
    else digitalWrite(ledPin3, LOW);
    
    if ( piezoValue4 >= threshold) digitalWrite(ledPin4,HIGH);
    else digitalWrite(ledPin4, LOW);
    
    if ( piezoValue5 >= threshold) digitalWrite(ledPin5,HIGH);
    else digitalWrite(ledPin5, LOW);
    
    if ( piezoValue6 >= threshold) digitalWrite(ledPin6,HIGH);
    else digitalWrite(ledPin6, LOW);
    
    
}
      
     
Advertisements