// Code from: https://www.youtube.com/watch?v=YoJh29inV8E
// LED tilt bar code

const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // LED pins
//sound level code
#define ENVELOPE_PIN A0 // SparkFun Sound Detector Envelope Output
#define LED_START_PIN 2 // Starting pin for LED bar graph
#define LED_COUNT 10 // Number of LEDs

unsigned long startTime;
const unsigned long interval = 6000; // 6 seconds per LED segment

void setup() { 
  //Serial.begin(9600);
  pinMode(ENVELOPE_PIN, INPUT);
  // Initialize each LED pin
  for (int i = 0; i < LED_COUNT; i++) {
    pinMode(LED_START_PIN + i, OUTPUT);
    digitalWrite(LED_START_PIN + i, LOW);
  }
}

void loop() {
  led1();
  //led2();
  //led3();
 //led4();

  //OneMinute_Timer() ;  
}

void led1() {
  // Turn LEDs ON
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, HIGH);
    delay(50);
  }
  // Turn LEDs OFF
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, LOW);
    delay(25);
  }
}

void led2() {
  // Turn LEDs ON
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, HIGH);
    delay(35);
  }
  // // Turn LEDs OFF
  for (int i = LED_COUNT - 1; i >= 0; i--) {
    digitalWrite(LED_START_PIN + i, LOW);
    delay(15);
  }
}

void led3() {
  // Turn on LEDs up to the mapped level
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, HIGH);
    delay(50);
    digitalWrite(LED_START_PIN + i, LOW);
    delay(50);
  }
}

void led4() {
  // Turn on LEDs up to the mapped level
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, HIGH);
    delay(50);
    digitalWrite(LED_START_PIN + i, LOW);
    delay(50);
  }
  for (int i = LED_COUNT - 1; i >= 0; i--) {
    digitalWrite(LED_START_PIN + i, HIGH);
    delay(50);
    digitalWrite(LED_START_PIN + i, LOW);
    delay(50);
  }
}

void OneMinute_Timer() {
  unsigned long currentTime = millis();
  unsigned long elapsedTime = currentTime - startTime;
  // Calculate the current LED index (0 to 9) based on elapsed time
  int currentLED = elapsedTime / interval;
  // Turn on LEDs up to the current index, inclusive
  for (int i = 0; i < LED_COUNT; i++) {
    digitalWrite(LED_START_PIN + i, i <= currentLED ? HIGH : LOW);
  }
  // Reset the timer and LEDs after 1 minute (60 seconds)
  if (currentLED >= LED_COUNT) {
    startTime = millis(); // Reset the timer
    for (int i = 0; i < LED_COUNT; i++) {
      digitalWrite(LED_START_PIN + i, LOW);
    }
  }
  delay(100); // Short delay for responsiveness
}