#include <MusicWithoutDelay.h>
#include <synth.h>
#include <tables.h>

//note values
const char note1[] PROGMEM = ":d=128,b=300:c-1+";  //plays c4
const char note2[] PROGMEM = ":d=128,b=300:e-1+";  //plays e4
const char note3[] PROGMEM = ":d=128,b=300:g-1+";  //plays g4
const char note4[] PROGMEM = ":d=128,b=300:c+";  //plays c5
const char note5[] PROGMEM = ":d=128,b=300:d+";  //plays d5
const char note6[] PROGMEM = ":d=128,b=300:e+";  //plays e5
const char note7[] PROGMEM = ":d=128,b=300:f+";  //plays f5
const char note8[] PROGMEM = ":d=128,b=300:g+";  //plays g5
const char note9[] PROGMEM = ":d=128,b=300:a+";  //plays a5
const char note10[] PROGMEM = ":d=128,b=300:b+";  //plays b5

//instruments
MusicWithoutDelay myI(note1);
MusicWithoutDelay myIn(note2);
MusicWithoutDelay myIns(note3);
MusicWithoutDelay myInst(note4);
MusicWithoutDelay myInstr(note5);
MusicWithoutDelay myInstru(note6);
MusicWithoutDelay myInstrum(note7);
MusicWithoutDelay myInstrume(note8);
MusicWithoutDelay myInstrumen(note9);
MusicWithoutDelay myInstrument(note10);

int count;
int vol;
int VA0, VA1, VA2, VA3, VA4, VA5, VA6, VA7, VA8, VA9;
int TH0, TH1, TH2, TH3, TH4, TH5, TH6, TH7, TH8, TH9;
int VOL0, VOL1, VOL2, VOL3, VOL4, VOL5, VOL6, VOL7, VOL8, VOL9;
bool CV;

bool BA;
bool BB;
bool BC;
bool BD;
bool BE;
bool BF;
bool BG;
bool BH;
bool BI;
bool BJ;

void setup() {
    Serial.begin(9600);

    //calibrate the LDRs
    Serial.println ("Calib");
    TH0 = (analogRead(A0)+20);
    TH1 = (analogRead(A1)+20);
    TH2 = (analogRead(A2)+20);
    TH3 = (analogRead(A3)+20);
    TH4 = (analogRead(A4)+20);
    TH5 = (analogRead(A5)+20);
    TH6 = (analogRead(A6)+20);
    TH7 = (analogRead(A7)+20);
    TH8 = (analogRead(A8)+20);
    TH9 = (analogRead(A9)+20);
   
    Serial.print ("TH0=");
    Serial.println (TH0);
    Serial.print ("TH1=");
    Serial.println (TH1);
    Serial.print ("TH2=");
    Serial.println (TH2);
    Serial.print ("TH3=");
    Serial.println (TH3);
    Serial.print ("TH4=");
    Serial.println (TH4);
    Serial.print ("TH5=");
    Serial.println (TH5);
    Serial.print ("TH6=");
    Serial.println (TH6);
    Serial.print ("TH7=");
    Serial.println (TH7);
    Serial.print ("TH8=");
    Serial.println (TH8);
    Serial.print ("TH9=");
    Serial.println (TH9);

    Serial.println("Wait4MWD");
    
    Serial.println("Ini MWD");
    myI.begin(CHA, SINE, ENVELOPE0, 0);
    myIn.begin(SINE, ENVELOPE0, 0);
    myIns.begin(SINE, ENVELOPE0, 0);
    myInst.begin(SINE, ENVELOPE0, 0);
    myInstr.begin(SINE, ENVELOPE0, 0);
    myInstru.begin(SINE, ENVELOPE0, 0);
    myInstrum.begin(SINE, ENVELOPE0, 0);
    myInstrume.begin(SINE, ENVELOPE0, 0);
    myInstrumen.begin(SINE, ENVELOPE0, 0);
    myInstrument.begin(SINE, ENVELOPE0, 0);

    myI.pause(true);
    myIn.pause(true);
    myIns.pause(true);
    myInst.pause(true);
    myInstr.pause(true);
    myInstru.pause(true);
    myInstrum.pause(true);
    myInstrume.pause(true);
    myInstrumen.pause(true);
    myInstrument.pause(true);

    Serial.println("go!");
}

