// Luminarpe 3.0
// Crée par Mathias Blandeau 2014

// LES LIGNES CI DESSOUS SONT NECESSAIRES AU MUSIC MAKER SHIELD

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>


// pin used by the shield
#define RESET -1 // VS1053 reset pin (unused)
#define CS 7 // VS1053 chip select pin (output)
#define DCS 6 // VS1053 Data/command select pin (output)
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);


// DEBUT DU CODE LUMINARPE

const int cordeMax = 5; // total number of chords
int sensorPin[cordeMax]; // infrared measured 
const int analogPin[] = {A8, A9, A10, A11, A12};
const int potentioPin = A7;
const int ledPin = 13; // for debugging
const int irPin = 22; // infrared emitter alimentation
const int motorPin[] = {14, 15, 16, 17, 18}; // for vibrating motors

char *files[5]={"track001.mp3", "track002.mp3", "track003.mp3", "track004.mp3", "track005.mp3"};

int corde = 0; // chord id
int potentioValue; 
int ambientIR; // infrared measure with the IR emitter OFF
int activeIR; // infrared measure with the IR emitter ON

int ledState[cordeMax]; // to know which chord is activated
float sensorTemp[cordeMax]; // difference between ambientIR and activeIR

int DEBUG = 0; // for debuggin 0:OFF, 1:ON
int debugMax = 51;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);

  pinMode(irPin, OUTPUT);    // IR emitter LED on digital pin

  for(int i = 0 ; i < cordeMax ; i++){
    ledState[i] = 0;               // Initialize all ledState to 0
    sensorTemp[i] = 0;              // Initialize all sensorTemp to 0
    sensorPin[i] = analogPin[i]; 
    pinMode(motorPin[i], OUTPUT); // Vibrating Motor on digital pin
  }

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }

  SD.begin(CARDCS); // initialise the SD card
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);
  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
  
  
  musicPlayer.startPlayingFile(files[0]); //OK GO
}


void loop(){

  if (corde == cordeMax) corde = 0;
  
  delay(10); //if I don't do that the harp bugs

  digitalWrite(irPin,LOW);        // turning the IR LEDs off to read the IR coming from the ambient
  delay(1);                              // minimum delay necessary to read values
  ambientIR = analogRead(sensorPin[corde]);  // storing IR coming from the ambient in the middle of the harpe
  digitalWrite(irPin,HIGH);       // turning the IR LEDs on to read the IR coming from the obstacle
  delay(1);                              // minimum delay necessary to read values

  potentioValue = analogRead(potentioPin); // the potentiometre is to change the sensitivity off the chords (if light changes for example)

  activeIR = analogRead(sensorPin[corde]);

  sensorTemp[corde] = activeIR - ambientIR;

  if (sensorTemp[corde] > potentioValue)
  {
    if (ledState[corde] == 0){
      ledState[corde] = 1;
//      Serial.write(corde); //for later communication with computer
      if(!musicPlayer.playingMusic)
      {
        musicPlayer.startPlayingFile(files[corde]);
        delay(1);
        digitalWrite(motorPin[corde], HIGH);
        delay(1);
      } 
    }
  }
  else
  {
    if (ledState[corde] == 1){
      ledState[corde] = 0;
      musicPlayer.stopPlaying();
      delay(1);
      digitalWrite(motorPin[corde], LOW);
      delay(1);
    }
  }



  if (DEBUG){
    DEBUG += 1;
    //    if (debugMin < activeIR){
    //      debugMin = activeIR;
    //    }
    if (DEBUG > debugMax){
      DEBUG = 1;

//      Serial.println(corde);
//      Serial.print("potar :  ");
//      Serial.println(potentioValue);
//      Serial.print("ambiant : ");
//      Serial.println(ambientIR);
//      Serial.print("activeIR : ");
//      Serial.println(activeIR);
//      Serial.print("sensorTemp : ");
//      Serial.println(sensorTemp[corde]);
//      Serial.println("");  

//      Serial.print("corde 1 : ");
//      Serial.println(sensorTemp[0]);
//      Serial.println(ledState[0]);
//      Serial.print("corde 2 : ");
//      Serial.println(sensorTemp[1]);   
//      Serial.println(ledState[1]);
//      Serial.print("corde 3 : ");
//      Serial.println(sensorTemp[2]);   
//      Serial.println(ledState[2]);
//      Serial.print("corde 4 : ");
//      Serial.println(sensorTemp[3]);   
//      Serial.println(ledState[3]);
//      Serial.print("corde 5 : ");
//      Serial.println(sensorTemp[4]);
//      Serial.println(ledState[4]);
//      Serial.println("");     

    }
  }

  corde += 1; 
}


