talofer99 icon

MIDI Example

talofer99 | PRO | 04/24/18 08:22:12 AM UTC | 0 ⭐ | 278 👁️ | Never ⏰ | []
text |

2.12 KB

|

None

|

0 👍

/

0 👎

/* 
MIDI TEST 2
 Analog A0 - POT
 */
 int pot = A0;
int note = 75;
int cmd = 0X90; // play
 const int buttonPin = 2;    // the number of the pushbutton pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
  void setup() {
  //  Set MIDI baud rate:
  Serial.begin(115200);//31250
 // Serial.println("System Started");
 // set pin mode
  pinMode(buttonPin, INPUT);
}
 void loop() {
   //read the velocity
  int  velocity =map( analogRead(pot),0,1023,0,127);
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
   // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:
   // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
   if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
     // if the button state has changed:
    if (reading != buttonState) {
      // set the reading
      buttonState = reading;
      // set the command
      cmd = buttonState == HIGH?0x90:0X80;
     // play
      noteOn(cmd, note, velocity);
      //delay 
      delay(10);      
    } // end if reading != buttonState
  } //end if ((millis() - lastDebounceTime) > debounceDelay) 
   // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
 } // end void loop
 //  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

Comments