LandoRo

piano code multibuttoned array

May 28th, 2022 (edited)
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 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 = 2; // set array colums with easy naming
  4.  
  5. int buttonPin[buttonPinL][buttonPinR] = {
  6.   {0, 3}, // 1.65v comparaor threshold buttons
  7.   {1, 4}, // 3.3v comparator threshold buttons
  8.   {2, 5} // damper off buttons
  9. };
  10.  
  11. volatile unsigned long timerA [2] = {0, 3}; // 1.65v comparaor timing
  12. volatile unsigned long timerB [2] = {1, 4}; // 3.3v comparaor timing
  13. volatile unsigned long timerPassed [2] = {1, 2};// timeb-timea
  14. volatile boolean triggerA [2] = {0, 3};// timer validation used to trigger sound
  15. volatile boolean triggerB [2] = {1, 4};
  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 setup ()
  60. { for (int j = 0; j < buttonPinL ; j++) {
  61.     for (int i = 0; i < buttonPinR ; i++) {
  62.       pinMode(buttonPin[j][i] , INPUT_PULLUP);
  63.  
  64.       triggerA [i] = false ;// timing debounce
  65.       triggerB [i] = false ;
  66.  
  67.  
  68.     }
  69.   }
  70.   attachInterrupt (buttonPin[0][0] , int1, RISING) ;
  71.   attachInterrupt (buttonPin[0][1] , int3, RISING) ;
  72.   attachInterrupt (buttonPin[1][0] , int2, RISING) ;
  73.   attachInterrupt (buttonPin[1][1] , int4, RISING) ;
  74.  
  75. }
  76. void loop () {
  77.   byte offButton[2] = {1, 4};
  78.   byte midiNote[2] = {60, 63};
  79.  
  80.   for (int i = 0; i < buttonPinR ; i++) {
  81.  
  82.     offButton[i] = digitalRead(buttonPin[2][i]);
  83.  
  84.     if (triggerB [i]) // when both thresholds are proven process note
  85.     {
  86.       timerPassed [i] = (timerB [i] - timerA [i]);
  87.       //Serial.println (timePassed1) ;
  88.  
  89.       velocityFloat1 = 127 * maxVelocityTime / timerPassed [i];
  90.       if (velocityFloat1 > 127)
  91.       {
  92.         velocity1 = 127;
  93.       }
  94.       else
  95.       {
  96.  
  97.         velocityFloatCorrected1 = sqrt(127 * velocityFloat1);
  98.         velocity1 = velocityFloatCorrected1;
  99.       }
  100.       usbMIDI.sendNoteOn(midiNote[i], velocity1, 1);
  101.  
  102.       noInterrupts () ;
  103.       triggerB [i] = triggerA [i] = false ;
  104.       interrupts () ;
  105.     }
  106.     if ( offButton[i] == LOW) {
  107.       usbMIDI.sendNoteOff(midiNote[i], 0, 1);
  108.  
  109.     }
  110.   }
  111. }
Add Comment
Please, Sign In to add comment