Participation

SensorComm Board

최근 사이트 활동

라이선스 안내

DIY 전자악기 1기

// arduino midi workshop 


//Digital Pins
#define push1 2
#define push2 3
#define rotary1 4
#define rotary2 5
#define rotary3 6
#define rotary4 7
#define rotary5 8
#define rotary6 9
#define rotary7 10
#define rotary8 11

#define ledPin 12 


//Analog Pins
#define Xaxis 0 
#define Yaxis 1 
#define Zaxis 2
#define pot1 3 
#define pot2 4 
#define pot3 5 


//MIDI channel: 1
#define midichan 1 


//State 
#define Xout 1
#define Yout 2
#define Zout 3
char pushPushed = 0;
char switchSelect = 0;


//Sensor Data
int XaxisValue = 0;
int YaxisValue = 0;
int ZaxisValue = 0;
int pot1Value = 0;
int pot2Value = 0;
int pot3Value = 0;
int pot1Valueold = 0;
int pot2Valueold = 0;
int pot3Valueold = 0;


void setup() 
  
  pinMode(push1, INPUT); 
  pinMode(push2, INPUT); 
  pinMode(rotary1, INPUT); 
  pinMode(rotary2, INPUT); 
  pinMode(rotary3, INPUT); 
  pinMode(rotary4, INPUT); 
  pinMode(rotary5, INPUT); 
  pinMode(rotary6, INPUT); 
  pinMode(rotary7, INPUT); 
  pinMode(rotary8, INPUT); 

  pinMode(ledPin, OUTPUT); 

  analogReference(EXTERNAL); // AREF = 3.3V
  
  Serial.begin(31250); // set MIDI baud rate 
  


void loop() 
  
  if ( digitalRead(push1) == LOW )
  {
    pushPushed = Xout;
  }
  
  if ( digitalRead(push2) == LOW )
  {
    pushPushed = Yout;
  }
  
  if ( digitalRead(push1) == LOW && digitalRead(push2) == LOW )
  {
    pushPushed = Zout;
  }
  
  if ( digitalRead(push1) == HIGH && digitalRead(push2) == HIGH )
  {
    pushPushed = 0;
  }
  
  
  if ( digitalRead(rotary1) == LOW ) //X, Y, Z, pot1, pot2, pot3
  {
    switchSelect = 1;
    
    if (pushPushed == Xout)
    {
      XaxisValue = Filter(Xaxis, 10);
      controllerChange(1, 0, XaxisValue / 8);
    }
    
    if (pushPushed == Yout)
    {
      YaxisValue = Filter(Yaxis, 10);
      controllerChange(1, 1, YaxisValue / 8);
    }

    if (pushPushed == Zout)
    {
      ZaxisValue = Filter(Zaxis, 10);
      controllerChange(1, 2, ZaxisValue / 8);
    }
    
    pot1Valueold = pot1Value;
    pot1Value = Filter(pot1, 10);
    if (pot1Value != pot1Valueold )
    {
      controllerChange(1, 3, int(pot1Value / 8));
    }
    
    pot2Valueold = pot2Value;
    pot2Value = Filter(pot2, 10);
    if (pot2Value != pot2Valueold )
    {
      controllerChange(1, 4, int(pot2Value / 8));
    }
    
    pot3Valueold = pot3Value;
    pot3Value = Filter(pot3, 10);
    if (pot3Value != pot3Valueold )
    {
      controllerChange(1, 5, int(pot3Value / 8));
    }
        
  }
  
  else if ( digitalRead(rotary2) == LOW )
  {
    switchSelect = 2;
  }
  
  else if ( digitalRead(rotary3) == LOW )
  {
    switchSelect = 3;
  }
  
  else if ( digitalRead(rotary4) == LOW )
  {
    switchSelect = 4;
  }
  
  else if ( digitalRead(rotary5) == LOW )
  {
    switchSelect = 5;
  }
  
  else if ( digitalRead(rotary6) == LOW )
  {
    switchSelect = 6;
  }
  
  else if ( digitalRead(rotary7) == LOW )
  {
    switchSelect = 7;
  }
  
  else if ( digitalRead(rotary8) == LOW )
  {
    switchSelect = 8;
  }
  


// Sensor Data Filtering
int Filter(char pin, int howMany)
{
  
  int data[howMany] ;
  unsigned long total = 0;
  int average = 0;
  
  for ( int i = 0; i < howMany ;i++ )
  {
    data[i]=analogRead(pin);
    total = total + data[i];
  }
  
  average = total / howMany;
  
  return average;
  
}

// Send a MIDI note-on message. 
void noteOn(byte channel, byte note, byte velocity) 
  
  midiMsg( (0x90 | (channel<<4)), note, velocity);
  


// Send a MIDI note-off message.
void noteOff(byte channel, byte note, byte velocity) 
  
  midiMsg( (0x80 | (channel<<4)), note, velocity); 
  


// Send a MIDI controller change message.
void controllerChange(byte channel, byte number, byte value)
{
  
  midiMsg( (0xB0 | (channel<<4)), number, value); 

}


// Send a general MIDI message 
void midiMsg(byte cmd, byte data1, byte data2) 
  
  Serial.print(cmd, BYTE); 
  Serial.print(data1, BYTE); 
  Serial.print(data2, BYTE); 
  
}


Comments