/*
SETUP VAN HET ARDUINO BORD + DE MP3 SPELER:
  Connect the Serial MP3 Player to the Arduino board:
    GND → GND
    VCC → 5V
    TX → pin 11
    RX → pin 10

  Sluit de HC-SR04 ultrasonic sensor aan:
   - Sluit de VCC-pin aan op de 5V-pin van de Arduino.
   - Sluit de GND-pin aan op de GND-pin van de Arduino.
   - Verbind de Trig-pin met digitale pin 2 van de Arduino.
   - Verbind de Echo-pin met digitale pin 3 van de Arduino.
*/

#include "SerialMP3Player.h"

#define TX 11
#define RX 10

SerialMP3Player mp3(RX, TX);

// Voor de ultrasonic sensor
const int trigPin = 2;
const int echoPin = 3;

void setup() {
  // Start de seriële verbinding op baud rate 9600
  Serial.begin(9600); 

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  mp3.begin(9600);  // start mp3-communication
  delay(500);       // wait for init

  mp3.sendCommand(CMD_SEL_DEV, 0, 2);  //select sd-card
  delay(500);                          // wait for init
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance < 10) {
    // Start muziek als de afstand minder dan 10cm is.
    mp3.play(1);   // Begint met spelen van "FridayNight.mp3".
    delay(200);


    // Wat volgt zijn de LEDs die in sequences met delays een patroon in de muziek vormen.
    digitalWrite(5, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(700);             // wait for a second
    digitalWrite(5, LOW);   // turn the LED on (HIGH is the voltage level)

    digitalWrite(6, HIGH);
    delay(700);
    digitalWrite(6, LOW);

    digitalWrite(7, HIGH);
    delay(2000);
    digitalWrite(7, LOW);
    delay(400);


    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    delay(700);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);

    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    delay(700);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    delay(600);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(50);

    digitalWrite(5, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(250);             // wait for a second
    digitalWrite(5, LOW);   // turn the LED on (HIGH is the voltage level)

    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);

    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);

    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    delay(400);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(600);

    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);


    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);

    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);


    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);

    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);


    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);

    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(250);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);


    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(100);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(100);
    digitalWrite(7, LOW);
    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(100);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(100);
    digitalWrite(7, LOW);
    digitalWrite(5, HIGH);
    delay(50);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(50);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(50);
    digitalWrite(7, LOW);
    digitalWrite(5, HIGH);
    delay(50);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(50);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(50);
    digitalWrite(7, LOW);
    digitalWrite(5, HIGH);
    delay(50);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(50);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(50);
    digitalWrite(7, LOW);

    delay(500);
    digitalWrite(5, HIGH);
    delay(500);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(250);
    digitalWrite(7, LOW);
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(750);
    digitalWrite(7, LOW);
  } else {
    mp3.stop();
  }
}