Advertisement
Guest User

Multi piezo

a guest
Mar 10th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.55 KB | None | 0 0
  1. #include <Timer.h>
  2. #include <MIDI.h>
  3. MIDI_CREATE_DEFAULT_INSTANCE();
  4. Timer t[10];
  5. float notes[] = {
  6.   8.1757989156,  8.6619572180,  9.1770239974,  10.3008611535,  10.3008611535,  10.9133822323,  11.5623257097,  12.2498573744,  12.9782717994,  13.7500000000,  14.5676175474,  15.4338531643,  16.3515978313,  17.3239144361,  18.3540479948,  19.4454364826,  20.6017223071,  21.8267644646,  23.1246514195,  24.4997147489,  25.9565435987,  27.5000000000,  29.1352350949,  30.8677063285,  32.7031956626,  34.6478288721,  36.7080959897,  38.8908729653,  41.2034446141,  43.6535289291,  46.2493028390,  48.9994294977,  51.9130871975,  55.0000000000,  58.2704701898,  61.7354126570,  65.4063913251,  69.2956577442,  73.4161919794,  77.7817459305,  82.4068892282,  87.3070578583,  92.4986056779,  97.9988589954,  103.8261743950,  110.0000000000,  116.5409403795,  123.4708253140,  130.8127826503,  138.5913154884,  146.8323839587,  155.5634918610,  164.8137784564,  174.6141157165,  184.9972113558,  195.9977179909,  207.6523487900,  220.0000000000,  233.0818807590,  246.9416506281,  261.6255653006,  277.1826309769,  293.6647679174,  311.1269837221,  329.6275569129,  349.2282314330,  369.9944227116,  391.9954359817,  415.3046975799,  440.0000000000,  466.1637615181,  493.8833012561,  523.2511306012,  554.3652619537,  587.3295358348,  622.2539674442,  659.2551138257,  698.4564628660,  739.9888454233,  783.9908719635,  830.6093951599,  880.0000000000,  932.3275230362,  987.7666025122,  1046.5022612024,  1108.7305239075,  1174.6590716696,  1244.5079348883,  1318.5102276515,  1396.9129257320,  1479.9776908465,  1567.9817439270,  1661.2187903198,  1760.0000000000,  1864.6550460724,  1975.5332050245,  2093.0045224048,  2217.4610478150,  2349.3181433393,  2489.0158697766,  2637.0204553030,  2793.8258514640,  2959.9553816931,  3135.9634878540,  3322.4375806396,  3520.0000000000,  3729.3100921447,  3951.0664100490,  4186.0090448096,  4434.9220956300,  4698.6362866785,  4978.0317395533,  5274.0409106059,  5587.6517029281,  5919.9107633862,  5919.9107633862,  6644.8751612791,  7040.0000000000,  7458.6201842894,  7902.1328200980,  8372.0180896192,  8869.8441912599,  9397.2725733570,  9956.0634791066,  10548.0818212118,  11175.3034058561,  11839.8215267723,  12543.8539514160
  7. };
  8. long pins[] = {
  9.   2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  10. };
  11.  
  12.  
  13. void setup() {
  14.     MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function
  15.  
  16.     // Do the same for NoteOffs
  17.     MIDI.setHandleNoteOff(handleNoteOff);
  18.  
  19.     // Initiate MIDI communications, listen to all channels
  20.     MIDI.begin(MIDI_CHANNEL_OMNI);
  21.     Serial.begin(9600);
  22.     pinMode(13, OUTPUT); //LED
  23.     for(int i = 0; i<11; i++) {
  24.       pinMode(pins[i], OUTPUT);
  25.     }
  26. }
  27.  
  28. void handleNoteOn(byte channel, byte pitch, byte velocity) {
  29.   int chan = map(channel, 1, 16, 1, 10);
  30.   if(velocity > 0) {
  31.   switch (chan) {
  32.     case 1:
  33.       playNote(pins[0], notes[pitch]);
  34.       break;
  35.     case 2:
  36.       playNote(pins[1], notes[pitch]);
  37.       break;
  38.     case 3:
  39.       playNote(pins[2], notes[pitch]);
  40.       break;
  41.     case 4:
  42.       playNote(pins[3], notes[pitch]);
  43.       break;
  44.     case 5:
  45.       playNote(pins[4], notes[pitch]);
  46.       break;
  47.     case 6:
  48.       playNote(pins[5], notes[pitch]);
  49.       break;
  50.     case 7:
  51.       playNote(pins[6], notes[pitch]);
  52.       break;
  53.     case 8:
  54.       playNote(pins[7], notes[pitch]);
  55.       break;
  56.     case 9:
  57.       playNote(pins[8], notes[pitch]);
  58.       break;
  59.     case 10:
  60.       playNote(pins[9], notes[pitch]);
  61.       break;
  62.   }
  63.   }
  64. }
  65. void handleNoteOff(byte channel, byte pitch, byte velocity) {
  66.   int chan = map(channel, 1, 16, 1, 10);
  67.   switch (chan) {
  68.     case 1:
  69.       stopNote(pins[0]);
  70.       break;
  71.     case 2:
  72.       stopNote(pins[1]);
  73.       break;
  74.     case 3:
  75.       stopNote(pins[2]);
  76.       break;
  77.     case 4:
  78.       stopNote(pins[3]);
  79.       break;
  80.     case 5:
  81.       stopNote(pins[4]);
  82.       break;
  83.     case 6:
  84.       stopNote(pins[5]);
  85.       break;
  86.     case 7:
  87.       stopNote(pins[6]);
  88.       break;
  89.     case 8:
  90.       stopNote(pins[7]);
  91.       break;
  92.     case 9:
  93.       stopNote(pins[8]);
  94.       break;
  95.     case 10:
  96.       stopNote(pins[9]);
  97.       break;
  98.   }
  99. }
  100.  
  101. void loop() {
  102.   MIDI.read();
  103.   t[1].update();
  104.   t[2].update();
  105.   t[3].update();
  106.   t[4].update();
  107.   t[5].update();
  108.   t[6].update();
  109.   t[7].update();
  110.   t[8].update();
  111.   t[9].update();
  112.   t[10].update();
  113. }
  114. long ids[10];
  115. void playNote(int pin, unsigned long freq) {
  116.   float ms = 1000/freq/2;
  117.   ids[pin] = t[pin].oscillate(pin, ms, HIGH);
  118. }
  119.  
  120. void stopNote(int pin) {
  121.   t[pin].stop(ids[pin]);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement