Advertisement
talofer99

MIDI Example

Apr 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. /*
  2. MIDI TEST 2
  3.  
  4. Analog A0 - POT
  5.  
  6. */
  7.  
  8. int pot = A0;
  9. int note = 75;
  10. int cmd = 0X90; // play
  11.  
  12. const int buttonPin = 2; // the number of the pushbutton pin
  13. int buttonState; // the current reading from the input pin
  14. int lastButtonState = LOW; // the previous reading from the input pin
  15. long lastDebounceTime = 0; // the last time the output pin was toggled
  16. long debounceDelay = 50; // the debounce time; increase if the output flickers
  17.  
  18.  
  19. void setup() {
  20. // Set MIDI baud rate:
  21. Serial.begin(115200);//31250
  22. // Serial.println("System Started");
  23. // set pin mode
  24. pinMode(buttonPin, INPUT);
  25. }
  26.  
  27. void loop() {
  28.  
  29. //read the velocity
  30. int velocity =map( analogRead(pot),0,1023,0,127);
  31. // read the state of the switch into a local variable:
  32. int reading = digitalRead(buttonPin);
  33.  
  34. // check to see if you just pressed the button
  35. // (i.e. the input went from LOW to HIGH), and you've waited
  36. // long enough since the last press to ignore any noise:
  37.  
  38. // If the switch changed, due to noise or pressing:
  39. if (reading != lastButtonState) {
  40. // reset the debouncing timer
  41. lastDebounceTime = millis();
  42. }
  43.  
  44. if ((millis() - lastDebounceTime) > debounceDelay) {
  45. // whatever the reading is at, it's been there for longer
  46. // than the debounce delay, so take it as the actual current state:
  47.  
  48. // if the button state has changed:
  49. if (reading != buttonState) {
  50. // set the reading
  51. buttonState = reading;
  52. // set the command
  53. cmd = buttonState == HIGH?0x90:0X80;
  54. // play
  55. noteOn(cmd, note, velocity);
  56. //delay
  57. delay(10);
  58. } // end if reading != buttonState
  59. } //end if ((millis() - lastDebounceTime) > debounceDelay)
  60.  
  61. // save the reading. Next time through the loop,
  62. // it'll be the lastButtonState:
  63. lastButtonState = reading;
  64.  
  65. } // end void loop
  66.  
  67. // plays a MIDI note. Doesn't check to see that
  68. // cmd is greater than 127, or that data values are less than 127:
  69. void noteOn(int cmd, int pitch, int velocity) {
  70. Serial.write(cmd);
  71. Serial.write(pitch);
  72. Serial.write(velocity);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement