Advertisement
Guest User

MIDI Joystick Project (Teensy 3.2)

a guest
Jan 31st, 2018
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | None | 0 0
  1. #include <MIDI.h>
  2. #define LED_PIN 13
  3.  
  4. // Note that MIDI THRU is on by  default! I couldn't figure out how to disable it here,
  5. // so I manually commented out COMPILE_MIDI_THRU in MIDI.h, the path for Teensy on OSX is:
  6. // /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/MIDI/MIDI.h
  7.  
  8. //#undef COMPILE_MIDI_THRU    // this doesn't work :(
  9.  
  10. // Set MIDI channel for Joystick only - incoming midi notes on are passed thru on all channels
  11. const uint8_t midiCh = 1;
  12.  
  13. elapsedMillis msec = 0;
  14. int previous_a1 = -1;
  15. int previous_a2 = -1;
  16.  
  17. // The analog readings can be very noisy, so they are smoothed out with multiple readings
  18. const int nsmooth = 4;    // # of analog values to use for smoothing
  19. int read_index = 0;
  20. int a1[nsmooth];
  21. int a2[nsmooth];
  22. int total_a1 = 0;
  23. int total_a2 = 0;
  24.  
  25. //============================================================
  26. // Setup some stuff at the start
  27. //============================================================
  28. void setup() {
  29.   //MIDI.turnThruOff();    // This doesn't work :(
  30.  
  31.   //------------------------------
  32.   // MIDI setup
  33.   //------------------------------
  34.   MIDI.begin(midiCh);
  35.  
  36.   //------------------------------
  37.   // set up the LED pin and blink
  38.   //------------------------------
  39.   pinMode(LED_PIN, OUTPUT);
  40.   digitalWrite(LED_PIN, HIGH);
  41.   delay(100);
  42.   digitalWrite(LED_PIN, LOW);
  43.  
  44.   //------------------------------
  45.   // initialize array for smoothing
  46.   //------------------------------
  47.   for (int n = 0; n < nsmooth; n++) {
  48.     a1[n] = 0;
  49.     a2[n] = 0;
  50.   }
  51.  
  52. }
  53. //============================================================
  54. // This is the main program
  55. //============================================================
  56. void loop() {
  57.   int d1,d2,ch,val;
  58.   //##########################################
  59.   // Handle incoming MIDI messages
  60.   //##########################################
  61.   if (MIDI.read()) {
  62.     d1 = MIDI.getData1();
  63.     d2 = MIDI.getData2();
  64.     ch = MIDI.getChannel();
  65.     byte type = MIDI.getType();
  66.     switch (type) {
  67.       //------------------------------
  68.       //------------------------------
  69.       case NoteOn:
  70.         if (d2 > 0) { MIDI.sendNoteOn (d1,d2,ch);  }
  71.         else        { MIDI.sendNoteOff(d1,d2,ch); }
  72.         break;
  73.       //------------------------------
  74.       case NoteOff:
  75.         MIDI.sendNoteOff(d1,d2,ch);
  76.         break;
  77.       //------------------------------
  78.       case PitchBend:
  79.         d2 = (d2-64)*128;
  80.         //MIDI.sendPitchBend(d2,ch);
  81.         //Serial.println(String("Message ")+ "   d1="+d1 +"   d2="+d2 );
  82.         break;
  83.       //------------------------------
  84.       case ProgramChange:
  85.         //MIDI.sendProgramChange(d2,ch);
  86.         break;
  87.       //------------------------------
  88.       case AfterTouchPoly:
  89.         //MIDI.sendAfterTouchPoly(d1,d2,ch);
  90.         MIDI.sendAfterTouch(d2,ch);
  91.         break;
  92.       //------------------------------
  93.       case ControlChange:
  94.         // Restrict the CC to certain types
  95.         //Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
  96.         // mod wheel
  97.         //if (d1==1) { MIDI.sendControlChange(d1,d2,ch); }
  98.         break;
  99.       //------------------------------
  100.       // Ignore all other messages
  101.       //------------------------------
  102.     }
  103.   }
  104.   //##########################################
  105.   // Read the analog joystick
  106.   //##########################################
  107.   if (msec >= 10) {
  108.     msec = 0;
  109.  
  110.     total_a1 = total_a1 - a1[read_index];
  111.     total_a2 = total_a2 - a2[read_index];
  112.  
  113.     //------------------------------
  114.     // Read joystick position
  115.     //------------------------------
  116.     a1[read_index] = analogRead(A1)*127/1023;
  117.     a2[read_index] = analogRead(A0)*127/1023;
  118.  
  119.     //------------------------------
  120.     // Smooth the analog readings
  121.     //------------------------------
  122.     total_a1 = total_a1 + a1[read_index];
  123.     total_a2 = total_a2 + a2[read_index];
  124.    
  125.     read_index = read_index + 1;
  126.     if (read_index >= nsmooth) { read_index = 0; }
  127.  
  128.     int avg_a1 = total_a1 / nsmooth;
  129.     int avg_a2 = total_a2 / nsmooth;
  130.    
  131.     //------------------------------
  132.     // send midi data according to analog readings
  133.     //------------------------------
  134.     if (avg_a1 != previous_a1) {
  135.       val = (avg_a1-64)*128*-1 -1;
  136.       MIDI.sendPitchBend( val ,midiCh);
  137.       previous_a1 = avg_a1;
  138.       Serial.println(String("A1 value : ")+avg_a1+"   "+val);
  139.     }
  140.     if (avg_a2 != previous_a2) {
  141.       val = avg_a2*2-128;
  142.       if (val<=-1) { MIDI.sendControlChange(1,val*-1-1,midiCh); }
  143.       //if (val>=0) { MIDI.sendControlChange(1,val,midiCh); }
  144.       previous_a2 = avg_a2;
  145.       //Serial.println(String("A2 value : ")+avg_a2+"   "+val);
  146.     }
  147.     //##########################################
  148.     //##########################################
  149.   }
  150. }
  151. //============================================================
  152. //============================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement