LandoRo

piano code multibuttoned partial array

May 24th, 2022 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1.  
  2. const int channel = 1; // global midi channel
  3. const int buttonPinL = 3; // set array rows with easy naming
  4. const int buttonPinR = 2; // set array colums with easy naming
  5.  
  6. int buttonPin[buttonPinL][buttonPinR] = {
  7.   {0, 3}, // 1.65v comparaor threshold buttons
  8.   {1, 4}, // 3.3v comparator threshold buttons
  9.   {2, 5} // damper off buttons
  10. };
  11.  
  12. volatile unsigned long time1 ; // 1.65v comparaor timing
  13. volatile unsigned long time2 ; // 3.3v comparaor timing
  14. volatile unsigned long timePassed1;
  15. volatile unsigned long time3 ;
  16. volatile unsigned long time4 ;
  17. volatile unsigned long timePassed2;
  18.  
  19. volatile boolean valid1 = false ;// timing debounce
  20. volatile boolean valid2 = false ;
  21. volatile boolean valid3 = false ;
  22. volatile boolean valid4 = false ;
  23.  
  24. float maxVelocityTime = 30000; // my slowest amount of time passed between 1.65 and 3.3v threshold
  25.  
  26. int velocity1;
  27. float velocityFloat1;
  28. float velocityFloatCorrected1;
  29. int velocity2;
  30. float velocityFloat2;
  31. float velocityFloatCorrected2;
  32.  
  33. void int1 () // ISR for threshold time stamps 1.65v
  34. {
  35.   if (!valid1)
  36.   {
  37.     time1 = micros () ;
  38.     valid1 = true ;
  39.   }
  40. }
  41.  
  42. void int2 () // // ISR for threshold time stamps 3.3v
  43. {
  44.   if (valid1 && !valid2)
  45.   {
  46.     time2 = micros () ;
  47.     valid2 = true ;
  48.   }
  49. }
  50.  
  51. void int3 ()
  52. {
  53.   if (!valid3)
  54.   {
  55.     time3 = micros () ;
  56.     valid3 = true ;
  57.   }
  58. }
  59.  
  60. void int4 ()
  61. {
  62.   if (valid3 && !valid4)
  63.   {
  64.     time4 = micros () ;
  65.     valid4 = true ;
  66.   }
  67. }
  68.  
  69. void setup ()
  70. { for (int j = 0; j < buttonPinL ; j++) {
  71.     for (int i = 0; i < buttonPinR ; i++) {
  72.       pinMode(buttonPin[j][i] , INPUT_PULLUP);
  73.  
  74.     }
  75.   }
  76.   attachInterrupt (buttonPin[0][0] , int1, RISING) ;
  77.   attachInterrupt (buttonPin[0][1] , int3, RISING) ;
  78.   attachInterrupt (buttonPin[1][0] , int2, RISING) ;
  79.   attachInterrupt (buttonPin[1][1] , int4, RISING) ;
  80.  
  81. }
  82. void loop () {
  83.   byte button2;
  84.   byte button5;
  85.   button2 = digitalRead(buttonPin[2][0]);
  86.   button5 = digitalRead(buttonPin[2][1]);
  87.  
  88.   if (valid2) // when both thresholds are proven process note
  89.   {
  90.     timePassed1 = (time2 - time1);
  91.     Serial.println (timePassed1) ;
  92.  
  93.     velocityFloat1 = 127 * maxVelocityTime / timePassed1;
  94.     if (velocityFloat1 > 127)
  95.     {
  96.       velocity1 = 127;
  97.     }
  98.     else
  99.     {
  100.  
  101.       velocityFloatCorrected1 = sqrt(127 * velocityFloat1);
  102.       velocity1 = velocityFloatCorrected1;
  103.     }
  104.     usbMIDI.sendNoteOn(62, velocity1, 1);
  105.  
  106.     noInterrupts () ;
  107.     valid1 = valid2 = false ;
  108.     interrupts () ;
  109.   }
  110.   if (button2 == LOW) {
  111.     usbMIDI.sendNoteOff(62, 0, 1);
  112.  
  113.   }
  114.  
  115.   if (valid4)
  116.   {
  117.     timePassed2 = (time4 - time3);
  118.     //Serial.println (timePassed2) ;
  119.  
  120.     velocityFloat2 = 127 * maxVelocityTime / timePassed2;
  121.     if (velocityFloat2 > 127)
  122.     {
  123.       velocity2 = 127;
  124.     }
  125.     else
  126.     {
  127.  
  128.       velocityFloatCorrected2 = sqrt(127 * velocityFloat2);
  129.       velocity2 = velocityFloatCorrected2;
  130.     }
  131.     usbMIDI.sendNoteOn(65, velocity2, 1);
  132.  
  133.     noInterrupts () ;
  134.     valid3 = valid4 = false ;
  135.     interrupts () ;
  136.   }
  137.   if (button5 == LOW) {
  138.     usbMIDI.sendNoteOff(65, 0, 1);
  139.  
  140.   }
  141. }
Add Comment
Please, Sign In to add comment