Guest User

Untitled

a guest
Feb 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. // you need MIDIUSB.
  2.  
  3. #include "MIDIUSB.h"
  4. #include "PitchToNote.h"
  5. #define NUM_BUTTONS 16
  6.  
  7. const uint16_t button1 = 2;
  8. const uint16_t button2 = 3;
  9. const uint16_t button3 = 4;
  10. const uint16_t button4 = 5;
  11. const uint16_t button5 = 6;
  12. const uint16_t button6 = 7;
  13. const uint16_t button7 = 8;
  14. const uint16_t button8 = 9;
  15. const uint16_t button9 = 10;
  16. const uint16_t button10 = 14;
  17. const uint16_t button11 = 15;
  18. const uint16_t button12 = 16;
  19. const uint16_t button13 = 18;
  20. const uint16_t button14 = 19;
  21. const uint16_t button15 = 20;
  22. const uint16_t button16 = 21;
  23.  
  24. const uint16_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16};
  25. const byte notePitches[NUM_BUTTONS] = {pitchC2, pitchD2b, pitchD2, pitchE2b, pitchE2, pitchF2, pitchG2b, pitchG2, pitchA2b, pitchA2, pitchB2b, pitchB2, pitchC3, pitchD3b, pitchD3, pitchE3b};
  26.  
  27. uint16_t notesTime[NUM_BUTTONS];
  28. uint16_t pressedButtons = 0x00;
  29. uint16_t previousButtons = 0x00;
  30. uint16_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. playNotes();
  41. }
  42.  
  43. // First parameter is the event type (0x0B = control change).
  44. // Second parameter is the event type, combined with the channel.
  45. // Third parameter is the control number number (0-119).
  46. // Fourth parameter is the control value (0-127).
  47.  
  48. void controlChange(byte channel, byte control, byte value) {
  49. midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  50. MidiUSB.sendMIDI(event);
  51. }
  52.  
  53. void readButtons()
  54. {
  55. for (int i = 0; i < NUM_BUTTONS; i++)
  56. {
  57. if (digitalRead(buttons[i]) == LOW)
  58. {
  59. bitWrite(pressedButtons, i, 1);
  60. delay(2);
  61. }
  62. else
  63. bitWrite(pressedButtons, i, 0);
  64. }
  65. }
  66.  
  67. void playNotes()
  68. {
  69. for (int i = 0; i < NUM_BUTTONS; i++)
  70. {
  71. if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
  72. {
  73. if (bitRead(pressedButtons, i))
  74. {
  75. bitWrite(previousButtons, i , 1);
  76. noteOn(0, notePitches[i], 100);
  77. MidiUSB.flush();
  78. }
  79. else
  80. {
  81. bitWrite(previousButtons, i , 0);
  82. noteOff(0, notePitches[i], 0);
  83. MidiUSB.flush();
  84. }
  85. }
  86. }
  87. }
  88.  
  89. // First parameter is the event type (0x09 = note on, 0x08 = note off).
  90. // Second parameter is note-on/note-off, combined with the channel.
  91. // Channel can be anything between 0-15. Typically reported to the user as 1-16.
  92. // Third parameter is the note number (48 = middle C).
  93. // Fourth parameter is the velocity (64 = normal, 127 = fastest).
  94.  
  95. void noteOn(byte channel, byte pitch, byte velocity) {
  96. midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  97. MidiUSB.sendMIDI(noteOn);
  98. }
  99.  
  100. void noteOff(byte channel, byte pitch, byte velocity) {
  101. midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  102. MidiUSB.sendMIDI(noteOff);
  103. }
Add Comment
Please, Sign In to add comment