Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- #include <Tone.h>
- // PIN OUTPUTS
- // Pin 1:
- // Pin 2:
- // Pin 3:
- // Pin 4:
- // Pin 5:
- // Pin 6:
- // Pin 7:
- // Pin 8: Tone 3 (fifth)
- // Pin 9:
- // Pin 10: Switch (3rd state)
- // Pin 11: Tone 2 (third)
- // Pin 12: Tone 1 (base * 8)
- // Pin 13: Trigger (inversed)
- int ledPin = 13;
- int thirdPin = 10;
- int thirdSwitch = 0;
- int freq1 = 0;
- int freq2 = 0;
- int freq3 = 0;
- Tone tone1;
- Tone tone2;
- Tone tone3;
- int bendfactor = 0;
- int bendfreq = 0;
- //Set frequencies for notes
- 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};
- 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};
- 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};
- 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};
- // This function will be automatically called when a NoteOn is received.
- // It must be a void-returning function with the correct parameters,
- // see documentation here:
- // http://arduinomidilib.sourceforge.net/class_m_i_d_i___class.html
- void HandleNoteOn(byte channel, byte note, byte velocity) {
- // Do whatever you want when you receive a Note On.
- if (velocity == 0) {
- digitalWrite(ledPin,HIGH);
- }
- else {
- digitalWrite(ledPin,LOW);
- }
- freq1 = f1[note];
- if (thirdSwitch == HIGH) {
- freq2 = f2_a[note];
- } else {
- freq2 = f2_b[note];
- }
- freq3 = f3[note];
- if(note == 0)digitalWrite(ledPin,HIGH);
- freq1 = freq1 + bendfreq;
- freq2 = freq2 + bendfreq;
- freq3 = freq3 + bendfreq;
- tone1.play(freq1);
- tone2.play(freq2);
- tone3.play(freq3);
- digitalWrite(ledPin,HIGH);
- // Try to keep your callbacks short (no delays ect) as the contrary would slow down the loop()
- // and have a bad impact on real-time performance.
- }
- void HandlePitchBend(byte channel, int bend) {
- bendfreq = bend/1000;
- freq1 = freq1 + bendfreq;
- freq2 = freq2 + bendfreq;
- freq3 = freq3 + bendfreq;
- tone1.play(freq1);
- tone2.play(freq2);
- tone3.play(freq3);
- }
- void setup() {
- // Initiate MIDI communications, listen to all channels
- MIDI.begin(1);
- // Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
- MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
- // Connect HandlePitchBend to the library
- MIDI.setHandlePitchBend(HandlePitchBend);
- pinMode(ledPin, OUTPUT);
- pinMode(thirdPin, INPUT);
- tone1.begin(12);
- tone2.begin(11);
- tone3.begin(8);
- tone1.play(freq1);
- tone2.play(freq2);
- tone3.play(freq3);
- }
- void loop() {
- //Read the 3rd switch pin state.
- thirdSwitch = digitalRead(thirdPin);
- // Call MIDI.read the fastest you can for real-time performance.
- MIDI.read();
- // There is no need to check if there are messages incoming if they are bound to a Callback function.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement