//#include <SensorToButton.h>

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


//note values
const char note0[] PROGMEM = ":d=128,b=300:c-1+";  //plays c4
const char note1[] PROGMEM = ":d=128,b=300:d-1+";  //plays d4
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:a-1+";  //plays a4
const char note5[] PROGMEM = ":d=128,b=300:c+";  //plays c5
const char note6[] PROGMEM = ":d=128,b=300:d+";  //plays d5
const char note7[] PROGMEM = ":d=128,b=300:e+";  //plays e5
const char note8[] PROGMEM = ":d=128,b=300:g+";  //plays g5
const char note9[] PROGMEM = ":d=128,b=300:a+";  //plays a5

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




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 ZER;
bool ONE;
bool TWO;
bool THR;
bool FOU;
bool FIV;
bool SIX;
bool SEV;
bool EIG;
bool NIN;

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

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

  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, TRIANGLE, ENVELOPE0, 0);
  myIn.begin(TRIANGLE, ENVELOPE0, 0);
  myIns.begin(TRIANGLE, ENVELOPE0, 0);
  myInst.begin(TRIANGLE, ENVELOPE0, 0);
  myInstr.begin(TRIANGLE, ENVELOPE0, 0);
  myInstru.begin(TRIANGLE, ENVELOPE0, 0);
  myInstrum.begin(TRIANGLE, ENVELOPE0, 0);
  myInstrume.begin(TRIANGLE, ENVELOPE0, 0);
  myInstrumen.begin(TRIANGLE, ENVELOPE0, 0);
  myInstrument.begin(TRIANGLE, 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() {
  CV = digitalRead(22);
  count = 0;

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

  //Messure LDR 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(" ");


  if (VA0 > TH0) {
    count++;
    ZER = true;
  }
  else if (VA0 < TH0) {
    ZER = false;
  }
    
  if (VA1 > TH1) {
    count++;
    ONE = true;
  }
  else if (VA1 < TH1) {
    ONE = false;
  }
  
  if (VA2 > TH2) {
    count++;
    TWO = true;
  }
  else if (VA2 < TH2) {
    TWO = false;
  }

  if (VA3 > TH3) {
    count++;
    THR = true;
  }
  else if (VA3 < TH3) {
    THR = false;
  }

  if (VA4 > TH4) {
    count++;
    FOU = true;
  }
  else if (VA4 < TH4) {
    FOU = false;
  }

  if (VA5 > TH5) {
    count++;
    FIV = true;
  }
  else if (VA5 < TH5) {
    FIV = false;
  }

  if (VA6 > TH6) {
    count++;
    SIX = true;
  }
  else if (VA6 < TH6) {
    SIX = false;
  }

  if (VA7 > TH7) {
    count++;
    SEV = true;
  }
  else if (VA7 < TH7) {
    SEV = false;
  }

  if (VA8 > TH8) {
    count++;
    EIG = true;
  }
  else if (VA8 < TH8) {
    EIG = false;
  }

  if (VA9 > TH9) {
    count++;
    NIN = true;
  }
  else if (VA9 < TH9) {
    NIN = false;
  }


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

  //Play notes and mix down (set volume depending on number of notes playing)
  if (ZER) {
    myI.play(true);
    VOL0 = 50/count+constrain(map(analogRead(A0), TH0, TH0+450, 0, 50), 0, 50);
    myI.setVolume(VOL0);
//    Serial.print("Tone0 active at vol:");
//    Serial.println(VOL0);
//    Serial.println(analogRead(A0));

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

  if (!ZER) {
    myI.pause (true);

    //Kill ControlVoltage
    digitalWrite(23, LOW);
  }

  if (ONE) {
    myIn.play(true);
    VOL1 = 50/count+constrain(map(analogRead(A1), TH1, TH1+450, 0, 50), 0, 50);
    myIn.setVolume(VOL1);
//    Serial.print("Tone1 active at vol:");
//    Serial.println(VOL1);
//    Serial.println(analogRead(A1));

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

  if (!ONE) {
    myIn.pause (true);

    //Kill ControlVoltage
    digitalWrite(25, LOW);
  }

  if (TWO) {
    myIns.play(true);
    VOL2 = 50/count+constrain(map(analogRead(A2), TH2, TH2+450, 0, 50), 0, 50);
    myIns.setVolume(VOL2);
//    Serial.print("Tone2 active at vol:");
//    Serial.println(VOL2);
//    Serial.println(analogRead(A2));

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

  if (!TWO) {
    myIns.pause (true);

    //Kill ControlVoltage
    digitalWrite(27, LOW);
  }

  if (THR) {
    myInst.play(true);
    VOL3 = 50/count+constrain(map(analogRead(A3), TH3, TH3+450, 0, 50), 0, 50);
    myInst.setVolume(VOL3);
    //        BD == true;
//    Serial.print("Tone3 active at vol:");
//    Serial.println(VOL3);
//    Serial.println(analogRead(A3));

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

  if (!THR) {
    myInst.pause (true);

    //Kill ControlVoltage
    digitalWrite(29, LOW);
  }

  if (FOU) {
    myInstr.play(true);
    VOL4 = 50/count+constrain(map(analogRead(A4), TH4, TH4+450, 0, 50), 0, 50);
    myInstr.setVolume(VOL4);
    //        BE == true;
//    Serial.print("Tone4 active at vol:");
//    Serial.println(VOL4);
//    Serial.println(analogRead(A4));

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

  if (!FOU) {
    myInstr.pause (true);

    //Kill ControlVoltage
    digitalWrite(31, LOW);
  }

  if (FIV) {
    myInstru.play(true);
    VOL5 = 50/count+constrain(map(analogRead(A5), TH5, TH5+450, 0, 50), 0, 50);
    //myInstru.setMod(VOL5);
    myInstru.setVolume(VOL5);
//    Serial.print("Tone5 active at vol:");
//    Serial.println(VOL5);
//    Serial.println(analogRead(A5));

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

  if (!FIV) {
    myInstru.pause (true);

    //Kill ControlVoltage
    digitalWrite(33, LOW);
  }


  if (SIX) {
    myInstrum.play(true);
    VOL6 = 50/count+constrain(map(analogRead(A6), TH6, TH6+450, 0, 50), 0, 50);
    //myInstrum.setMod(VOL6);
    myInstrum.setVolume(VOL6);
//    Serial.print("Tone6 active at vol:");
//    Serial.println(VOL6);
//    Serial.println(analogRead(A6));

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

  if (!SIX) {
    myInstrum.pause (true);

    //Kill ControlVoltage
    digitalWrite(35, LOW);
  }

  if (SEV) {
    myInstrume.play(true);
    VOL7 = 50/count+constrain(map(analogRead(A7), TH7, TH7+450, 0, 50), 0, 50);
    myInstrume.setVolume(VOL7);
//    Serial.print("Tone7 active at vol:");
//    Serial.println(VOL7);
//    Serial.println(analogRead(A7));

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

  if (!SEV) {
    myInstrume.pause (true);

    //Kill ControlVoltage
    digitalWrite(37, LOW);
  }


  if (EIG) {
    myInstrumen.play(true);
    VOL8 = 50/count+constrain(map(analogRead(A8), TH8, TH8+450, 0, 50), 0, 50);
    myInstrumen.setVolume(VOL8);
//    Serial.print("Tone8 active at vol:");
//    Serial.println(VOL8);
//    Serial.println(analogRead(A8));

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

  if (!EIG) {
    myInstrumen.pause (true);

    //Kill ControlVoltage
    digitalWrite(39, LOW);
  }

  if (NIN) {
    myInstrument.play(true);
    VOL9 = 50/count+constrain(map(analogRead(A9), TH9, TH9+450, 0, 50), 0, 50);
    myInstrument.setVolume(VOL9);
//    Serial.print("Tone9 active at vol:");
//    Serial.println(VOL9);
//    Serial.println(analogRead(A9));

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

  if (!NIN) {
    myInstrument.pause (true);

    //Kill ControlVoltage
    digitalWrite(41, LOW);
  }
}
