/*
MIDI message on serial.
===
Button & Volume (Pitch bend & Modulation)
*/
int analogPin = A0;//Pitchbend
int analogPin2 = A2;//Modulatioin
// potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V
// variable to store the Analog value read
int val = 0; int oldval = 0;int olderval = 0; // pitchbend
int mod = 0; int oldmod =0; int oldermod =0; // modulation wheel
const int button = 2;
int buttonState = 0;
int noteOnFlag = 0;// whether currently noteon or not
void setup()
{
Serial.begin(57600); // midi rate : 31250
pinMode(button, INPUT); // Input Mode
digitalWrite(button, HIGH); // Internal Pullup
}
void loop()
{
buttonState = digitalRead(button); // read the digital input
val = (analogRead(analogPin )); // read the analog input (0~1024)
mod = (analogRead(analogPin2)); // read the analog input (0~1024)
// analog input (Volume) procedure
if (val!=oldval && val != olderval) // pitchbend
{
MIDI_TX(0xE0, (val&0x7)*16, (val>>0x3) );
olderval = oldval;
oldval = val;
}
if (mod !=oldmod && mod != oldermod) // modulation wheel
{
MIDI_TX(0xB0, 0x01, (mod>>0x3) );//
oldermod = oldmod;
oldmod = mod;
}
// digital input (button) procedure
if (buttonState == LOW && noteOnFlag == 0) // button : pressed && noteOnFlag : off
{ MIDI_TX(144, 60, 80); // Transmit noteOn message
noteOnFlag = 1;} // noteOnFlag : Off -> On
if (buttonState == HIGH && noteOnFlag == 1)// button : released && noteOnFlag : on
{ MIDI_TX(128, 60, 80);// Transmit noteOff message
noteOnFlag = 0;}// noteOnFlag : On -> Off
delay(10);
}
void MIDI_TX(unsigned char MESSAGE, unsigned char DATA1, unsigned char DATA2)
{
Serial.print(MESSAGE);
Serial.print(DATA1); // Pitch, or LSB
Serial.print(DATA2);// velocity or MSB
}
응답