Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int channel = 1; // global midi channel
- const int buttonPinL = 3; // set array rows with easy naming
- const int buttonPinR = 2; // set array colums with easy naming
- int buttonPin[buttonPinL][buttonPinR] = {
- {0, 3}, // 1.65v comparaor threshold buttons
- {1, 4}, // 3.3v comparator threshold buttons
- {2, 5} // damper off buttons
- };
- volatile unsigned long time1 ; // 1.65v comparaor timing
- volatile unsigned long time2 ; // 3.3v comparaor timing
- volatile unsigned long timePassed1;
- volatile unsigned long time3 ;
- volatile unsigned long time4 ;
- volatile unsigned long timePassed2;
- volatile boolean valid1 = false ;// timing debounce
- volatile boolean valid2 = false ;
- volatile boolean valid3 = false ;
- volatile boolean valid4 = false ;
- float maxVelocityTime = 30000; // my slowest amount of time passed between 1.65 and 3.3v threshold
- int velocity1;
- float velocityFloat1;
- float velocityFloatCorrected1;
- int velocity2;
- float velocityFloat2;
- float velocityFloatCorrected2;
- void int1 () // ISR for threshold time stamps 1.65v
- {
- if (!valid1)
- {
- time1 = micros () ;
- valid1 = true ;
- }
- }
- void int2 () // // ISR for threshold time stamps 3.3v
- {
- if (valid1 && !valid2)
- {
- time2 = micros () ;
- valid2 = true ;
- }
- }
- void int3 ()
- {
- if (!valid3)
- {
- time3 = micros () ;
- valid3 = true ;
- }
- }
- void int4 ()
- {
- if (valid3 && !valid4)
- {
- time4 = micros () ;
- valid4 = true ;
- }
- }
- void setup ()
- { for (int j = 0; j < buttonPinL ; j++) {
- for (int i = 0; i < buttonPinR ; i++) {
- pinMode(buttonPin[j][i] , INPUT_PULLUP);
- }
- }
- attachInterrupt (buttonPin[0][0] , int1, RISING) ;
- attachInterrupt (buttonPin[0][1] , int3, RISING) ;
- attachInterrupt (buttonPin[1][0] , int2, RISING) ;
- attachInterrupt (buttonPin[1][1] , int4, RISING) ;
- }
- void loop () {
- byte button2;
- byte button5;
- button2 = digitalRead(buttonPin[2][0]);
- button5 = digitalRead(buttonPin[2][1]);
- if (valid2) // when both thresholds are proven process note
- {
- timePassed1 = (time2 - time1);
- Serial.println (timePassed1) ;
- velocityFloat1 = 127 * maxVelocityTime / timePassed1;
- if (velocityFloat1 > 127)
- {
- velocity1 = 127;
- }
- else
- {
- velocityFloatCorrected1 = sqrt(127 * velocityFloat1);
- velocity1 = velocityFloatCorrected1;
- }
- usbMIDI.sendNoteOn(62, velocity1, 1);
- noInterrupts () ;
- valid1 = valid2 = false ;
- interrupts () ;
- }
- if (button2 == LOW) {
- usbMIDI.sendNoteOff(62, 0, 1);
- }
- if (valid4)
- {
- timePassed2 = (time4 - time3);
- //Serial.println (timePassed2) ;
- velocityFloat2 = 127 * maxVelocityTime / timePassed2;
- if (velocityFloat2 > 127)
- {
- velocity2 = 127;
- }
- else
- {
- velocityFloatCorrected2 = sqrt(127 * velocityFloat2);
- velocity2 = velocityFloatCorrected2;
- }
- usbMIDI.sendNoteOn(65, velocity2, 1);
- noInterrupts () ;
- valid3 = valid4 = false ;
- interrupts () ;
- }
- if (button5 == LOW) {
- usbMIDI.sendNoteOff(65, 0, 1);
- }
- }
Add Comment
Please, Sign In to add comment