/* */ // digital pin 2 has a pushbutton attached to it. Give it a name: int Valve1Pin = 2; int Valve2Pin = 3; int Valve3Pin = 4; int OldBC = -1; bool bNoteOn = false; int iCurrentNote = 0; int iLookup[70] = {0,210,360,480,570,720,840,930,1080,1200, -30,180,330,450,540,690,810,900,1050,1170, -60,150,300,420,510,660,780,870,1020,1140, -90,120,270,390,480,630,750,840,990,1110, -120,90,240,360,450,600,720,810,960,1080, -150,60,210,330,420,570,690,780,930,1050, -180,30,180,300,390,540,660,750,900,1020}; int iStartNote = 58; // Bb3, Middle C on Bb Trumpet int iNotesPerBit = 30; // Scale slider int iBitOffset = 90; // Offset slider int iNoteOnThreshold = 85; int iNoteOffThreshold = 80; int iBCDelta = 16 ; // the setup routine runs once when you press reset: void setup() { // initialize MIDI communication at 31.25 kbaud: Serial.begin(31250); // make the pushbutton's pin an input: pinMode(Valve1Pin, INPUT_PULLUP); pinMode(Valve2Pin, INPUT_PULLUP); pinMode(Valve3Pin, INPUT_PULLUP); } void MidiNoteOn( int iNote, int iVelocity) { Serial.write( 0x90 ); Serial.write( iNote ); Serial.write( iVelocity ); } void MidiNoteOff(int iNote) { Serial.write( 0x80 ); Serial.write( iNote ); Serial.write( 64 ); } void MidiCC(int iController, int iValue) { Serial.write( 0xb0 ); Serial.write( iController ); Serial.write( iValue ); } int CalculateNote(int SliderValue) { int Valve1 = digitalRead(Valve1Pin); int Valve2 = digitalRead(Valve2Pin); int Valve3 = digitalRead(Valve3Pin); int iIndex = Valve1 * 20 + Valve2 * 10 + Valve3 * 30; int iEndOfIndex = iIndex + 9; int iLastDelta = 9999; int iLastIndex = iIndex; bool bDone = false; while (!bDone) { int iDelta = abs( SliderValue - iBitOffset - iLookup[iIndex] ); if ( iLastDelta <= iDelta ) { // Last index was closer, use it bDone = true; iIndex = iLastIndex; } else { iLastDelta = iDelta; iLastIndex = iIndex; if ( iIndex == iEndOfIndex ) { // this is the last index, just use it bDone = true; } else { iIndex++; } } } return( iLookup[iIndex] / iNotesPerBit + iStartNote); } // the loop routine runs over and over again forever: void loop() { // read the input pin: int BCValue = analogRead(A0); int SliderValue = analogRead(A1); if (bNoteOn) { if (BCValue < iNoteOffThreshold ) { MidiNoteOff(iCurrentNote); bNoteOn = false; } else { int iNewNote = CalculateNote(SliderValue); if (iNewNote != iCurrentNote) { MidiNoteOn(iNewNote, 64 /* BCCourse */); MidiNoteOff(iCurrentNote); iCurrentNote = iNewNote; } if ( abs(BCValue - OldBC) > iBCDelta) { int BCData = (BCValue - 70) >> 2; if (BCData > 127) { BCData = 127; } MidiCC( 2, BCData); OldBC = BCValue; } } } else { if (BCValue > iNoteOnThreshold) { iCurrentNote = CalculateNote(SliderValue); MidiNoteOn(iCurrentNote, 64 /* BCCourse */); bNoteOn = true; } } }