//https://www.youtube.com/watch?v=nyIhNKPQ_8k
//https://www.youtube.com/watch?v=_YlgMv0YoSk  //random code

#define t   30
#define t1  20
#define t2  100
#define t3  50
//#define numLeds 18

int leds[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int outLeds[] =  {2, 3, 4, 11, 12, 13};
int innerLeds[] =  {5, 6, 7, 8, 9, 10};

int numLeds = sizeof(leds) / sizeof(int);

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

void loop() {
  for (int i = 0; i < 2; i++) {
    // check if the button is pressed (LOW due to pull-up resistor)
    int buttonValue = i; //digitalRead(A5);
    if (buttonValue == LOW) {
      // Generate a random LED pattern
      randomPattern();
      delay(500); // Debounce delay
    }
    else {
      loop1();
    }
  }
}

void loop1() {
  animation_14(20);
  testa();
  testb();
  //testc();
  testd();
  teste();
  effect_10();
}

void randomPattern() {
  for (int x = 0; x < 30; x++) {
    for (int i = 0; i < numLeds; i++) {
      int ledState = random(2);
      digitalWrite(leds[i], ledState);
    }
    delay(200);
    clear();
  }
}

void testa() {
  for (int p = 1; p < 6; p++) {
    for (int x = 0; x < 2; x++) {
      for (int i = 0; i < numLeds; i++) {
        digitalWrite(leds[i], i % 2 == x);
      }
      delay(200);
    }
  }
  clear();
}

void testb() {
  for (int x = 1; x < 3; x++) {
    for (int i = 0; i < numLeds; i++) {
      digitalWrite(leds[i], HIGH);
      if (i > 0)  digitalWrite(leds[i - 1], LOW);
      delay(70);
    }
    digitalWrite(leds[numLeds - 1], HIGH);
    delay(100);
    digitalWrite(leds[numLeds - 1], LOW);
    delay(100);

    for (int i = numLeds - 1; i >= 0; i--) {
      digitalWrite(leds[i], HIGH);
      if (i < numLeds)  digitalWrite(leds[i + 1], LOW);
      delay(70);
      //digitalWrite(leds[i], LOW);

    }
    digitalWrite(leds[0], HIGH);
    delay(100);
    digitalWrite(leds[0], LOW);
    delay(100);
  }
}

void testc() {
  for (int i = 1; i < 3; i++) {
    for (int i = 0; i < numLeds; i++) {
      digitalWrite(leds[i], HIGH);
      delay(100);
      digitalWrite(leds[i], LOW);
      delay(100);
    }
  }
}

void testd() {
  for (int i = 1; i < 4; i++) {
    for (int i = 0; i < numLeds / 2; i++) {
      digitalWrite(leds[i], HIGH);
      digitalWrite(leds[numLeds - 1 - i], HIGH);
      delay(100);
    }
    for (int i = 0; i < numLeds / 2; i++) {
      digitalWrite(leds[i], LOW);
      digitalWrite(leds[numLeds - 1 - i], LOW);
      delay(100);
    }
  }
}

void teste() {
  for (int i = 1; i < 10; i++) {
    for (int i = 0; i < numLeds; i++) {
      digitalWrite(leds[i], HIGH);
    }
    delay(300);
    for (int i = 0; i < numLeds; i++) {
      digitalWrite(leds[i], LOW);
    }
    delay(200);
  }
}


///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

void test4(const int dTime) {
  //clear();
  for (int i = numLeds - 1; i >= 0; i--) {
    for (int k = i; k >= 0; k--) {
      digitalWrite(leds[k], HIGH);
      delay(dTime);
      digitalWrite(leds[k], LOW);
      delay(dTime);
    }
    digitalWrite(leds[i], HIGH);
    delay(dTime);
  }
}

void test3(const int dTime) {
  //clear();
  for (int i = numLeds - 1; i >= 0; i--) {
    for (int k = i; k >= 0; k--) {
      digitalWrite(leds[k], HIGH);
      delay(dTime);
      digitalWrite(leds[k], LOW);
      delay(dTime);
    }
    digitalWrite(leds[i], HIGH);
    delay(dTime);
  }
  for (int i = 0; i < 20; i++) {
    allLedsOn(0, numLeds);
    delay(30);
    allLedsOff(0, numLeds);
    delay(30);
  }
}

void effect_10() {

  for (int i = numLeds - 1; i >= 0; i--) {
    for (int k = 0; k < i; k++) {
      digitalWrite(leds[k], HIGH);
      delay(20);
      digitalWrite(leds[k], LOW);
      delay(10);
    }
    digitalWrite(leds[i], HIGH);
    delay(20);
  }

}


void test2(const int dTime) {
  for (int i = numLeds - 1; i >= 0; i--) {
    digitalWrite(leds[numLeds - i], HIGH);
    delay(10);
    for (int k = numLeds - 1; k >= 0; k--) {
      digitalWrite(leds[k - i], HIGH);
      delay(10);
      digitalWrite(leds[k - i], LOW);
      delay(10);
    }
  }
}

void test1(const int dTime) {
  for (int i = numLeds - 1; i >= 0; i--) {
    if (i >= 0) {
      digitalWrite(leds[i], HIGH);
      delay(30);
      digitalWrite(leds[i], LOW);
      delay(30);
    }

  }
}

/////////////////////////////////////animation_1///////////////////////////////////////
void animation_1() {
  clear();
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(leds[i], HIGH);
    delay(50);
    digitalWrite(leds[i], LOW);
  }
  for (int i = numLeds - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    delay(50);
    digitalWrite(leds[i], LOW);
  }
}

