talofer99

MIDI_programChange

Jun 26th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. /*
  2.    This examples shows how to make a simple seven keys MIDI keyboard with volume control
  3.  
  4.    Created: 4/10/2015
  5.    Author: Arturo Guadalupi <[email protected]>
  6.    
  7.    http://www.arduino.cc/en/Tutorial/MidiDevice
  8. */
  9.  
  10. #include "MIDIUSB.h"
  11. #include "PitchToNote.h"
  12. #define NUM_BUTTONS  7
  13.  
  14. const uint8_t button1 = 2;
  15. const uint8_t button2 = 3;
  16. const uint8_t button3 = 4;
  17. const uint8_t button4 = 5;
  18. const uint8_t button5 = 6;
  19. const uint8_t button6 = 7;
  20. const uint8_t button7 = 8;
  21.  
  22. const int intensityPot = 0;  //A0 input
  23.  
  24. const uint8_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7};
  25. const byte notePitches[NUM_BUTTONS] = {C3, D3, E3, F3, G3, A3, B3};
  26.  
  27. uint8_t notesTime[NUM_BUTTONS];
  28. uint8_t pressedButtons = 0x00;
  29. uint8_t previousButtons = 0x00;
  30. uint8_t intensity;
  31.  
  32. void setup() {
  33.   for (int i = 0; i < NUM_BUTTONS; i++)
  34.     pinMode(buttons[i], INPUT_PULLUP);
  35. }
  36.  
  37.  
  38. void loop() {
  39.   readButtons();
  40.   readIntensity();
  41.   playNotes();
  42. }
  43.  
  44. // First parameter is the event type (0x0B = control change).
  45. // Second parameter is the event type, combined with the channel.
  46. // Third parameter is the control number number (0-119).
  47. // Fourth parameter is the control value (0-127).
  48.  
  49. void controlChange(byte channel, byte control, byte value) {
  50.   midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  51.   MidiUSB.sendMIDI(event);
  52. }
  53.  
  54. void readButtons()
  55. {
  56.   for (int i = 0; i < NUM_BUTTONS; i++)
  57.   {
  58.     if (digitalRead(buttons[i]) == LOW)
  59.     {
  60.       bitWrite(pressedButtons, i, 1);
  61.       delay(50);
  62.     }
  63.     else
  64.       bitWrite(pressedButtons, i, 0);
  65.   }
  66. }
  67.  
  68. void readIntensity()
  69. {
  70.   int val = analogRead(intensityPot);
  71.   intensity = (uint8_t) (map(val, 0, 1023, 0, 127));
  72. }
  73.  
  74. void playNotes()
  75. {
  76.   for (int i = 0; i < NUM_BUTTONS; i++)
  77.   {
  78.     if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
  79.     {
  80.       if (bitRead(pressedButtons, i))
  81.       {
  82.         bitWrite(previousButtons, i , 1);
  83.         noteOn(0, notePitches[i], intensity);
  84.         MidiUSB.flush();
  85.       }
  86.       else
  87.       {
  88.         bitWrite(previousButtons, i , 0);
  89.         noteOff(0, notePitches[i], 0);
  90.         MidiUSB.flush();
  91.       }
  92.     }
  93.   }
  94. }
  95.  
  96. // First parameter is the event type (0x09 = note on, 0x08 = note off).
  97. // Second parameter is note-on/note-off, combined with the channel.
  98. // Channel can be anything between 0-15. Typically reported to the user as 1-16.
  99. // Third parameter is the note number (48 = middle C).
  100. // Fourth parameter is the velocity (64 = normal, 127 = fastest).
  101.  
  102. void noteOn(byte channel, byte pitch, byte velocity) {
  103.   midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  104.   MidiUSB.sendMIDI(noteOn);
  105. }
  106.  
  107. void noteOff(byte channel, byte pitch, byte velocity) {
  108.   midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  109.   MidiUSB.sendMIDI(noteOff);
  110. }
  111.  
  112. void programChange (byte channel, byte data) {
  113.   midiEventPacket_t programChange = {0x0C, 0xC0 | channel, data};
  114.   MidiUSB.sendMIDI(programChange);
  115. }
Add Comment
Please, Sign In to add comment