void loop() {

    myI.update();
    myIn.update();
    myIns.update();
    myInst.update();
    myInstr.update();
    myInstru.update();
    myInstrum.update();
    myInstrume.update();
    myInstrumen.update();
    myInstrument.update();

    //Messure LDR multiplex inputs
    VA0 = analogRead(A0);
    VA1 = analogRead(A1);
    VA2 = analogRead(A2);
    VA3 = analogRead(A3);
//
//    Serial.print("VA0: "); Serial.println(VA0);
//    Serial.print("VA1: "); Serial.println(VA1);
//    Serial.print("VA2: "); Serial.println(VA2);
//    Serial.print("VA3: "); Serial.println(VA3);
//    Serial.println(" ");
  
    VA4 = analogRead(A4);
    VA5 = analogRead(A5);
    VA6 = analogRead(A6);
    VA7 = analogRead(A7);
//
//    Serial.print("VA4: "); Serial.println(VA4);
//    Serial.print("VA5: "); Serial.println(VA5);
//    Serial.print("VA6: "); Serial.println(VA6);
//    Serial.print("VA7: "); Serial.println(VA7);
//    Serial.println(" ");
  
    VA8 = analogRead(A8);
    VA9 = analogRead(A9);
//    
//    Serial.print("VA8: "); Serial.println(VA8);
//    Serial.print("VA9: "); Serial.println(VA9);
//    Serial.println(" ");
  
    count = 0;
    CV = digitalRead(22);
    
    if (VA0 > TH0) {
        count++;
    }
    if (VA1 > TH1) {
        count++;
    }
    if (VA2 > TH2) {
        count++;
    }
    if (VA3 > TH3) {
        count++;
    }
    if (VA4 > TH4) {
        count++;
    }
    if (VA5 > TH5) {
        count++;
    }
    if (VA6 > TH6) {
        count++;
    }
    if (VA7 > TH7) {
        count++;
    }
    if (VA8 > TH8) {
        count++;
    }
    if (VA9 > TH9) {
        count++;
    }

//    Serial.print("Intruments playing = ");
//    Serial.println(count);

    //Play notes and mix down (set volume depending on number of notes playing)
    if (VA0 > TH0 && !BA) {
        vol = 100/count + 25;
        myI.play(true);
        myI.setVolume(vol);
        VOL0 = constrain(map(VA0, TH0, 500, 30, 127), 30, 127);
        //myI.setMod(VOL0);
        myI.setVolume(VOL0);
        BA == true;
//        Serial.print("Tone1 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(23, HIGH);
          }
        else if (!CV) {
          digitalWrite(23, LOW);           
           }
    }
    
    else if (VA0 < TH0 && BA) {
        myI.pause (true);
        BA == false;
        
        //Kill ControlVoltage
        digitalWrite(23, LOW);        
    }
    
    if (VA1 > TH1 && !BB) {
        vol = 100/count + 25;
        myIn.play(true);
        myIn.setVolume(vol);
        VOL1 = constrain(map(VA1, TH1, 500, 30, 127), 30, 127);
        //myIn.setMod(VOL1);
        myIn.setVolume(VOL1); 
        BB == true;
//        Serial.print("Tone2 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(25, HIGH);
          }
        else if (!CV) {
          digitalWrite(25, LOW);           
           }
    }

    else if (VA1 < TH1 && BB) {
        myIn.pause (true);
        BB == false;
        
        //Kill ControlVoltage
        digitalWrite(25, LOW); 
    }
    
    if (VA2 > TH2 && !BC) {
        vol = 100/count + 25;
        myIns.play(true);
        myIns.setVolume(vol);
        VOL2 = constrain(map(VA2, TH2, 500, 30, 127), 30, 127);
        //myIns.setMod(VOL2);
        myIns.setVolume(VOL2);     
        BC == true;
//        Serial.print("Tone3 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(27, HIGH);
          }
        else if (!CV) {
          digitalWrite(27, LOW);           
           }
    }

    else if (VA2 < TH2 && BC) {
        myIns.pause (true);
        BC == false;
        
        //Kill ControlVoltage
        digitalWrite(27, LOW);
    }
    
    if (VA3 > TH3 && !BD) {
        vol = 100/count + 25;
        myInst.play(true);
        myInst.setVolume(vol);
        VOL3 = constrain(map(VA3, TH3, 500, 30, 127), 30, 127);
        //myInst.setMod(VOL3);
        myInst.setVolume(VOL3);     
        BD == true;
//        Serial.print("Tone4 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(29, HIGH);
          }
        else if (!CV) {
          digitalWrite(29, LOW);           
           }
    }

    else if (VA3 < TH3 && BD) {
        myInst.pause (true);
        BD == false;
        
        //Kill ControlVoltage
        digitalWrite(29, LOW); 
    }

    if (VA4 > TH4 && !BE) {
        vol = 100/count + 25;
        myInstr.play(true);
        myInstr.setVolume(vol);
        VOL4 = constrain(map(VA4, TH4, 500, 30, 127), 30, 127);
        //myInstr.setMod(VOL4);
        myInstr.setVolume(VOL4);
        BE == true;
//        Serial.print("Tone5 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(31, HIGH);
          }
        else if (!CV) {
          digitalWrite(31, LOW);           
           }
    }
  
    else if (VA4 < TH4 && BE) {
        myInstr.pause (true);
        BE == false;
        
        //Kill ControlVoltage
        digitalWrite(31, LOW);
    }

    if (VA5 > TH5 && !BF) {
        vol = 100/count + 25;
        myInstru.play(true);
        myInstru.setVolume(vol);
        VOL5 = constrain(map(VA5, TH5, 500, 30, 127), 30, 127);
        //myInstru.setMod(VOL5);
        myInstru.setVolume(VOL5);
        BF == true;
//        Serial.print("Tone6 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(33, HIGH);
          }
        else if (!CV) {
          digitalWrite(33, LOW);           
           }
    }
  
    else if (VA5 < TH5 && BF) {
        myInstru.pause (true);
        BF == false;
        
        //Kill ControlVoltage
        digitalWrite(33, LOW); 
    }


    if (VA6 > TH6 && !BG) {
        vol = 100/count + 25;
        myInstrum.play(true);
        myInstrum.setVolume(vol);
        VOL6 = constrain(map(VA6, TH6, 500, 30, 127),30, 127);
        //myInstrum.setMod(VOL6);
        myInstrum.setVolume(VOL6);       
        BG == true;
//        Serial.print("Tone7 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(35, HIGH);
          }
        else if (!CV) {
          digitalWrite(35, LOW);           
           }
    }
  
    else if (VA6 < TH6 && BG) {
        myInstrum.pause (true);
        BG == false;
        
        //Kill ControlVoltage
        digitalWrite(35, LOW);  
    }

    if (VA7 > TH7 && !BH) {
        vol = 100/count + 25;
        myInstrume.play(true);
        myInstrume.setVolume(vol);
        VOL7 = constrain(map(VA7, TH7, 500, 30, 127), 30, 127);
        //myInstrume.setMod(VOL7);
        myInstrume.setVolume(VOL7);       
        BH == true;
//        Serial.print("Tone8 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(37, HIGH);
          }
        else if (!CV) {
          digitalWrite(37, LOW);           
           }
    }
  
    else if (VA7 < TH7 && BH) {
        myInstrume.pause (true);
        BH == false;
        
        //Kill ControlVoltage
        digitalWrite(37, LOW); 
    }


    if (VA8 > TH8 && !BI) {
        vol = 100/count + 25;
        myInstrumen.play(true);
        myInstrumen.setVolume(vol);
        VOL8 = constrain(map(VA8, TH8, 500, 30, 127), 30, 127);
        //myInstrumen.setMod(VOL8);
        myInstrumen.setVolume(VOL8);
        BI == true;
//        Serial.print("Tone9 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(39, HIGH);
          }
        else if (!CV) {
          digitalWrite(39, LOW);           
           }
    }

    else if (VA8 < TH8 && BI) {
        myInstrumen.pause (true);
        BI == false;
        
        //Kill ControlVoltage
        digitalWrite(39, LOW); 
    }
    
    if (VA9 > TH9 && !BJ) {
        vol = 100/count + 25;
        myInstrument.play(true);
        myInstrument.setVolume(vol);
        VOL9 = constrain(map(VA9, TH9, 500, 30, 127), 30, 127);
        //myInstrument.setMod(VOL9);
        myInstrument.setVolume(VOL9);
        BJ == true;
//        Serial.print("Tone10 active at vol:");
//        Serial.println(vol);

        //Output ControlVoltage if switch is activated
        if (CV) {
          digitalWrite(41, HIGH);
          }
        else if (!CV) {
          digitalWrite(41, LOW);           
           }
    }

    else if (VA9 < TH9 && BJ) {
        myInstrument.pause (true);
        BJ == false;
        
        //Kill ControlVoltage
        digitalWrite(41, LOW);
    }
}