/////////////////////////////////////animation_2///////////////////////////////////////
void animation_2() {
  int count = 13;

  for (int i = 0; i < numLeds; i++) {
    clear();
    digitalWrite(leds[i], HIGH);      // chaser 1
    digitalWrite(count, HIGH); // chaser 2
    count--;

    if (count != 7) {
      delay(70);
    }
  }

  for (int i = numLeds - 1; i >= 0; i--) {
    clear();
    digitalWrite(leds[i], HIGH);
    digitalWrite(count, HIGH);
    count++;

    if (count != 8) {
      delay(70);
    }
  }
}

/////////////////////////////////////animation_3///////////////////////////////////////
void animation_3() {
  clear();
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(leds[i], HIGH);
    delay(50);
  }
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(leds[i], LOW);
    delay(50);
  }
  for (int i = 0; i < 8; i++) {
    digitalWrite (13, HIGH);
    delay(20);
    digitalWrite (13, LOW);
    delay(20);
  }


  for (int i = numLeds - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    delay(50);
  }

  for (int i = numLeds - 1; i >= 0; i--) {
    digitalWrite(leds[i], LOW);
    delay(50);
  }
  for (int i = 0; i < 8; i++) {
    digitalWrite (2, HIGH);
    delay(20);
    digitalWrite (2, LOW);
    delay(20);
  }
}

/////////////////////////////////////animation_4///////////////////////////////////////
void animation_4(const int dTime) {
  for (int i = 0; i < numLeds; i = i + 2) {
    digitalWrite(leds[i], HIGH);
  }
  delay(dTime);
  for (int i = 0; i < numLeds; i = i + 2) {
    digitalWrite(leds[i], LOW);
  }
  delay(dTime);
  for (int i = 1; i < numLeds; i = i + 2) {
    digitalWrite(leds[i], HIGH);
  }
  delay(dTime);
  for (int i = 1; i < numLeds; i = i + 2) {
    digitalWrite(leds[i], LOW);
  }
  delay(dTime);
}

/////////////////////////////////////animation_5///////////////////////////////////////
void animation_5() {
  for (int i = 0; i < 6; i++) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[numLeds - i - 1], HIGH);
    delay(70);
  }

  for (int i = 0; i < 6; i++) {
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[numLeds - i - 1], LOW);
    delay(70);
  }

  for (int k = 0; k < 5; k++) {
    allLedsOn(5, 7);
    delay(110);
    allLedsOff(5, 7);
    delay(110);
  }
}

