#include <Servo.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"

#define SD_ChipSelectPin 10

#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523

class Melody {
private:
  int tempo;
  int buzzer;
  int *notesArray;
  int notesCount;

public:
  Melody(int *melodyArray, int arraySize, int tempo, int buzzerPin) {
    this->notesArray = melodyArray;
    this->notesCount = arraySize / 2;
    this->tempo = tempo;
    this->buzzer = buzzerPin;
  }

  void play() {
    int wholenote = (60000 * 4) / tempo;
    int divider = 0, noteDuration = 0;

    for (int thisNote = 0; thisNote < notesCount * 2; thisNote += 2) {
      divider = notesArray[thisNote + 1];
      if (divider > 0) {
        noteDuration = (wholenote) / divider;
      } else if (divider < 0) {
        noteDuration = (wholenote) / abs(divider);
        noteDuration *= 1.5;
      }
      tone(buzzer, notesArray[thisNote], noteDuration * 0.9);
      delay(noteDuration);
      noTone(buzzer);
    }
  }
};

// Define tempo
int tempo = 140;

// Notes of the Happy Birthday melody followed by durations
int happybdy[] = {
  NOTE_C4, 4, NOTE_C4, 8, NOTE_D4, -4, NOTE_C4, -4, NOTE_F4, -4,
  NOTE_E4, -2, NOTE_C4, 4, NOTE_C4, 8, NOTE_D4, -4, NOTE_C4, -4,
  NOTE_G4, -4, NOTE_F4, -2, NOTE_C4, 4, NOTE_C4, 8, NOTE_C5, -4,
  NOTE_A4, -4, NOTE_F4, -4, NOTE_E4, -4, NOTE_D4, -4, NOTE_AS4, 4,
  NOTE_AS4, 8, NOTE_A4, -4, NOTE_F4, -4, NOTE_G4, -4, NOTE_F4, -2
};

// Create an instance of Melody class with Happy Birthday song data
Melody happyBirthdayMelody(happybdy, sizeof(happybdy) / sizeof(int), tempo, 9);

Servo handServo;
Servo boxServo;
int switchAnimation = 0, action = 1, vot, randNumber = 0;
// Servo Pins
const int doorSwitch = 2;
const int servoHandpin = 5;
const int ServoBoxpin = 6;

TMRpcm tmrpcm;
void setup() {
  Serial.begin(9600);
  tmrpcm.speakerPin = 9;
  tmrpcm.setVolume(5);

  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD card initialization failed!");
    return;
  }
 // Serial.println("SD card initialized successfully.");

handServo.attach(servoHandpin);
boxServo.attach(ServoBoxpin);
handServo.write(180);  //
boxServo.write(70);    //

}


void loop() {
  switchAnimation = digitalRead(doorSwitch);
  // First Animation

  if (switchAnimation == LOW) {
    if (action == 1) {
      Serial.println("Action 1");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(13);
      }
      action1();
      delay(500);
      happyBirthdayMelody.play();

      for (int i = 180; i >= 35; i--) {
        handServo.write(i);
        delay(20);
      }
      delay(1000);
      handServo.write(26);
      delay(200);
      for (int i = 26; i <= 180; i++) {
        handServo.write(i);
        delay(20);
      }
      for (int i = 110; i >= 70; i--) {
        boxServo.write(i);
        delay(20);
      }
      action++;
    }
    // Second Animation
    else if (action == 2) {
      Serial.println("Action 2");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(550);
      handServo.write(26);
      delay(550);
      handServo.write(180);
      delay(550);
      boxServo.write(70);
      delay(1500);
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(3000);
      boxServo.write(70);
      action++;
    }

    //Third Animation
    else if (action == 3) {
      Serial.println("Action 3");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(1000);
      boxServo.write(70);
      delay(2000);
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      handServo.write(26);
      delay(550);
      handServo.write(180);
      delay(500);
      boxServo.write(70);
      action++;
    }

    //Fourth Animation
    else if (action == 4) {
      Serial.println("Action 4");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(20);
      }
      for (int i = 180; i >= 35; i--) {
        handServo.write(i);
        delay(20);
      }
      delay(2000);
      handServo.write(26);
      delay(200);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    //Fifth Animation

    else if (action == 5) {
      Serial.println("Action 5");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(2000);
      handServo.write(65);
      delay(550);
      handServo.write(40);
      delay(200);
      handServo.write(65);
      delay(200);
      handServo.write(40);
      delay(200);
      handServo.write(65);
      delay(200);
      handServo.write(40);
      delay(200);
      handServo.write(65);
      delay(200);
      handServo.write(40);
      delay(200);
      handServo.write(65);
      delay(200);
      handServo.write(40);
      delay(200);
      handServo.write(65);
      delay(2000);
      handServo.write(26);
      delay(400);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    //Sixth Animation

    else if (action == 6) {
      Serial.println("Action 6");

      for (int i = 70; i < 110; i++) {
        boxServo.write(i);
        delay(20);
      }
      delay(1000);
      for (int i = 0; i < 12; i++) {
        for (int j = 70; j <= 110; j++) {
          boxServo.write(j);
          delay(6);
        }
        delay(200);
      }
      for (int i = 180; i >= 35; i--) {
        handServo.write(i);
        delay(30);
      }
      handServo.write(26);
      delay(400);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      delay(1500);
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(3000);
      boxServo.write(80);
      action++;
    }

    //Seventh Animation

    else if (action == 7) {
      Serial.println("Action 7");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(400);
      handServo.write(26);
      delay(1000);
      for (int i = 0; i < 6; i++) {
        for (int j = 70; j <= 110; j++) {
          boxServo.write(j);
          delay(6);
        }
        delay(200);
      }
      delay(500);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    // Eight Animation

    else if (action == 8) {
      Serial.println("Action 8");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      handServo.write(26);
      delay(2000);
      for (int i = 0; i < 7; i++) {
        handServo.write(75);
        delay(100);
        handServo.write(40);
        delay(100);
      }
      delay(500);
      handServo.write(180);
      delay(1000);
      boxServo.write(70);
      action++;
    }

    //Nineth Animation

    else if (action == 9) {
      Serial.println("Action 9");
      for (int i = 70; i <= 105; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(2000);
      boxServo.write(110);
      for (int i = 180; i >= 35; i--) {
        handServo.write(i);
        delay(40);
      }
      delay(500);
      handServo.write(26);
      delay(200);
      for (int i = 26; i <= 180; i++) {
        handServo.write(i);
        delay(40);
      }
      boxServo.write(70);
      delay(2000);
      boxServo.write(100);
      delay(3000);
      boxServo.write(70);
      action++;
    }

    // Tenth Animation

    else if (action == 10) {
      Serial.println("Action 10");
      for (int i = 70; i <= 105; i++) {
        boxServo.write(i);
        delay(100);
      }
      delay(1000);
      for (int i = 0; i < 10; i++) {
        for (int j = 70; j <= 100; j++) {
          boxServo.write(j);
          delay(6);
        }
        delay(100);
      }
      boxServo.write(110);
      delay(100);
      handServo.write(26);
      delay(650);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    // Eleventh Animation

    else if (action == 11) {
      Serial.println("Action 11");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      handServo.write(26);
      delay(2000);
      for (int i = 0; i < 3; i++) {
        handServo.write(65);
        delay(200);
        handServo.write(40);
        delay(200);
      }
      delay(1500);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      delay(1500);
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      handServo.write(26);
      delay(3000);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    // Twelveth Animation

    else if (action == 12) {
      Serial.println("Action 12");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(50);
      }
      delay(1000);
      for (int i = 180; i >= 26; i--) {
        handServo.write(i);
        delay(50);
      }
      delay(2000);
      handServo.write(26);
      delay(200);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action++;
    }

    //Thirteenth Animation

    else if (action == 13) {
      Serial.println("Action 13");
      for (int i = 70; i <= 110; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(2000);
      for (int i = 180; i >= 60; i--) {
        handServo.write(i);
        delay(40);
      }
      for (int i = 0; i < 3; i++) {
        handServo.write(60);
        delay(200);
        handServo.write(40);
        delay(800);
      }
      handServo.write(26);
      delay(2000);
      handServo.write(180);
      delay(500);
      boxServo.write(70);
      action++;
    }

    //Fourteenth Animation

    else if (action == 14) {
      Serial.println("Action 14");
      for (int i = 70; i <= 105; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(4000);
      boxServo.write(110);
      handServo.write(26);
      delay(650);
      handServo.write(180);
      delay(500);
      boxServo.write(100);
      delay(4000);
      boxServo.write(70);
      action++;
    }

    else if (action == 15) {
      Serial.println("Action 15");
      for (int i = 70; i <= 105; i++) {
        boxServo.write(i);
        delay(6);
      }
      delay(1000);
      for (int i = 0; i < 3; i++) {
        for (int j = 70; j <= 105; j++) {
          boxServo.write(j);
          delay(50);
        }
        for (int j = 105; j >= 70; j--) {
          boxServo.write(j);
          delay(50);
        }
      }
      for (int j = 70; j <= 110; j++) {
        boxServo.write(j);
        delay(50);
      }
      for (int i = 180; i >= 35; i--) {
        handServo.write(i);
        delay(40);
      }
      delay(1000);
      handServo.write(26);
      delay(400);
      handServo.write(180);
      delay(400);
      boxServo.write(70);
      action = 1;
    }
  }
}

void action1() {
  tmrpcm.play("0001.wav");
  while(tmrpcm.isPlaying()){

  }
  tmrpcm.disable();
  
}
$abcdeabcde151015202530354045505560fghijfghij