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

//multiplexer adresses
Adafruit_ADS1115 adsa (0x48);
Adafruit_ADS1115 adsb (0x49);
Adafruit_ADS1115 adsc (0x4A);
Adafruit_ADS1115 adsd (0x4B);

//note values
const char note1[] PROGMEM = {"::c-1"};  //plays c4
const char note2[] PROGMEM = {"::e-1"};  //plays e4
const char note3[] PROGMEM = {"::f-1"};  //plays f4
const char note4[] PROGMEM = {"::g-1"};  //plays g4
const char note5[] PROGMEM = {"::c"};  //plays c5
const char note6[] PROGMEM = {"::e"};  //plays e5
const char note7[] PROGMEM = {"::f"};  //plays f5
const char note8[] PROGMEM = {"::g"};  //plays g5
//const char note9[] PROGMEM = {"::c+1"};  //plays c6
//const char note10[] PROGMEM = {"::e+1"};  //plays e6
//const char note11[] PROGMEM = {"::f+1"};  //plays f6
//const char note12[] PROGMEM = {"::g+1"};  //plays g6




//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);
//MusicWithoutDelay myInstruments(note11);
//MusicWithoutDelay myInstrumentss(note12);

int count;
int vol;
int TH;
int16_t adc0a, adc1a, adc2a, adc3a, adc0b, adc1b, adc2b, adc3b, adc0c, adc1c, adc2c, adc3c, adc0d, adc1d, adc2d, adc3d;
//bool codeswitch = false;

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



void setup() {
    Serial.begin(115200);
  
    //set the gain for multiplexed ins
    adsa.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
    adsb.setGain(GAIN_ONE); 
    adsc.setGain(GAIN_ONE); 
    adsd.setGain(GAIN_ONE); 
    
    //start the multiplexing
    Serial.println("Initializing Multiplexing ");
    adsa.begin();
    adsb.begin();
    adsc.begin();
    adsd.begin();
    Serial.println("Multiplexing Initialized");

    //calibrate the LDRs
    Serial.println ("Calibrating");
    TH = (adsb.readADC_SingleEnded(3));
    Serial.print ("TH=");
    Serial.println (TH);

    Serial.println("Initializing Music Without Delay");
    myI.begin(CHA, TRIANGLE, ENVELOPE0, 0);
    myIn.begin(TRIANGLE, ENVELOPE0, 0);
    myIns.begin(TRIANGLE, ENVELOPE0, 0);
    myInst.begin(TRIANGLE, ENVELOPE0, 0);
    Serial.println("Music Witout Delay Initialized");
    Serial.println("Start messuring LDRs");
}

