#include <FastLED.h>

#define DATA_PIN 11
#define LED_COUNT 5
CRGB leds[LED_COUNT];

#define BUTTON_2_PIN 2
#define BUTTON_3_PIN 3
#define BUTTON_4_PIN 4
#define BUTTON_5_PIN 5
#define BUTTON_6_PIN 6
#define BUTTON_7_PIN 7

#define COOLING 55
#define SPARKING 120
#define NUM_LEDS 5
#define gReverseDirection 0

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, LED_COUNT);
  FastLED.setBrightness(64); // Adjust the brightness as needed

  pinMode(BUTTON_3_PIN, INPUT_PULLUP);
  pinMode(BUTTON_4_PIN, INPUT_PULLUP);
  pinMode(BUTTON_5_PIN, INPUT_PULLUP);
  pinMode(BUTTON_6_PIN, INPUT_PULLUP);
  pinMode(BUTTON_7_PIN, INPUT_PULLUP);
}

void loop() {
  // Check if button 2 is pressed
  if (digitalRead(BUTTON_2_PIN) == LOW) {
    turnOffLEDs();
  }
  // Check if button 3 is pressed
  if (digitalRead(BUTTON_3_PIN) == LOW) {
    RainbowPattern();
  }

  // Check if button 4 is pressed
  if (digitalRead(BUTTON_4_PIN) == LOW) {
    RainbowWithGlitterPattern();
  }

  // Check if button 5 is pressed
  if (digitalRead(BUTTON_5_PIN) == LOW) {
    ConfettiPattern();
  }

  // Check if button 6 is pressed
  if (digitalRead(BUTTON_6_PIN) == LOW) {
    SineLonPattern();
  }

  // Check if button 7 is pressed
  if (digitalRead(BUTTON_7_PIN) == LOW) {
    Fire2012Pattern();
  }
}

// Turn off all LEDs (set to black)
void turnOffLEDs() {
  fill_solid(leds, LED_COUNT, CRGB::Black);
  FastLED.show();
 }

// FastLED Rainbow pattern
void RainbowPattern() {
  uint8_t wait = 20;
  for (int j = 0; j < 256; j++) {
    for (int i = 0; i < LED_COUNT; i++) {
      leds[i] = CHSV((i * 256 / LED_COUNT) + j, 255, 255);
    }
    FastLED.show();
    delay(wait);
  }
}

// FastLED Rainbow with Glitter pattern
void RainbowWithGlitterPattern() {
  uint8_t wait = 20;
  for (int j = 0; j < 256; j++) {
    for (int i = 0; i < LED_COUNT; i++) {
      leds[i] = CHSV((i * 256 / LED_COUNT) + j, 255, 255);
    }
    addGlitter(80); // Add glitter effect
    FastLED.show();
    delay(wait);
  }
}

// FastLED Confetti pattern
void ConfettiPattern() {
  fadeToBlackBy(leds, LED_COUNT, 10);
  int pos = random16(LED_COUNT);
  leds[pos] += CHSV(random8(), 255, 192);
  FastLED.show();
  delay(20);
}

// FastLED SineLon pattern
void SineLonPattern() {
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, LED_COUNT, 20);
  uint8_t gHue = 0; // rotatin "base color" used by many of the patterns
  int pos = beatsin16(13,0,LED_COUNT);
  leds[pos] += CHSV( gHue, 255, 192);

    FastLED.show();

}

// FastLED Fire2012 pattern
void Fire2012Pattern() {
  static byte heat[NUM_LEDS];
  for (int i = 0; i < NUM_LEDS; i++) {
    heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  }
  for (int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  }
  if (random8() < SPARKING) {
    int y = random8(10); // Adjust the range as needed
    heat[y] = qadd8(heat[y], random8(160, 255));
  }
  for (int j = 0; j < NUM_LEDS; j++) {
    CRGB color = HeatColor(heat[j]);
    int pixelnumber;
    if (gReverseDirection) {
      pixelnumber = (NUM_LEDS - 1) - j;
    } else {
      pixelnumber = j;
    }
    leds[pixelnumber] = color;
  }
  FastLED.show();
}

void addGlitter(fract8 chanceOfGlitter) {
  if (random8() < chanceOfGlitter) {
    leds[random16(LED_COUNT)] += CRGB::White;
  }
}