/*
Information is received from the controller and IR sensor. Then, depending on the button pressed 
a specific song will play only after the butterfly wings flap 5 times, which symbolizes the 
loading of the song. 
*/

#include <IRremote.h> /*IRremote library*/
#include <SD.h> /*SD library*/
#include <TMRpcm.h> /*Arduino library for asynchronous playback of PCM/WAV files*/
#include <SPI.h> /*SPI library: allows you to communicate with SPI devices (the SD card reader in our case), with the Arduino as the master device*/
#include <Servo.h> /*Servo Library*/

#define SD_ChipSelectPin 4 /*connect pin 4 of arduino to cs pin of SD card */
#define button1 0xFF30CF /*Plays Pokemon Theme Song*/
#define button2 0xFF18E7 /*Plays 34 + 35*/
#define button3 0xFF7A85 /*Plays 3 Night*/
#define button4 0xFF10EF /*Plays Bad Day*/
#define Pause 0x20FE4DBB /*Pause or Play Music*/

int RECV_PIN = 2; /*IR receiver pin*/

/*Create objects for use in this sketch*/
Servo myservo;
IRrecv irrecv(RECV_PIN);
decode_results results;
TMRpcm tmrpcm; 


void setup() {
  Serial.begin(9600);
  myservo.attach(10); /*Servo connected to pin 10 */
  myservo.write(120);   /*servo starting position at 120 degrees*/
  irrecv.enableIRIn(); /* Start the receiver*/
  tmrpcm.speakerPin = 9; /* Speaker connected to pin 9 */
  tmrpcm.setVolume(4); /* To further amplify music (can be set from 0-7)*/

  /*Check if the SD card reader is functioning*/
  if (!SD.begin(SD_ChipSelectPin)) { // returns 1 if the card is present
    Serial.println("SD fail");
    return;
  }
  
}

void loop() {
  /*This loop will continously scan for an input from the controller and IR Sensor.
  Then, when an input matching the buttons defined in this sketch are pressed, an action will 
  play out*/
  
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX); /*Check the button*/
    
    irrecv.resume(); // Receive the next value

    /*If "1" is pressed, flap wings 5 times and then play Pokemon Theme Song 5 seconds in*/
    if (results.value == button1) {
      for (int x = 0; x <= 5; x++) {
        Serial.println("Butterfly Wings Flap");
        servo_move();
      } 
      Serial.println("Now Playing Pokemon Theme Song");
      tmrpcm.play("Pokemon.wav",5);
    }

    /*If "2" is pressed, flap wings 5 times and then plays Ariana Grande 34+35*/
    if (results.value == button2) {
      for (int x = 0; x < 5; x++) {
        Serial.println("Butterfly Wings Flap");
        servo_move();
      }
      Serial.println("Now Playing Ariana Grande");
      tmrpcm.play("3435.wav");
    }

    /*If "3" is pressed, flap wings 5 times and then play Dominic Fyke 3 Nights*/
    if (results.value == button3) {
      for (int x = 0; x < 5; x++) {
        Serial.println("Butterfly Wings Flap");
        servo_move();
      }
      Serial.println("Now Playing Dominic Fyke 3 Nights");
      tmrpcm.play("3Nights.wav");
    }

    /*If "4" is pressed, flap wings 5 times and then plays Bad Day*/
    if (results.value == button4) {
      for (int x = 0; x <= 5; x++) {
        Serial.println("Butterfly Wings Flap");
        servo_move();
      } 
      Serial.println("Now Playing Justus Bad Day");
      tmrpcm.play("BadDay.wav");
    }
 
    /*If Pause is pressed, music will stop and vice versa if it was already paused*/
    if (results.value == Pause){
      Serial.println("Music has been paused/resumed");
      tmrpcm.pause();
      }
  }
}

/*Function meant to move the servo between 120 degrees and 180 degrees*/
void servo_move() {
  for (int i = 120; i <= 180; i++) { 
    myservo.write(i);              
    delay(10);                       
  }
  for (int i = 180; i >= 120; i--) { 
    myservo.write(i);              
    delay(10);                       
  }
}