void loop() {

//    myI.update();
//    myIn.update();
//    myIns.update();
//    myInst.update();

    //Messure LDR multiplex inputs

    adc0a = adsa.readADC_SingleEnded(0);
    adc1a = adsa.readADC_SingleEnded(1);
    adc2a = adsa.readADC_SingleEnded(2);
    adc3a = adsa.readADC_SingleEnded(3);

    Serial.print("AIN0A: "); Serial.println(adc0a);
    Serial.print("AIN1A: "); Serial.println(adc1a);
    Serial.print("AIN2A: "); Serial.println(adc2a);
    Serial.print("AIN3A: "); Serial.println(adc3a);
    Serial.println(" ");
  
    adc0b = adsb.readADC_SingleEnded(0);
    adc1b = adsb.readADC_SingleEnded(1);
    adc2b = adsb.readADC_SingleEnded(2);
    adc3b = adsb.readADC_SingleEnded(3);

    Serial.print("AIN0B: "); Serial.println(adc0b);
    Serial.print("AIN1B: "); Serial.println(adc1b);
    Serial.print("AIN2B: "); Serial.println(adc2b);
    Serial.print("AIN3B: "); Serial.println(adc3b);
    Serial.println(" ");
  
    adc0c = adsc.readADC_SingleEnded(0);
    adc1c = adsc.readADC_SingleEnded(1);
    adc2c = adsc.readADC_SingleEnded(2);
    adc3c = adsc.readADC_SingleEnded(3);

    Serial.print("AIN0C: "); Serial.println(adc0c);
    Serial.print("AIN1C: "); Serial.println(adc1c);
    Serial.print("AIN2C: "); Serial.println(adc2c);
    Serial.print("AIN3C: "); Serial.println(adc3c);
    Serial.println(" ");
  
    adc0d = adsd.readADC_SingleEnded(0);
    adc1d = adsd.readADC_SingleEnded(1);
    adc2d = adsd.readADC_SingleEnded(2);
    adc3d = adsd.readADC_SingleEnded(3);

    Serial.print("AIN0D: "); Serial.println(adc0d);
    Serial.print("AIN1D: "); Serial.println(adc1d);
    Serial.print("AIN2D: "); Serial.println(adc2d);
    Serial.print("AIN3D: "); Serial.println(adc3d);
    Serial.println(" ");

    count = 0;
    
    if (adc0a < TH) {
        count++;
    }
    if (adc1a < TH) {
        count++;
    }
    if (adc2a < TH) {
        count++;
    }
    if (adc3a < TH) {
        count++;
    }
    if (adc0b < TH) {
        count++;
    }
    if (adc1b < TH) {
        count++;
    }
    if (adc2b < TH) {
        count++;
    }
    if (adc3b < TH) {
        count++;
    }
//    if (adc0c < TH) {
//        count++;
//    }
//    if (adc1c < TH) {
//        count++;
//    }
//    if (adc2c < TH) {
//        count++;
//    }
//    if (adc3c < TH) {
//        count++;
//    }   

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

    //Play notes and mix down (set volume depending on number of notes playing)
    if (adc0a < TH && !BA) {
        vol = 120/count;
        myI.play(true);
        myI.setVolume(vol);
//        myI.setSustain(50);
        //myI.setMod(int mod1 = map(adc0a, 3000, 60000, 0, 127);
        BA == true;
        Serial.print("Tone1 active at vol:");
        Serial.println(vol);
    }

    else if (adc0a > TH && BA) {
        myI.pause (true);
        BA == false;
        Serial.println("Tone1 not active");
    }
    if (adc1a < TH && !BB) {
        vol = 120/count;
        myIn.play(true);
        myIn.setVolume(vol);
//        myIn.setSustain(50);
        BB == true;
        Serial.print("Tone2 active at vol:");
        Serial.println(vol);
    }

    else if (adc1a > TH && BB) {
        myIn.pause (true);
        BB == false;
        Serial.println("Tone2 not active");
    }
    
    if (adc2a < TH && !BC) {
        vol = 120/count;
        myIns.play(true);
        myIns.setVolume(vol);
//        myIns.setSustain(50);
        BC == true;
        Serial.print("Tone3 active at vol:");
        Serial.println(vol);
    }

    else if (adc2a > TH && BC) {
        myIns.pause (true);
        BC == false;
        Serial.println("Tone3 not active");
    }
    
    if (adc3a < TH && !BD) {
        vol = 120/count;
        myInst.play(true);
        myInst.setVolume(vol);
//        myInst.setSustain(50);
        BD == true;
        Serial.print("Tone4 active at vol:");
        Serial.println(vol);
    }

    else if (adc3a < TH && BD) {
        myInst.pause (true);
        BD == false;
        Serial.println("Tone4 not active"); 
    }

    if (adc0b < TH && !BE) {
        vol = 120/count;
        myInstr.play(true);
        myInstr.setVolume(vol);
//      myInst.setSustain(50);
        BE == true;
        Serial.print("Tone5 active at vol:");
        Serial.println(vol);
    }
  
    else if (adc0b < TH && BE) {
        myInstr.pause (true);
        BE == false;
        Serial.println("Tone5 not active"); 
    }

    if (adc1b < TH && !BF) {
        vol = 120/count;
        myInstru.play(true);
        myInstru.setVolume(vol);
//      myInst.setSustain(50);
        BF == true;
        Serial.print("Tone6 active at vol:");
        Serial.println(vol);
    }
  
    else if (adc1b < TH && BF) {
        myInstru.pause (true);
        BF == false;
        Serial.println("Tone6 not active"); 
    }


    if (adc2b < TH && !BG) {
        vol = 120/count;
        myInstrum.play(true);
        myInstrum.setVolume(vol);
//      myInst.setSustain(50);
        BG == true;
        Serial.print("Tone7 active at vol:");
        Serial.println(vol);
    }
  
    else if (adc2b < TH && BG) {
        myInstrum.pause (true);
        BG == false;
        Serial.println("Tone7 not active"); 
    }

    if (adc3b < TH && !BH) {
        vol = 120/count;
        myInstrume.play(true);
        myInstrume.setVolume(vol);
//      myInst.setSustain(50);
        BH == true;
        Serial.print("Tone8 active at vol:");
        Serial.println(vol);
    }
  
    else if (adc3b < TH && BH) {
        myInstrume.pause (true);
        BH == false;
        Serial.println("Tone8 not active"); 
    }
//    if (adc0b < TH) {
//        //result += tone_5.next() * (1/count);
//    }
//    if (adc1b < TH) {
//        //result += tone_6.next() * (1/count);
//    }
//    if (adc2b < TH) {
//        //result += tone_7.next() * (1/count);
//    }
//    if (adc3b < TH) {
//        //result += tone_8.next() * (1/count);
//    }
//    if (adc0c < TH) {
//        //result += tone_9.next() * (1/count);
//    }
//    if (adc1c < TH) {
//        //result += tone_10.next() * (1/count);
//    }
//    if (adc2c < TH) {
//        //result += tone_11.next() * (1/count);
//    }
//    if (adc3c < TH) {
//        //result += tone_12.next() * (1/count);
//    }
}
