Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #include "MIDIUSB.h"
  2. #define debug SERIAL_PORT_MONITOR
  3. #define ledPin A2
  4. #define Keys 32
  5. #define debugStart
  6. //comment out to disable delay and printing during setup,
  7. //the purpose is to show the pins for matrix through l2c
  8.  
  9. const int rows[] = {5, 4, 3, 2, 1, 0, 21, 20};
  10. const int cols[] = {6, 8, 10, 12, 7, 9, 11, 13};
  11. const int rowCount = 8;
  12. const int colCount = 8;
  13.  
  14. //Loop count and delay variables
  15. //Loopcount
  16. unsigned long startTime;
  17. unsigned long loopCount = 0;
  18. unsigned long looptimer = 0;
  19. //Delay
  20. int printTime = 30;
  21. int printDelay = 600;
  22. unsigned long time_now = 0;
  23.  
  24. //Debounce variables
  25. int debounceSampleMax = 40; // number of millis/samples to consider before declaring a debounced input
  26. int debounceRead[colCount][rowCount]; // the current value read from the input pin
  27. int debounceState[colCount][rowCount]; // the debounced input value
  28. int debounceSampleCount[colCount][rowCount]; // how many times we have seen new value
  29. long debounceTime[colCount][rowCount]; // the last time the output pin was sampled
  30. //
  31.  
  32. //Normal rows: {8,9,10,11,12,13,14,15};
  33. //Inverted rows:{15,14,13,12,11,10,9,8};
  34.  
  35. const int keynote[colCount][rowCount] = {
  36. {1,9,17,25,33,41,49,57},
  37. {2,10,18,26,34,42,50,58},
  38. {3,11,19,27,35,43,51,59},
  39. {4,12,20,28,36,44,52,60},
  40. {5,13,21,29,37,45,53,61},
  41. {6,14,22,30,38,46,54,62},
  42. {7,15,23,31,39,47,55,63},
  43. {8,16,24,32,40,48,56,64}
  44. };
  45. int firstKey=36;
  46. int num_keys = Keys;
  47. int channel=0;
  48. int keys[colCount][rowCount];
  49. int keys_prev[colCount][rowCount];
  50. int noteIndex[48];
  51. int noteCount;
  52.  
  53. void setup() {
  54. debug.begin(115200);
  55. // use default address 0
  56. // pinMode(0, INPUT);
  57. // pinMode(0, HIGH); // turn on a 100K pullup internally
  58. #ifdef debugStart
  59. delay(2500);
  60. debug.println("*****************START*******************");
  61. #endif
  62. for (int x = 0; x < rowCount; x++) {
  63. #ifdef debugStart
  64. debug.print(x + 1); debug.print("/"); debug.print(rowCount);
  65. debug.print(": "); debug.print("Pin #"); debug.print(rows[x]); debug.println(" as input");
  66. debug.println("************************************");
  67. delay(250);
  68. #endif
  69. pinMode(rows[x], INPUT);
  70. delay(100);
  71.  
  72. }
  73.  
  74. for (int x = 0; x < colCount; x++) {
  75. #ifdef debugStart
  76. debug.print(x + 1);
  77. debug.print("/");
  78. debug.print(colCount);
  79. debug.print(": ");
  80. debug.print("Pin #"); debug.print(cols[x]); debug.println(" as input-pullup");
  81. debug.println("************************************");
  82. delay(250);
  83. #endif
  84. delay(100);
  85. pinMode(cols[x], INPUT_PULLUP);
  86.  
  87.  
  88.  
  89. }
  90.  
  91. }
  92.  
  93. void scanMatrix()
  94. {
  95. for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
  96. for (int colIndex=0; colIndex < colCount; colIndex++) {
  97. int rx=keys[colIndex][rowIndex];
  98. debounceMatrix(colIndex, rowIndex, rx);
  99. }
  100. }
  101. }
  102.  
  103. void readMatrix() {
  104. for (int colIndex = 0; colIndex < colCount; colIndex++) {
  105. // col: set to output to low
  106. int curCol = cols[colIndex];
  107. pinMode(curCol, OUTPUT);
  108. digitalWrite(curCol, LOW);
  109. // row: interate through the rows
  110. for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  111. int rowCol = rows[rowIndex];
  112. pinMode(rowCol, INPUT_PULLUP);
  113. keys[colIndex][rowIndex] = digitalRead(rowCol);
  114. //int rx=digitalRead(rowCol);
  115.  
  116. pinMode(rowCol, INPUT);
  117. // debounceMatrix(colIndex, rowIndex, rx);
  118. }
  119. // disable the column
  120. pinMode(curCol, INPUT);
  121. }
  122. }
  123.  
  124.  
  125. void debounceMatrix(int colIndex, int rowIndex , int rx)
  126. {
  127. // if (rowCol != keys_prev[colIndex][rowIndex]) //If the rowCol (state) is different from last time scanned
  128. // {
  129. // If we have gone on to the next millisecond
  130. if(millis() != debounceTime[colIndex][rowIndex])
  131. {
  132. keys[colIndex][rowIndex] = rx;
  133. if(rx == keys_prev[colIndex][rowIndex] && debounceSampleCount[colIndex][rowIndex] > 0)
  134. {
  135. debounceSampleCount[colIndex][rowIndex]--;
  136. //debounceSampleCount[colIndex][rowIndex]=debounceSampleCount[colIndex][rowIndex]-1;
  137. }
  138. if(rx != keys_prev[colIndex][rowIndex])
  139. {
  140. debounceSampleCount[colIndex][rowIndex]++;
  141. //debounceSampleCount[colIndex][rowIndex]=debounceSampleCount[colIndex][rowIndex]+1;
  142. }
  143. // If the Input has shown the same value for long enough let's switch it
  144. if(debounceSampleCount[colIndex][rowIndex] >= debounceSampleMax)
  145. {
  146. debounceSampleCount[colIndex][rowIndex] = 0;
  147. keys_prev[colIndex][rowIndex] = rx;
  148. showMatrix(colIndex, rowIndex, rx);
  149. // debug.print("KEY STATE IS DIFFERENT: "); debug.print("current="); debug.print(rx); debug.print("//"); //ebug.print("last known="); debug.println(keys_prev[colIndex][rowIndex]);
  150. }
  151. debounceTime[colIndex][rowIndex] = millis();
  152. } // keys_prev[colIndex][rowIndex] = rowCol; //save the new state
  153. }
  154.  
  155. void showMatrix(int colIndex, int rowIndex, int rx)
  156. {
  157. debug.print("column[");
  158. debug.print(colIndex);
  159. debug.print("] row[");
  160. debug.print(rowIndex);
  161. debug.print("]");
  162. debug.print(" = ");
  163. debug.println(rx);
  164. }
  165.  
  166. void noteOn(int channel, int pitch, int velocity) {
  167. midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  168. MidiUSB.sendMIDI(noteOn);
  169. MidiUSB.flush();
  170. debug.print("MIDI | NOTE ON: ");
  171. debug.print(pitch);
  172. debug.print(" / ");
  173. debug.println(pitch, HEX);
  174. }
  175.  
  176. void noteOff(int channel, int pitch, int velocity) {
  177. midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  178. MidiUSB.sendMIDI(noteOff);
  179. MidiUSB.flush();
  180. debug.print("MIDI | NOTE OFF: ");
  181. debug.print(pitch);
  182. debug.print(" / ");
  183. debug.println(pitch, HEX);
  184. }
  185.  
  186. void loop() {
  187. readMatrix();
  188. scanMatrix();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement