SHARE
TWEET

Laser Harp

Geek1599 Jul 24th, 2015 (edited) 231 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int velocity = 0x64;
  2. int baudrate = 31250; //use 9600 for debugging; 31250 is the native baudrate for all MIDI instruments.
  3. int threshold = 937; //the threshold resistance to trigger sound-earlier
  4. int noteON = 0x90;    //experimentation has indicated that the threshold
  5. int noteOFF = 129;   //is different for USB and DC connections
  6. int octave = 3;
  7. int volume = A15;
  8.  
  9. int c = 0xC; //define the MIDI value for the note
  10. int cin = A0;  //defines input port
  11. int cled = 53; //defines LED output port
  12. int cvalue = 0;  //defines the variable for the current of the c photodiode
  13.  
  14. int cs = 0xD;
  15. int csin = A1;
  16. int csled = 52;
  17. int csvalue = 0;
  18.  
  19. int d = 0xE;
  20. int din = A2;
  21. int dled = 51;
  22. int dvalue = 0;
  23.  
  24. int ds = 0xF;
  25. int dsin = A3;
  26. int dsled = 50;
  27. int dsvalue = 0;
  28.  
  29. int e = 0x10;
  30. int ein = A4;
  31. int eled = 49;
  32. int evalue = 0;
  33.  
  34. int f = 0x11;
  35. int fin = A5;
  36. int fled = 48;
  37. int fvalue = 0;
  38.  
  39. int fs = 0x12;
  40. int fsin = A6;
  41. int fsled = 47;
  42. int fsvalue = 0;
  43.  
  44. int g = 0x13;
  45. int gin = A7;
  46. int gled = 46;
  47. int gvalue = 0;
  48.  
  49. int gs = 0x14;
  50. int gsin = A8;
  51. int gsled = 45;
  52. int gsvalue = 0;
  53.  
  54. int a = 0x15;
  55. int ain = A9;
  56. int aled = 44;
  57. int avalue = 0;
  58.  
  59. int as = 0x16;
  60. int ashin = A10;
  61. int asled = 43;
  62. int asvalue = 0;
  63.  
  64. int b = 0x17;
  65. int bin = A11;
  66. int bled = 42;
  67. int bvalue = 0;
  68.  
  69. int co = 0x18;
  70. int coin = A12;
  71. int coled = 41;
  72. int covalue = 0;
  73.  
  74. void setup() {
  75.   Serial.begin(baudrate);
  76.  
  77.   pinMode(cin, INPUT);
  78.   pinMode(csin, INPUT);
  79.   pinMode(din, INPUT);
  80.   pinMode(dsin, INPUT);
  81.   pinMode(ein, INPUT);
  82.   pinMode(fin, INPUT);
  83.   pinMode(fsin, INPUT);
  84.   pinMode(gin, INPUT);
  85.   pinMode(gsin, INPUT);
  86.   pinMode(ain, INPUT);
  87.   pinMode(ashin, INPUT);
  88.   pinMode(bin, INPUT);
  89.   pinMode(coin, INPUT);
  90.   //pinMode(potenpin, INPUT);  cancelled feature: octaves. Notes wouldn't turn off :/
  91.  
  92.   pinMode(cled, OUTPUT);
  93.   pinMode(csled, OUTPUT);
  94.   pinMode(dled, OUTPUT);
  95.   pinMode(dsled, OUTPUT);
  96.   pinMode(eled, OUTPUT);
  97.   pinMode(fled, OUTPUT);
  98.   pinMode(fsled, OUTPUT);
  99.   pinMode(gled, OUTPUT);
  100.   pinMode(gsled, OUTPUT);
  101.   pinMode(aled, OUTPUT);
  102.   pinMode(asled, OUTPUT);
  103.   pinMode(bled, OUTPUT);
  104.   pinMode(coled, OUTPUT);
  105.  
  106. }
  107. void loop() {
  108.  
  109.   bool cstatus;  //defines boolean states. These determine whether or not a note is turned on.
  110.   bool csstatus;
  111.   bool dstatus;
  112.   bool dsstatus;
  113.   bool estatus;
  114.   bool fstatus;
  115.   bool fsstatus;
  116.   bool gstatus;
  117.   bool gsstatus;
  118.   bool astatus;
  119.   bool asstatus;
  120.   bool bstatus;
  121.   bool costatus;
  122.  
  123.   int atime = 03;  //pause for stability and minimizing "ticks" when the harp randomly plays itsself due to variation
  124.   int btime = 03;  //in environment light and static
  125.  
  126.   int cvalue = analogRead(cin);
  127.  
  128.   int csvalue = analogRead(csin);
  129.   int dvalue = analogRead(din);
  130.   int dsvalue = analogRead(dsin);
  131.   int evalue = analogRead(ein);
  132.   int fvalue = analogRead(fin);
  133.   int fsvalue = analogRead(fsin);
  134.   int gvalue = analogRead(gin);
  135.   int gsvalue = analogRead(gsin);
  136.   int avalue = analogRead(ain);
  137.   int asvalue = analogRead(ashin);
  138.   int bvalue = analogRead(bin);
  139.   int covalue = analogRead(coin);
  140.  
  141.   if (cvalue > threshold && !cstatus){ //if the current is below the threshold, the light is blocked
  142.     digitalWrite(cled, HIGH);   //turn on the corresponding light
  143.     MIDImessage(noteON, c+12*octave, velocity);  //play the note
  144.     cstatus = true;            //report that the note is on
  145.     delay(atime);               //delay for stability
  146.     }
  147.     else if(cvalue > threshold && cstatus){
  148.       delay(atime);              //if the note is already on just delay;do not play it again
  149.     }
  150.   else{                       //if the threshold is not reached
  151.     digitalWrite(cled, LOW); //turn off the light
  152.     MIDImessage(noteON, c+12*octave, 0x00); //turn the note off(play it with velocity 0)
  153.     cstatus = false;             //report that the note is off
  154.     delay(btime);                //delay the secondary time
  155.   }
  156.   if (csvalue > threshold && !csstatus){             //same for different note
  157.     digitalWrite(csled, HIGH);          //etc
  158.     MIDImessage(noteON, cs+12*octave, velocity);
  159.     csstatus = true;
  160.     delay(atime);
  161.     }
  162.     else if(csvalue > threshold && csstatus){
  163.       delay(atime);
  164.     }
  165.   else{
  166.     digitalWrite(csled, LOW);
  167.     MIDImessage(noteON, cs+12*octave, 0x00);
  168.     csstatus = false;
  169.     delay(btime);
  170.   }
  171.   if (dvalue > threshold+10 && !dstatus){
  172.     digitalWrite(dled, HIGH);
  173.     MIDImessage(noteON, d+12*octave, velocity);
  174.     dstatus = true;
  175.     delay(atime);
  176.     }
  177.     else if (dvalue > threshold+10 && dstatus == 1){
  178.       delay(atime);
  179.     }
  180.   else{
  181.     digitalWrite(dled, LOW);
  182.     MIDImessage(noteON, d+12*octave, 0x00);
  183.     dstatus = false;
  184.     delay(btime);
  185.   }
  186.   if (dsvalue > threshold && !dsstatus){
  187.     digitalWrite(dsled, HIGH);
  188.     MIDImessage(noteON, ds+12*octave, velocity);
  189.     dstatus = true;
  190.     delay(atime);
  191.     }
  192.     else if (dsvalue > threshold && dsstatus){
  193.       delay(atime);
  194.   }
  195.   else{
  196.     digitalWrite(dsled, LOW);
  197.     MIDImessage(noteON, ds+12*octave, 0x00);
  198.     dsstatus = false;
  199.     delay(btime);
  200.   }
  201.   if (evalue > threshold && !estatus){
  202.     digitalWrite(eled, HIGH);
  203.     MIDImessage(noteON, e+12*octave, velocity);
  204.     estatus = true;
  205.     delay(atime);
  206.     }
  207.     else if (evalue > threshold && estatus){
  208.      delay(atime);
  209.     }
  210.   else{
  211.     digitalWrite(eled, LOW);
  212.     MIDImessage(noteON, e+12*octave, 0x00);
  213.     estatus = false;
  214.     delay(btime);
  215.   }
  216.   if (fvalue > threshold && !estatus){
  217.     digitalWrite(fled, HIGH);
  218.     MIDImessage(noteON, f+12*octave, velocity);
  219.     fstatus = true;
  220.     delay(atime);
  221.     }
  222.     else if (fvalue > threshold && fstatus){
  223.       delay(atime);
  224.     }
  225.  
  226.   else{
  227.     digitalWrite(fled, LOW);
  228.     MIDImessage(noteON, f+12*octave, 0x00);
  229.     fstatus = false;
  230.     delay(btime);
  231.   }
  232.   if (fsvalue > threshold && !fsstatus){
  233.     digitalWrite(fsled, HIGH);
  234.     MIDImessage(noteON, fs+12*octave, velocity);
  235.     fsstatus = true;
  236.     delay(atime);
  237.     }
  238.     else if (fsvalue > threshold && fsstatus){
  239.       delay(atime);
  240.     }
  241.   else{
  242.     digitalWrite(fsled, LOW);
  243.     MIDImessage(noteON, fs+12*octave, 0x00);
  244.     fsstatus = false;
  245.     delay(btime);
  246.   }
  247.   if (gvalue > threshold && !gstatus){
  248.     digitalWrite(gled, HIGH);
  249.     MIDImessage(noteON, g+12*octave, velocity);
  250.     gstatus = true;
  251.     delay(atime);
  252.     }
  253.     else if (gvalue > threshold && gstatus){
  254.        delay(atime);
  255.     }
  256.   else{
  257.     digitalWrite(gled, LOW);
  258.     MIDImessage(noteON, g+12*octave, 0x00);
  259.     gstatus = false;
  260.     delay(btime);
  261.   }
  262.   if (gsvalue > threshold && !gsstatus){
  263.     digitalWrite(gsled, HIGH);
  264.     MIDImessage(noteON, gs+12*octave, velocity);
  265.     gsstatus = true;
  266.     delay(atime);
  267.     }
  268.     else if (gsvalue > threshold && gsstatus){
  269.       delay(atime);
  270.     }
  271.   else{
  272.     digitalWrite(gsled, LOW);
  273.     MIDImessage(noteON, gs+12*octave, 0x00);
  274.     gsstatus = false;
  275.     delay(btime);
  276.   }
  277.   if (avalue > threshold && !astatus){
  278.     digitalWrite(aled, HIGH);
  279.     MIDImessage(noteON, a+12*octave, velocity);
  280.     astatus = true;
  281.     delay(atime);
  282.     }
  283.     else if (avalue > threshold && astatus){
  284.       delay(atime);
  285.     }
  286.   else{
  287.     digitalWrite(aled, LOW);
  288.     MIDImessage(noteON, a+12*octave, 0x00);
  289.     astatus = false;
  290.     delay(btime);
  291.   }
  292.   if (asvalue > threshold && !asstatus){
  293.     digitalWrite(asled, HIGH);
  294.     MIDImessage(noteON, as+12*octave, velocity);
  295.     asstatus = true;
  296.     delay(atime);
  297.     }
  298.    
  299.     else if (asvalue > threshold && asstatus){
  300.       delay(atime);
  301.     }
  302.   else{
  303.     digitalWrite(asled, LOW);
  304.     MIDImessage(noteON, as+12*octave, 0x00);
  305.     asstatus = false;
  306.     delay(btime);
  307.   }
  308.   if (bvalue > threshold-4 && !bstatus){
  309.     digitalWrite(bled, HIGH);
  310.     MIDImessage(noteON, b+12*octave, velocity);
  311.     bstatus = true;
  312.     delay(atime);
  313.     }
  314.     else if (bvalue > threshold-4 && bstatus){
  315.       delay(atime);
  316.  
  317.   else{
  318.     digitalWrite(bled, LOW);
  319.     MIDImessage(noteON, b+12*octave, 0x00);
  320.     bstatus = false;
  321.     delay(atime);
  322.   }
  323.   if (covalue > threshold && !costatus){
  324.     digitalWrite(coled, HIGH);
  325.     MIDImessage(noteON, co+12*octave, velocity);
  326.     costatus = true;
  327.     delay(atime);
  328.     }
  329.     else if (covalue > threshold && costatus){
  330.       delay(atime);
  331.     }
  332.   else{
  333.     digitalWrite(coled, LOW);
  334.     MIDImessage(noteON, co+12*octave, 0x00);
  335.     costatus = false;
  336.     delay(atime);
  337.   }
  338. } //defines function for sending MIDI signals
  339.   void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  340.     Serial.write(command);//send note on or note off command
  341.     Serial.write(MIDInote);//send pitch data
  342.     Serial.write(MIDIvelocity);//send velocity data
  343.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top