Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- #define LED_PIN 13
- // Note that MIDI THRU is on by default! I couldn't figure out how to disable it here,
- // so I manually commented out COMPILE_MIDI_THRU in MIDI.h, the path for Teensy on OSX is:
- // /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/MIDI/MIDI.h
- //#undef COMPILE_MIDI_THRU // this doesn't work :(
- // Set MIDI channel for Joystick only - incoming midi notes on are passed thru on all channels
- const uint8_t midiCh = 1;
- elapsedMillis msec = 0;
- int previous_a1 = -1;
- int previous_a2 = -1;
- // The analog readings can be very noisy, so they are smoothed out with multiple readings
- const int nsmooth = 4; // # of analog values to use for smoothing
- int read_index = 0;
- int a1[nsmooth];
- int a2[nsmooth];
- int total_a1 = 0;
- int total_a2 = 0;
- //============================================================
- // Setup some stuff at the start
- //============================================================
- void setup() {
- //MIDI.turnThruOff(); // This doesn't work :(
- //------------------------------
- // MIDI setup
- //------------------------------
- MIDI.begin(midiCh);
- //------------------------------
- // set up the LED pin and blink
- //------------------------------
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, HIGH);
- delay(100);
- digitalWrite(LED_PIN, LOW);
- //------------------------------
- // initialize array for smoothing
- //------------------------------
- for (int n = 0; n < nsmooth; n++) {
- a1[n] = 0;
- a2[n] = 0;
- }
- }
- //============================================================
- // This is the main program
- //============================================================
- void loop() {
- int d1,d2,ch,val;
- //##########################################
- // Handle incoming MIDI messages
- //##########################################
- if (MIDI.read()) {
- d1 = MIDI.getData1();
- d2 = MIDI.getData2();
- ch = MIDI.getChannel();
- byte type = MIDI.getType();
- switch (type) {
- //------------------------------
- //------------------------------
- case NoteOn:
- if (d2 > 0) { MIDI.sendNoteOn (d1,d2,ch); }
- else { MIDI.sendNoteOff(d1,d2,ch); }
- break;
- //------------------------------
- case NoteOff:
- MIDI.sendNoteOff(d1,d2,ch);
- break;
- //------------------------------
- case PitchBend:
- d2 = (d2-64)*128;
- //MIDI.sendPitchBend(d2,ch);
- //Serial.println(String("Message ")+ " d1="+d1 +" d2="+d2 );
- break;
- //------------------------------
- case ProgramChange:
- //MIDI.sendProgramChange(d2,ch);
- break;
- //------------------------------
- case AfterTouchPoly:
- //MIDI.sendAfterTouchPoly(d1,d2,ch);
- MIDI.sendAfterTouch(d2,ch);
- break;
- //------------------------------
- case ControlChange:
- // Restrict the CC to certain types
- //Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
- // mod wheel
- //if (d1==1) { MIDI.sendControlChange(d1,d2,ch); }
- break;
- //------------------------------
- // Ignore all other messages
- //------------------------------
- }
- }
- //##########################################
- // Read the analog joystick
- //##########################################
- if (msec >= 10) {
- msec = 0;
- total_a1 = total_a1 - a1[read_index];
- total_a2 = total_a2 - a2[read_index];
- //------------------------------
- // Read joystick position
- //------------------------------
- a1[read_index] = analogRead(A1)*127/1023;
- a2[read_index] = analogRead(A0)*127/1023;
- //------------------------------
- // Smooth the analog readings
- //------------------------------
- total_a1 = total_a1 + a1[read_index];
- total_a2 = total_a2 + a2[read_index];
- read_index = read_index + 1;
- if (read_index >= nsmooth) { read_index = 0; }
- int avg_a1 = total_a1 / nsmooth;
- int avg_a2 = total_a2 / nsmooth;
- //------------------------------
- // send midi data according to analog readings
- //------------------------------
- if (avg_a1 != previous_a1) {
- val = (avg_a1-64)*128*-1 -1;
- MIDI.sendPitchBend( val ,midiCh);
- previous_a1 = avg_a1;
- Serial.println(String("A1 value : ")+avg_a1+" "+val);
- }
- if (avg_a2 != previous_a2) {
- val = avg_a2*2-128;
- if (val<=-1) { MIDI.sendControlChange(1,val*-1-1,midiCh); }
- //if (val>=0) { MIDI.sendControlChange(1,val,midiCh); }
- previous_a2 = avg_a2;
- //Serial.println(String("A2 value : ")+avg_a2+" "+val);
- }
- //##########################################
- //##########################################
- }
- }
- //============================================================
- //============================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement