midi contoroller続き

せっかくだからコードも載せておきます。サンプルをちょっといじっただけですが。
以下、コード。

//#define pushVal 1 //for push-off switches.
#define pushVal 0 //Otherwise,use this for push-on switches.

#define nd 4 //number of digital switches.
#define msg 176 //controll change. note-on:144 note-off:128
int inPin[nd] = { 4,6,8,10 }; // select the input pins for the switches
int gndPin[nd] = {5,7,9,11}; //sellect the input pins' ground.
int val1[nd] = {70,71,72,73}; // control No or note No
int val2[nd] ; //controll value or velocity
long time[nd] ; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

int reading;

void setup()
{
for(int i = 0 ; i < nd ; i++) {
pinMode(inPin[i], INPUT);
digitalWrite(inPin[i],HIGH);
pinMode(gndPin[i],OUTPUT);
digitalWrite(gndPin[i],LOW);
time[i] = millis();

val2[i] = digitalRead(inPin[i]);
val2[i] = 127 * ( ( val2[i] && pushVal ) || ( !val2[i] && !pushVal ) );
}
Serial.begin(31250);
}

void loop()
{
for(int i = 0 ; i < nd ; i++ ){

reading = digitalRead(inPin[i]); // read the value from the sensor
reading = 127 * ( ( reading && pushVal ) || ( !reading && !pushVal ) );

if(reading != val2[i] && millis() - time[i] > debounce){
val2[i] = reading ;
MIDI_TX(msg,val1[i],val2[i]);
time[i] = millis();
}
}
}


void MIDI_TX(int MESSAGE, int VAL1, int VAL2)
{
Serial.print(MESSAGE,BYTE);
Serial.print(VAL1,BYTE);
Serial.print(VAL2,BYTE);
}



ここまで。(コピペしたらなんだか滅茶苦茶になっていたので直しました。今度は動くと思います。)

コメント