/////////////////////////////////////animation_6///////////////////////////////////////
void animation_6() {
  // nothing special lol.
}

/////////////////////////////////////animation_7///////////////////////////////////////
void animation_7() {
  // nothing special lol.
}

/////////////////////////////////////animation_8///////////////////////////////////////
void animation_8() {
  for (int i = 0; i < 3; i++) {
    allLedsOn(0, 6);
    delay(70);
    allLedsOff(0, 6);
    delay(70);
  }
  delay(500);
  for (int i = 0; i < 3; i++) {
    allLedsOn(6, 12);
    delay(70);
    allLedsOff(6, 12);
    delay(70);
  }
  delay(500);
}

/////////////////////////////////////animation_9///////////////////////////////////////
void animation_9() {
  allLedsOn(0, 6);
  delay(70);
  allLedsOff(0, 6);
  delay(70);

  delay(500);

  allLedsOn(6, 12);
  delay(70);
  allLedsOff(6, 12);
  delay(70);

  delay(500);
}

////////////////////////////////////animation_10///////////////////////////////////////
void animation_10() {
  allLedsOn(0, 6);
  delay (100);
  allLedsOff(0, 6);
  delay (100);
  allLedsOn(6, numLeds);
  delay (100);
  allLedsOff(6, numLeds);
  delay (100);

}

////////////////////////////////////animation_11///////////////////////////////////////
void animation_11() {
  allLedsOn(0, numLeds);
  delay(500);
  allLedsOff(0, numLeds);
  delay(500);
}

////////////////////////////////////animation_12///////////////////////////////////////
void animation_12() {
  for (int p = 0; p < 6; p++) {
    for (int i = 0; i < 6; i++) {
      digitalWrite(outLeds[i], HIGH);
    }
    delay(40);
    for (int i = 0; i < 6; i++) {
      digitalWrite(outLeds[i], LOW);
    }
    delay(40);
  }
  delay(400);
  for (int p = 0; p < 6; p++) {
    for (int i = 0; i < 6; i++) {
      digitalWrite(innerLeds[i], HIGH);
    }
    delay(40);
    for (int i = 0; i < 6; i++) {
      digitalWrite(innerLeds[i], LOW);
    }
    delay(40);
  }
  delay(400);
}

////////////////////////////////////animation_13///////////////////////////////////////
void animation_13() {
  for (int i = 0; i < 8; i++) {
    digitalWrite(leds[i], HIGH);
    delay(15);
  }
  delay(150);
  for (int i = 8; i >= 0; i--) {
    digitalWrite(leds[i], LOW);
    delay(40);
  }
  for (int i = numLeds - 1; i >= 4; i--) {
    digitalWrite(leds[i], HIGH);
    delay(15);
  }
  delay(150);
  for (int i = 4; i < numLeds; i++) {
    digitalWrite(leds[i], LOW);
    delay(40);
  }
}

////////////////////////////////////animation_14///////////////////////////////////////
void animation_14(const int dTime) {
  for (int i = numLeds - 1; i >= 0; i--) {
    for (int k = i; k >= 0; k--) {
      digitalWrite(leds[k], HIGH);
      delay(dTime);
      digitalWrite(leds[k], LOW);
      delay(dTime);
    }
    digitalWrite(leds[i], HIGH);
    delay(dTime);
  }
  for (int i = 0; i < 10; i++) {
    allLedsOn(0, numLeds);
    delay(50);
    allLedsOff(0, numLeds);
    delay(100);
  }
}

///////////////////////////////////////////////////////////////////////////
void clear() {
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(leds[i], LOW);
  }
}

///////////////////////////////////////////////////////////////////////////
void allLedsOn(int startLed, int endLed) {
  for (int i = startLed; i < endLed; i++) {
    digitalWrite(leds[i], HIGH);
  }
}

///////////////////////////////////////////////////////////////////////////
void allLedsOff(int startLed, int endLed) {
  for (int i = startLed; i < endLed; i++) {
    digitalWrite(leds[i], LOW);
  }
}