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 timerA [2] = {0, 3}; // 1.65v comparaor timing
- volatile unsigned long timerB [2] = {1, 4}; // 3.3v comparaor timing
- volatile unsigned long timerPassed [2] = {1, 2};// timeb-timea
- volatile boolean triggerA [2] = {0, 3};// timer validation used to trigger sound
- volatile boolean triggerB [2] = {1, 4};
- float maxVelocityTime = 30000; // my slowest amount of time passed between 1.65 and 3.3v threshold
- int velocity1;
- float velocityFloat1;
- float velocityFloatCorrected1;
- void int1 () // ISR for threshold time stamps 1.65v
- {
- if (!triggerA [0])
- {
- timerA [0] = micros () ;
- triggerA [0] = true ;
- }
- }
- void int2 () // // ISR for threshold time stamps 3.3v
- {
- if (triggerA [0] && !triggerB [0])
- {
- timerB [0] = micros () ;
- triggerB [0] = true ;
- }
- }
- void int3 ()
- {
- if (!triggerA [1])
- {
- timerA [1] = micros () ;
- triggerA [1] = true ;
- }
- }
- void int4 ()
- {
- if (triggerA [1] && !triggerB [1])
- {
- timerB [1] = micros () ;
- triggerB [1] = true ;
- }
- }
- void setup ()
- { for (int j = 0; j < buttonPinL ; j++) {
- for (int i = 0; i < buttonPinR ; i++) {
- pinMode(buttonPin[j][i] , INPUT_PULLUP);
- triggerA [i] = false ;// timing debounce
- triggerB [i] = false ;
- }
- }
- 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 offButton[2] = {1, 4};
- byte midiNote[2] = {60, 63};
- for (int i = 0; i < buttonPinR ; i++) {
- offButton[i] = digitalRead(buttonPin[2][i]);
- if (triggerB [i]) // when both thresholds are proven process note
- {
- timerPassed [i] = (timerB [i] - timerA [i]);
- //Serial.println (timePassed1) ;
- velocityFloat1 = 127 * maxVelocityTime / timerPassed [i];
- if (velocityFloat1 > 127)
- {
- velocity1 = 127;
- }
- else
- {
- velocityFloatCorrected1 = sqrt(127 * velocityFloat1);
- velocity1 = velocityFloatCorrected1;
- }
- usbMIDI.sendNoteOn(midiNote[i], velocity1, 1);
- noInterrupts () ;
- triggerB [i] = triggerA [i] = false ;
- interrupts () ;
- }
- if ( offButton[i] == LOW) {
- usbMIDI.sendNoteOff(midiNote[i], 0, 1);
- }
- }
- }
Add Comment
Please, Sign In to add comment