Advertisement
LandoRo

MultiButton_Array_full_14_corrected

Sep 10th, 2022 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.03 KB | None | 0 0
  1. const int channel = 1; // global midi channel
  2. const int buttonPinL = 3; // set array rows with easy naming
  3. const int buttonPinR = 14; // set array colums with easy naming
  4.  
  5. int buttonPin[buttonPinL][buttonPinR] = {
  6.   {0, 3, 6, 9, 12, 26, 29, 32, 21, 18, 15, 41, 38, 35}, // 1.65v comparaor threshold buttons
  7.   {1, 4, 7, 10, 24, 27, 30, 23, 20, 17, 14, 40, 37, 34}, // 3.3v comparator threshold buttons
  8.   {2, 5, 8, 11, 25, 28, 31, 22, 19, 16, 13, 39, 36, 33 } // damper off buttons
  9. };
  10.  
  11. volatile unsigned long timerA [14] = {0, 3, 6, 9, 12, 26, 29, 32, 21, 18, 15, 41, 38, 35}; // 1.65v comparaor timing
  12. volatile unsigned long timerB [14] = {1, 4, 7, 10, 24, 27, 30, 23, 20, 17, 14, 40, 37, 34}; // 3.3v comparaor timing
  13. volatile unsigned long timerPassed [14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};// timeb-timea
  14. volatile boolean triggerA [14] = {0, 3, 6, 9, 12, 26, 29, 32, 21, 18, 15, 41, 38, 35};// timer validation used to trigger sound
  15. volatile boolean triggerB [14] = {1, 4, 7, 10, 24, 27, 30, 23, 20, 17, 14, 40, 37, 34};
  16.  
  17. float maxVelocityTime = 30000; // my slowest amount of time passed between 1.65 and 3.3v threshold
  18.  
  19. int velocity1;
  20. float velocityFloat1;
  21. float velocityFloatCorrected1;
  22.  
  23. void int1 () // ISR for threshold time stamps 1.65v
  24. {
  25.   if (!triggerA [0])
  26.   {
  27.     timerA [0] = micros () ;
  28.     triggerA [0] = true ;
  29.   }
  30. }
  31.  
  32. void int2 () // // ISR for threshold time stamps 3.3v
  33. {
  34.   if (triggerA [0] && !triggerB [0])
  35.   {
  36.     timerB [0] = micros () ;
  37.     triggerB [0] = true ;
  38.   }
  39. }
  40.  
  41. void int3 ()
  42. {
  43.   if (!triggerA [1])
  44.   {
  45.     timerA [1] = micros () ;
  46.     triggerA [1] = true ;
  47.   }
  48. }
  49.  
  50. void int4 ()
  51. {
  52.   if (triggerA [1] && !triggerB [1])
  53.   {
  54.     timerB [1] = micros () ;
  55.     triggerB [1] = true ;
  56.   }
  57. }
  58.  
  59. void int5 () // ISR for threshold time stamps 1.65v
  60. {
  61.   if (!triggerA [2])
  62.   {
  63.     timerA [2] = micros () ;
  64.     triggerA [2] = true ;
  65.   }
  66. }
  67.  
  68. void int6 () // // ISR for threshold time stamps 3.3v
  69. {
  70.   if (triggerA [2] && !triggerB [2])
  71.   {
  72.     timerB [2] = micros () ;
  73.     triggerB [2] = true ;
  74.   }
  75. }
  76.  
  77. void int7 () // ISR for threshold time stamps 1.65v
  78. {
  79.   if (!triggerA [3])
  80.   {
  81.     timerA [3] = micros () ;
  82.     triggerA [3] = true ;
  83.   }
  84. }
  85.  
  86. void int8 () // // ISR for threshold time stamps 3.3v
  87. {
  88.   if (triggerA [3] && !triggerB [3])
  89.   {
  90.     timerB [3] = micros () ;
  91.     triggerB [3] = true ;
  92.   }
  93. }
  94.  
  95. void int9 () // ISR for threshold time stamps 1.65v
  96. {
  97.   if (!triggerA [4])
  98.   {
  99.     timerA [4] = micros () ;
  100.     triggerA [4] = true ;
  101.   }
  102. }
  103.  
  104. void int10 () // // ISR for threshold time stamps 3.3v
  105. {
  106.   if (triggerA [4] && !triggerB [4])
  107.   {
  108.     timerB [4] = micros () ;
  109.     triggerB [4] = true ;
  110.   }
  111. }
  112.  
  113. void int11 ()
  114. {
  115.   if (!triggerA [5])
  116.   {
  117.     timerA [5] = micros () ;
  118.     triggerA [5] = true ;
  119.   }
  120. }
  121.  
  122. void int12 ()
  123. {
  124.   if (triggerA [5] && !triggerB [6])
  125.   {
  126.     timerB [5] = micros () ;
  127.     triggerB [5] = true ;
  128.   }
  129. }
  130.  
  131. void int13 () // ISR for threshold time stamps 1.65v
  132. {
  133.   if (!triggerA [6])
  134.   {
  135.     timerA [6] = micros () ;
  136.     triggerA [6] = true ;
  137.   }
  138. }
  139.  
  140. void int14 () // // ISR for threshold time stamps 3.3v
  141. {
  142.   if (triggerA [6] && !triggerB [6])
  143.   {
  144.     timerB [6] = micros () ;
  145.     triggerB [6] = true ;
  146.   }
  147. }
  148.  
  149. void int15 () // ISR for threshold time stamps 1.65v
  150. {
  151.   if (!triggerA [7])
  152.   {
  153.     timerA [7] = micros () ;
  154.     triggerA [7] = true ;
  155.   }
  156. }
  157.  
  158. void int16 () // // ISR for threshold time stamps 3.3v
  159. {
  160.   if (triggerA [7] && !triggerB [7])
  161.   {
  162.     timerB [7] = micros () ;
  163.     triggerB [7] = true ;
  164.   }
  165. }
  166.  
  167. void int17 () // ISR for threshold time stamps 1.65v
  168. {
  169.   if (!triggerA [8])
  170.   {
  171.     timerA [8] = micros () ;
  172.     triggerA [8] = true ;
  173.   }
  174. }
  175.  
  176. void int18 () // // ISR for threshold time stamps 3.3v
  177. {
  178.   if (triggerA [8] && !triggerB [8])
  179.   {
  180.     timerB [8] = micros () ;
  181.     triggerB [8] = true ;
  182.   }
  183. }
  184.  
  185. void int19 ()
  186. {
  187.   if (!triggerA [9])
  188.   {
  189.     timerA [9] = micros () ;
  190.     triggerA [9] = true ;
  191.   }
  192. }
  193.  
  194. void int20 ()
  195. {
  196.   if (triggerA [9] && !triggerB [9])
  197.   {
  198.     timerB [9] = micros () ;
  199.     triggerB [9] = true ;
  200.   }
  201. }
  202.  
  203. void int21 () // ISR for threshold time stamps 1.65v
  204. {
  205.   if (!triggerA [10])
  206.   {
  207.     timerA [10] = micros () ;
  208.     triggerA [10] = true ;
  209.   }
  210. }
  211.  
  212. void int22 () // // ISR for threshold time stamps 3.3v
  213. {
  214.   if (triggerA [10] && !triggerB [10])
  215.   {
  216.     timerB [10] = micros () ;
  217.     triggerB [10] = true ;
  218.   }
  219. }
  220.  
  221. void int23 () // ISR for threshold time stamps 1.65v
  222. {
  223.   if (!triggerA [11])
  224.   {
  225.     timerA [11] = micros () ;
  226.     triggerA [11] = true ;
  227.   }
  228. }
  229.  
  230. void int24 () // // ISR for threshold time stamps 3.3v
  231. {
  232.   if (triggerA [11] && !triggerB [11])
  233.   {
  234.     timerB [11] = micros () ;
  235.     triggerB [11] = true ;
  236.   }
  237. }
  238.  
  239. void int25 () // ISR for threshold time stamps 1.65v
  240. {
  241.   if (!triggerA [12])
  242.   {
  243.     timerA [12] = micros () ;
  244.     triggerA [12] = true ;
  245.   }
  246. }
  247.  
  248. void int26 () // // ISR for threshold time stamps 3.3v
  249. {
  250.   if (triggerA [12] && !triggerB [12])
  251.   {
  252.     timerB [12] = micros () ;
  253.     triggerB [12] = true ;
  254.   }
  255. }
  256.  
  257. void int27 ()
  258. {
  259.   if (!triggerA [13])
  260.   {
  261.     timerA [13] = micros () ;
  262.     triggerA [13] = true ;
  263.   }
  264. }
  265.  
  266. void int28 ()
  267. {
  268.   if (triggerA [13] && !triggerB [13])
  269.   {
  270.     timerB [13] = micros () ;
  271.     triggerB [13] = true ;
  272.   }
  273. }
  274.  
  275. void setup ()
  276. { for (int j = 0; j < buttonPinL ; j++) {
  277.     for (int i = 0; i < buttonPinR ; i++) {
  278.       pinMode(buttonPin[j][i] , INPUT_PULLUP);
  279.  
  280.       triggerA [i] = false ;// timing debounce
  281.       triggerB [i] = false ;
  282.  
  283.  
  284.     }
  285.   }
  286.   attachInterrupt (buttonPin[0][0] , int1, RISING) ;
  287.   attachInterrupt (buttonPin[0][1] , int3, RISING) ;
  288.   attachInterrupt (buttonPin[0][2] , int5, RISING) ;
  289.   attachInterrupt (buttonPin[0][3] , int7, RISING) ;
  290.   attachInterrupt (buttonPin[0][4] , int9, RISING) ;
  291.   attachInterrupt (buttonPin[0][5] , int11, RISING) ;
  292.   attachInterrupt (buttonPin[0][6] , int13, RISING) ;
  293.   attachInterrupt (buttonPin[0][7] , int15, RISING) ;
  294.   attachInterrupt (buttonPin[0][8] , int17, RISING) ;
  295.   attachInterrupt (buttonPin[0][9] , int19, RISING) ;
  296.   attachInterrupt (buttonPin[0][10] , int21, RISING) ;
  297.   attachInterrupt (buttonPin[0][11] , int23, RISING) ;
  298.   attachInterrupt (buttonPin[0][12] , int25, RISING) ;
  299.   attachInterrupt (buttonPin[0][13] , int27, RISING) ;
  300.  
  301.  
  302.   attachInterrupt (buttonPin[1][0] , int2, RISING) ;
  303.   attachInterrupt (buttonPin[1][1] , int4, RISING) ;
  304.   attachInterrupt (buttonPin[1][2] , int6, RISING) ;
  305.   attachInterrupt (buttonPin[1][3] , int8, RISING) ;
  306.   attachInterrupt (buttonPin[1][4] , int10, RISING) ;
  307.   attachInterrupt (buttonPin[1][5] , int12, RISING) ;
  308.   attachInterrupt (buttonPin[1][6] , int14, RISING) ;
  309.   attachInterrupt (buttonPin[1][7] , int16, RISING) ;
  310.   attachInterrupt (buttonPin[1][8] , int18, RISING) ;
  311.   attachInterrupt (buttonPin[1][9] , int20, RISING) ;
  312.   attachInterrupt (buttonPin[1][10] , int22, RISING) ;
  313.   attachInterrupt (buttonPin[1][11] , int24, RISING) ;
  314.   attachInterrupt (buttonPin[1][12] , int26, RISING) ;
  315.   attachInterrupt (buttonPin[1][13] , int28, RISING) ;
  316.  }
  317. void loop () {
  318.   byte offButton[14] = {2, 5, 8, 11, 25, 28, 31, 22, 19, 16, 13, 39, 36, 33};
  319.   byte midiNote[14] = {60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73};
  320.  
  321.   for (int i = 0; i < buttonPinR ; i++) {
  322.  
  323.     offButton[i] = digitalRead(buttonPin[2][i]);
  324.  
  325.     if (triggerB [i]) // when both thresholds are proven process note
  326.     {
  327.       timerPassed [i] = (timerB [i] - timerA [i]);
  328.       //Serial.println (timePassed1) ;
  329.  
  330.       velocityFloat1 = 127 * maxVelocityTime / timerPassed [i];
  331.       if (velocityFloat1 > 127)
  332.       {
  333.         velocity1 = 127;
  334.       }
  335.       else
  336.       {
  337.  
  338.         velocityFloatCorrected1 = sqrt(127 * velocityFloat1);
  339.         velocity1 = velocityFloatCorrected1;
  340.       }
  341.       usbMIDI.sendNoteOn(midiNote[i], velocity1, 1);
  342.  
  343.       noInterrupts () ;
  344.       triggerB [i] = triggerA [i] = false ;
  345.       interrupts () ;
  346.     }
  347.     if ( offButton[i] == LOW) {
  348.       usbMIDI.sendNoteOff(midiNote[i], 0, 1);
  349.  
  350.     }
  351.   }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement