﻿#include <Wire.h>
#include <LiquidCrystal_I2C.h>

int MELODY_PIN = 3;
int BUTTON_MUTE = 9;
int BUTTON_SKIP = 8;
int BUTTON_REWIND = 10;
int BUTTON_RESET = 11;

LiquidCrystal_I2C lcd(0x27, 16, 2);  

const int melody1[] = {392, 587, 784, 740, 587, 392, 440, 466, 587, 392, 392, 440, 466, 587, 392, 392, 440, 466, 440, 392, 370, 294, 370, 392, 587, 784, 740, 587, 392, 440, 466, 587, 392, 392, 440, 466, 440, 392, 370, 294, 370, 392, 392, 587, 784, 740, 587, 392, 440, 466, 587, 392, 392, 440, 466, 587, 392, 392, 440, 466, 440, 392, 370, 294, 370, 392, 587, 784, 740, 587, 392, 440, 466, 587, 392, 392, 440, 466, 440, 392, 370, 294, 370, 392, 0}; //blackboxwarrior notes

const int melody2[] = {0, 0, 0, 622, 0, 0, 220, 349, 349, 349, 349, 349, 349, 311, 349, 349, 349, 311, 0, 233, 0, 349, 349, 349, 349, 349, 349, 311, 349, 349, 311, 0, 311, 311, 440, 392, 349, 349, 0, 0, 233, 233, 233, 440, 349, 349, 392, 311, 311, 0, 0, 349, 349, 349, 349, 349, 349, 311, 349, 349, 311, 0, 0, 233, 349, 349, 349, 349, 349, 349, 311, 349, 349, 0  }; //call me what you like notes

const int melody3[] = {277, 277, 277, 277, 311, 311, 311, 311, 311, 311, 208, 208, 208, 208, 175, 175, 175, 175, 175, 175, 277, 277, 277, 277, 311, 311, 311, 311, 311, 311, 208, 208, 208, 208, 175, 175, 175, 175, 175, 175, 0, 0, 523, 523, 523, 554, 466, 466, 0, 466, 466, 415, 466, 466, 466, 523, 277, 349, 349, 523, 523, 523, 523, 523, 523, 554, 466, 466, 0, 466, 466, 311, 466, 466, 466, 523, 415, 392, 0, 0}; //viva la vida notes

const int melodyDurations1[] = {125, 125, 125, 125, 250, 125, 125, 250, 250, 250, 125, 125, 250, 250, 250, 125, 125, 125, 125, 125, 125, 250, 250, 125, 125, 125, 125, 250, 125, 125, 250, 250, 250, 125, 125, 125, 125, 125, 125, 250, 250, 1000, 125, 125, 125, 125, 250, 125, 125, 250, 250, 250, 125, 125, 250, 250, 250, 125, 125, 125, 125, 125, 125, 250, 250, 125, 125, 125, 125, 250, 125, 125, 250, 250, 250, 125, 125, 125, 125, 125, 125, 250, 250, 1000, 0};  //blackboxwarrior durations

const int melodyDurations2[] = {500, 500, 500, 500, 1000, 750, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 750, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 750, 250, 500, 500, 250, 250, 1000, 1000, 250, 250, 250, 250, 500, 250, 500, 500, 250, 500, 500, 1000, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 500, 750, 250, 250, 250, 250, 250, 250, 250, 250, 250, 500, 0};  //call me what you like durations

const int melodyDurations3[] = {500, 500, 500, 250, 250, 250, 500, 250, 500, 500, 500, 500, 500, 250, 250, 250, 500, 250, 500, 500, 500, 500, 500, 250, 250, 250, 500, 250, 500, 500, 500, 500, 500, 250, 250, 250, 500, 250, 500, 500, 250, 500, 250, 500, 500, 250, 250, 1000, 250, 250, 250, 250, 500, 250, 250, 500, 250, 250, 250, 250, 250, 250, 250, 250, 500, 250, 250, 1000, 250, 250, 250, 250, 500, 500, 500, 250, 250, 250, 250, 0}; //viva la vida durations

int currentTrack = 1;  // initial track number
int mute=1;

void setup() {
  pinMode(BUTTON_MUTE, INPUT);
  pinMode(BUTTON_SKIP, INPUT);
  pinMode(BUTTON_REWIND, INPUT);
  pinMode(BUTTON_RESET, INPUT);

  lcd.init();  //initialize the lcd
  lcd.backlight();  //open the backlight 

  pinMode(MELODY_PIN, OUTPUT);
}


void loop(){

if (currentTrack==0){
currentTrack=3;}

if (currentTrack==1){
songOneTime();}

if (currentTrack==2){
songTwoTime();}

if (currentTrack==3){
songThreeTime();}

if (currentTrack==4){
currentTrack=1;}

}



void buttoncheck(){

//Mute button

if(digitalRead(BUTTON_MUTE)==HIGH){
    noTone(MELODY_PIN);
    lcd.setCursor(0, 1);
    lcd.print("Status: Muted     ");}
else{
  mute=0;
    lcd.setCursor(0, 1);
lcd.print("Status: Playing");}


updateLCD();

//Skip button

if(digitalRead(BUTTON_SKIP)==HIGH)
  currentTrack=currentTrack+1;

if(currentTrack>3)
  currentTrack=1;

updateLCD();

//Rewind button

if(digitalRead(BUTTON_REWIND)==HIGH)
  currentTrack=currentTrack-1;

if(currentTrack<0)
  currentTrack=3;

updateLCD();

//Reset button

if(digitalRead(BUTTON_RESET)==HIGH)
  currentTrack=1;

updateLCD();
}

void updateLCD() {

  lcd.setCursor(0, 0);

  String songName;

  if(currentTrack==1){
      songName = "BlackBoxWarrior       ";}
  else if(currentTrack==2)
      songName = "Call Me What You Like      ";
  else if(currentTrack==3)
      songName = "Viva La Vida         ";

    lcd.setCursor(0, 0);
    lcd.print(songName);
}

void songOneTime(){
   
  for (int thisNote = 0; thisNote < sizeof(melody1) / sizeof(int); thisNote++)
  {    
    tone(3, melody1[thisNote]); 
    buttoncheck();
    delay(melodyDurations1[thisNote] * .3);
    noTone(MELODY_PIN);
    if (currentTrack!=1){
      break;
      }

  }
  if (currentTrack==1){
    currentTrack=2;}
   
}

void songTwoTime(){
  
  for (int thisNote = 0; thisNote < sizeof(melody2) / sizeof(int); thisNote++)
  {    
    tone(3, melody2[thisNote]);    
    buttoncheck();
    delay(melodyDurations2[thisNote] * .4);
    noTone(MELODY_PIN);
    if (currentTrack!=2){
      break;
      }

  }
  if (currentTrack==2){
    currentTrack=3;}
}

void songThreeTime(){
  
  for (int thisNote = 0; thisNote < sizeof(melody3) / sizeof(int); thisNote++)
  {    
    tone(3, melody3[thisNote]);   
    buttoncheck();
    delay(melodyDurations3[thisNote] * .5);
    noTone(MELODY_PIN);
    if (currentTrack!=3){
      break;
      }

  }
  if (currentTrack==3){
    currentTrack=1;}
}