Advertisement
Guest User

Untitled

a guest
Aug 1st, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #include <MIDI.h>
  2. #include <Tone.h>
  3.  
  4. // PIN OUTPUTS
  5. // Pin 1:
  6. // Pin 2:
  7. // Pin 3:
  8. // Pin 4:
  9. // Pin 5:
  10. // Pin 6:
  11. // Pin 7:
  12. // Pin 8: Tone 3 (fifth)
  13. // Pin 9:
  14. // Pin 10: Switch (3rd state)
  15. // Pin 11: Tone 2 (third)
  16. // Pin 12: Tone 1 (base * 8)
  17. // Pin 13: Trigger (inversed)
  18.  
  19. int ledPin = 13;
  20. int thirdPin = 10;
  21. int thirdSwitch = 0;
  22. int freq1 = 0;
  23. int freq2 = 0;
  24. int freq3 = 0;
  25. Tone tone1;
  26. Tone tone2;
  27. Tone tone3;
  28. int bendfactor = 0;
  29. int bendfreq = 0;
  30.  
  31. //Set frequencies for notes
  32.  
  33. const long f1[128] = { 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 116, 123, 131, 138, 146, 155, 164, 174, 185, 196, 208, 220, 233, 247, 261, 277, 294, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 830, 880, 932, 988, 1046, 1109, 1175, 1244, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7458, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544, 13290, 14080, 14917, 15804, 16744, 17740, 18794, 19912, 21096, 22350, 23679, 25087, 26579, 28160, 29834, 31608, 33488, 35479, 37589, 39824, 42192, 44701, 47359, 50475, 53159, 56320, 59669, 63217, 66976, 70959, 75178, 79648, 84385, 89402, 94718, 100351};
  34. const int f2_a[128] = {10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 116, 123, 131, 138, 147, 155, 165, 174, 185, 196, 208, 220, 233, 247, 261, 277, 294, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 830, 880, 932, 988, 1046, 1109, 1175, 1244, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7458, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544, 13290, 14080, 14917, 15804};
  35. const int f2_b[128] = {10, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 116, 123, 131, 138, 147, 155, 165, 174, 185, 196, 208, 220, 233, 247, 261, 277, 294, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 830, 880, 932, 988, 1046, 1109, 1175, 1244, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7458, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544, 13290, 14080, 14917};
  36. const int f3[128] = {12, 13, 14, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 116, 123, 131, 138, 147, 155, 165, 174, 185, 196, 208, 220, 233, 247, 261, 277, 294, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 830, 880, 932, 988, 1046, 1109, 1175, 1244, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7458, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544, 13290, 14080, 14917, 15804, 16744, 17740, 18794};
  37.  
  38. // This function will be automatically called when a NoteOn is received.
  39. // It must be a void-returning function with the correct parameters,
  40. // see documentation here:
  41. // http://arduinomidilib.sourceforge.net/class_m_i_d_i___class.html
  42.  
  43. void HandleNoteOn(byte channel, byte note, byte velocity) {
  44.  
  45. // Do whatever you want when you receive a Note On.
  46.  
  47. if (velocity == 0) {
  48. digitalWrite(ledPin,HIGH);
  49. }
  50. else {
  51. digitalWrite(ledPin,LOW);
  52. }
  53.  
  54. freq1 = f1[note];
  55. if (thirdSwitch == HIGH) {
  56. freq2 = f2_a[note];
  57. } else {
  58. freq2 = f2_b[note];
  59. }
  60. freq3 = f3[note];
  61. if(note == 0)digitalWrite(ledPin,HIGH);
  62.  
  63. freq1 = freq1 + bendfreq;
  64. freq2 = freq2 + bendfreq;
  65. freq3 = freq3 + bendfreq;
  66.  
  67. tone1.play(freq1);
  68. tone2.play(freq2);
  69. tone3.play(freq3);
  70.  
  71. digitalWrite(ledPin,HIGH);
  72.  
  73. // Try to keep your callbacks short (no delays ect) as the contrary would slow down the loop()
  74. // and have a bad impact on real-time performance.
  75. }
  76.  
  77.  
  78. void HandlePitchBend(byte channel, int bend) {
  79. bendfreq = bend/1000;
  80.  
  81. freq1 = freq1 + bendfreq;
  82. freq2 = freq2 + bendfreq;
  83. freq3 = freq3 + bendfreq;
  84.  
  85. tone1.play(freq1);
  86. tone2.play(freq2);
  87. tone3.play(freq3);
  88.  
  89. }
  90.  
  91. void setup() {
  92. // Initiate MIDI communications, listen to all channels
  93. MIDI.begin(1);
  94.  
  95. // Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
  96. MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
  97.  
  98. // Connect HandlePitchBend to the library
  99. MIDI.setHandlePitchBend(HandlePitchBend);
  100.  
  101. pinMode(ledPin, OUTPUT);
  102. pinMode(thirdPin, INPUT);
  103. tone1.begin(12);
  104. tone2.begin(11);
  105. tone3.begin(8);
  106. tone1.play(freq1);
  107. tone2.play(freq2);
  108. tone3.play(freq3);
  109. }
  110.  
  111.  
  112. void loop() {
  113. //Read the 3rd switch pin state.
  114. thirdSwitch = digitalRead(thirdPin);
  115.  
  116. // Call MIDI.read the fastest you can for real-time performance.
  117. MIDI.read();
  118.  
  119. // There is no need to check if there are messages incoming if they are bound to a Callback function.
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement