/*
  Created by: Electronic Projects
  Youtube channel: https://www.youtube.com/channel/UCAE_BN64jkkjfGJLSs21upg
  Video: https://www.youtube.com/watch?v=NuBxGNQD6xU
  Arduino Sketch: https://drive.google.com/file/d/16Fp7YZaMxFzJ9D5EKZ0g1XoV_dDMVuk4/view
  Date: Jan-03-18
*/

#define LED1 3
#define LED2 4
#define LED3 5
#define LED4 6
#define LED5 7
#define LED6 8
#define LED7 9
#define LED8 10
#define LED9 11
#define LED10 12
#define buzzer 2
#define numLeds 10

int LED[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int red[] = {8, 9, 10, 11, 12};
int blue[] = {7, 6, 5, 4, 3};

unsigned int i, j, n, k;
const int x = 400, y = 1000, t1 = 7, t2 = 500;  // t1 = delay in milliseconds, t2 = delay in microseconds

void setup() {
  for (int i = 0; i < numLeds; i++) {
    pinMode(LED[i], OUTPUT);
  }


  Serial.begin(9600);
}


void loop() {

  k = (y - x) / 10;
  //Serial.println(k);

  sound0_1();
  delay(300);
  sound1_1();
  delay(300);
  sound1_2();
  delay(300);
  sound1_3();
  delay(300);
  sound1_4();
  delay(300);
  sound1_5();
  delay(300);

  sound2_1();
  sound2_1();
  delay(300);
  sound2_2();
  sound2_2();
  delay(300);
  sound2_3();
  sound2_3();
  delay(300);
  sound2_4();
  sound2_4();
  delay(300);
  sound2_5();
  sound2_5();
  delay(300);
  sound2_6();
  sound2_6();
  delay(300);

  //  sound1_5();   // test

  noTone(buzzer);
  delay(1000);
}

void sound0_1() {
  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y - 150; i += 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        digitalWrite(blue[n], HIGH);
        digitalWrite(red[n], HIGH);
        n++;
      }
      delay(t1);
    }
    allLEDoff();
    noTone(buzzer);
    delay(300);
  }
  noTone(buzzer);
  delay(10);
}

void sound1_1() {
  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        digitalWrite(LED[n], HIGH);
        n++;
      }
      delay(t1);
    }
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        n--;
        digitalWrite(LED[n], LOW);
      }
      delay(t1);
    }
  }
  allLEDoff();
  noTone(buzzer);
  delay(10);
}

void sound1_2() {
  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0 && n < 5) {
        digitalWrite(blue[n], HIGH);
        digitalWrite(red[n], HIGH);
        n++;
      }
      delay(t1);
    }
    n = 5;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0 && n > 0) {
        n--;
        digitalWrite(blue[n], LOW);
        digitalWrite(red[n], LOW);

      }
      delay(t1);
    }
    allLEDoff();
  }
  noTone(buzzer);
  delay(10);
}

void sound1_3() {

  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i == 400 || i == 550) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED10, HIGH);
      }
      else if (i == 474 || i == 624) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED10, LOW);
      }
      else if (i == 700 || i == 850) {
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      }
      else if (i == 774 || i == 924) {
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      }
      delay(t1);
    }
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i == 1000 || i == 850) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED10, HIGH);
      }
      else if (i == 924 || i == 774) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED10, LOW);
      }
      else if (i == 700 || i == 550) {
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      }
      else if (i == 624 || i == 474) {
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      }
      delay(t1);
    }
  }
  allLEDoff();
  noTone(buzzer);
  delay(10);
}

void sound1_4() {
  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        digitalWrite(LED[n - 1], LOW);
        digitalWrite(LED[n], HIGH);
        n++;
      }
      delay(t1);
    }
    n = 10;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        n--;
        digitalWrite(LED[n + 1], LOW);
        digitalWrite(LED[n], HIGH);
      }
      delay(t1);
    }
  }
  allLEDoff();
  noTone(buzzer);
  delay(10);
}

void sound1_5() {
  allLEDon();
  for (j = 1; j <= 3; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        digitalWrite(LED[n - 1], HIGH);
        digitalWrite(LED[n], LOW);
        n++;
      }
      delay(t1);
    }
    n = 10;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        n--;
        digitalWrite(LED[n + 1], HIGH);
        digitalWrite(LED[n], LOW);
      }
      delay(t1);
    }
  }
  allLEDoff();
  noTone(buzzer);
  delay(10);
}

void sound2_1() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        digitalWrite(LED[n], HIGH);
        n++;
      }
      delayMicroseconds(t2);
    }
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        n--;
        digitalWrite(LED[n], LOW);

      }
      delayMicroseconds(t2);
    }
  }
  allLEDoff();
  noTone(buzzer);
  //delay(10);
}

void sound2_2() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0 && n < 5) {
        digitalWrite(blue[n], HIGH);
        digitalWrite(red[n], HIGH);
        n++;
      }
      delayMicroseconds(t2);
    }
    n = 5;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0 && n > 0) {
        n--;
        digitalWrite(blue[n], LOW);
        digitalWrite(red[n], LOW);

      }
      delayMicroseconds(t2);
    }
  }
  allLEDoff();
  noTone(buzzer);
  //delay(10);
}

void sound2_3() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % k == 0 ) {
        digitalWrite(LED[n - 1], LOW);
        digitalWrite(LED[n], HIGH);
        n++;
      }
      delayMicroseconds(t2);
    }
    n = 10;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % k == 0) {
        n--;
        digitalWrite(LED[n + 1], LOW);
        digitalWrite(LED[n], HIGH);
      }
      delayMicroseconds(t2);
    }
    allLEDoff();
  }
  noTone(buzzer);
  //delay(10);
}

void sound2_4() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    allLEDon();
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % x == 0 && n == 0) {
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
        n++;
      }
      delayMicroseconds(t2);
    }
    n = 1;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % y == 0 && n == 1) {
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
        n = 0;
      }
      delayMicroseconds(t2);
    }
    allLEDoff();
  }
  noTone(buzzer);
  //delay(10);
}

void sound2_5() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    allLEDon();
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % x == 0 && n == 0) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
        digitalWrite(LED10, LOW);
        n++;
      }
      delayMicroseconds(t2);
    }
    // n = 1;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % y == 0 && n == 1) {
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
        digitalWrite(LED10, HIGH);
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        //n=0;
      }
      delayMicroseconds(t2);
    }
    allLEDoff();
  }
  noTone(buzzer);
  //delay(10);
}

void sound2_6() {
  for (j = 1; j <= 10; j++) {
    n = 0;
    allLEDon();
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    for (i = x; i <= y; i += 2) {

      tone(buzzer, i);
      if (i % x == 0 && n == 0) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED10, HIGH);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
        n++;
      }
      delayMicroseconds(t2);
    }
    // n = 1;
    for (i = y; i >= x; i -= 2) {

      tone(buzzer, i);
      if (i % y == 0 && n == 1) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED10, LOW);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
        n = 0;
      }
      delayMicroseconds(t2);
    }
    allLEDoff();
  }
  noTone(buzzer);
  //delay(10);
}

void allLEDoff() {
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(LED[i], LOW);
  }
}

void allLEDon() {
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(LED[i], HIGH);
  }
}