// https://github.com/embeddedlab786/17Channel_LED_Chaser/tree/main


#define potentiometer  A7
//#define debug
int read_ADC, delay_time;


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

// https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
// sizeof(); https://forum.arduino.cc/t/how-do-you-check-the-length-of-an-array/88325/7
int total_leds = sizeof(leds) / sizeof(int); // enter the number of LEDs you want to use here

void setup() {// put your setup code here, to run once
  Serial.begin(9600);// initialize serial communication at 9600 bits per second:
#ifdef debug
  Serial.println("Debug Enabled");
#else
  Serial.println("Debug Disabled");
#endif

  for (int i = 0; i < total_leds; i++) {
    pinMode(leds[i], OUTPUT); // declare LEDs as output
  }
  //delay(200);
  ledsOn();
  delay(500); // Waiting for a while
  ledsOff();
  delay(500);
}

//this function for texting only
void loop1() {
  effect_1();
}

void loop() {

  for (int i = 0; i < 3; i++) effect_1();

  for (int i = 0; i < 3; i++) effect_2();

  for (int i = 0; i < 20; i++) effect_3();

  for (int i = 0; i < 3; i++) effect_4();

  for (int i = 0; i < 3; i++) effect_5();

  for (int i = 0; i < 3; i++) effect_6();

  for (int i = 0; i < 3; i++) effect_7();

  for (int i = 0; i < 3; i++) effect_8();

  for (int i = 0; i < 10; i++) effect_9();

  for (int i = 0; i < 10; i++) effect_10();

  for (int i = 0; i < 5; i++) effect_11();

  for (int i = 0; i < 5; i++) effect_12();

  ledsOff();
  //get_delay();
}

//TURN ON ALL LEDs
void ledsOn() {
  for (int i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], HIGH);
  }
}

//TURN OFF ALL LEDs
void ledsOff() {
  for (int i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], LOW);
  }
}

//LEFT TO RIGH FILL UNFILL - RIGHT TO LEFT FILL UNFIL
void effect_1() {
  String text = "Effect 1 ";
  int i = 0;

  for (i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
  }

  for (i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], LOW);
    get_delay(text);
  }

  for (i = total_leds - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
  }

  for (i = total_leds - 1; i >= 0; i--) {
    digitalWrite(leds[i], LOW);
    get_delay(text);
  }
}

//SPRING
void effect_2() {
  String text = "Effect 2 ";
  int i = 0;
  for (i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
  }

  for (i = total_leds - 1; i >= 0; i--) {
    digitalWrite(leds[i], LOW);
    get_delay(text);
  }

  for (i = total_leds - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
  }

  for (i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], LOW);
    get_delay(text);
  }
}

//RUN ODD LEDs
void effect_3() {
  String text = "Effect 3 ";
  for (int i = 0; i < total_leds; i = i + 2) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[i + 1], LOW);
  }

  get_delay(text);

  for (int i = 0; i < total_leds; i = i + 2) {
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[i + 1], HIGH);
  }

  get_delay(text);
}

//RUN EVEN ODD LEDs
void effect_4() {
  String text = "Effect 4 ";
  for (int i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
    i = i + 1;
  }

  for (int i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], LOW);
    get_delay(text);
    i = i + 1;
  }

  for (int j = total_leds - 1; j >= 0; j--) {
    digitalWrite(leds[j], HIGH);
    get_delay(text);
    j = j - 1;
  }

  for (int j = total_leds - 1; j >= 0; j--) {
    digitalWrite(leds[j], LOW);
    get_delay(text);
    j = j - 1;
  }
}


//FROM LEFT AND RIGHT TO CENTER - AND VICE VERSA
void effect_5() {
  String text = "Effect 5 ";
  int i = 0;
  for (i = 0; i < total_leds / 2 ; i++) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[total_leds - 1 - i], HIGH);
    get_delay(text);
  }

  for (i = 0; i < total_leds / 2 ; i++) {
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[total_leds - 1 - i], LOW);
    get_delay(text);
  }

  get_delay(text);

  for (i = 0; i < total_leds / 2 ; i++) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[total_leds - 1 - i], HIGH);
    get_delay(text);
  }
  for (i = total_leds / 2 - 1; i >= 0 ; i--) {
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[total_leds - 1 - i], LOW);
    get_delay(text);
  }
}

//NIGHT RIDER LEFT TO RIGHT (ONE LED)
void effect_6() {
  String text = "Effect 6 ";
  for (int i = 0; i < total_leds; i++) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
  }
}

//NIGHT RIDER RIGHT TO LEFT (ONE LED)
void effect_7() {
  String text = "Effect 7 ";
  for (int i = total_leds - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
  }
}

//NIGHT RIDER BACK AND FORTH (ONE LED)
void effect_8() {
  String text = "Effect 8 ";
  for (int i = 0; i < total_leds + 1; i++) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
  }
  for (int i = total_leds - 1; i >= -1; i--) {
    digitalWrite(leds[i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
  }
}

//INSIDE TO CENTER
void effect_9() {
  String text = "Effect 9 ";
  for (int i = 0; i < total_leds / 2; i++) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[total_leds - 1 - i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[total_leds - 1 - i], LOW);
  }
}

//OUTSIDE FROM CENTER
void effect_10() {
  String text = "Effect 10 ";
  for (int i = (total_leds / 2) - 1; i >= 0; i--) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[total_leds - 1 - i], HIGH);
    get_delay(text);
    digitalWrite(leds[i], LOW);
    digitalWrite(leds[total_leds - 1 - i], LOW);
  }
}

//RUN 2 LEDs FROM LEFT TO RIGHT
void effect_11() {
  String text = "Effect 11 ";
  for (int i = 0; i < total_leds + 3; i++) {
    if (i < total_leds) {
      digitalWrite(leds[i], HIGH);
    }
    if (i > 1) {
      digitalWrite(leds[i - 2], LOW);
    }
    get_delay(text);
  }
}

//RUN 2 LEDS BACK AND FORTH
void effect_12() {
  String text = "Effect 12 ";
  for (int i = 2; i < total_leds; i++) {

    if (i == 2) {
      digitalWrite(leds[0], HIGH);
      //digitalWrite(leds[1], HIGH);
    }

    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[i - 2], LOW);
    get_delay(text);
  }

  for (int i = total_leds - 3; i > -1; i--) {
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[i + 2], LOW);
    get_delay(text);
  }
}


void get_delay(String text) {
  read_ADC = analogRead(potentiometer);
  //delay_time = read_ADC / 2.046; //max value ADC 1013/2.046 = 500
  delay_time = map(read_ADC, 0, 1023, 10, 200);
#ifdef debug
  printData(text, delay_time);
#endif
  delay(delay_time);
}

void get_delay1() {
  read_ADC = analogRead(potentiometer);
  delay_time = read_ADC / 2.046; //max value ADC 1013/2.046 = 500
  if (delay_time < 10)delay_time = 10;
  Serial.print("Speed ");
  Serial.println(delay_time);
  delay(delay_time);
}

void printData(String text, int delayTime) {
  Serial.print(text);
  Serial.print("/ ");
  Serial.print("Speed ");
  Serial.println(delayTime);
  delay(30);
}