//First all the Ledstrip stuff
#include <FastLED.h>
#define NUM_LEDS 34     //After cutting my strip this was the amount of leds
#define LED_PIN 10
#define LEDTYPE WS2813  //Or whichever Ledstrip you're using
#define COLOR_ORDER GRB //Depends on which Ledstrip you're using
#define BRIGHTNESS 40   //60 is a good starting point, but feel free to change it
#define VOLTS 5         //Voltage of your Ledstrip
#define MAX_AMPS 500    //Limit the current to prevent problems

//Then all the Sound Sensor stuff
#define MIC_PIN A0

//Color Palettes
DEFINE_GRADIENT_PALETTE (clover){
  0, 135, 6, 6,
  64, 226, 40, 5,
  120, 12, 218, 20,
  185, 28, 82, 147,
  255, 3, 10, 55
};

DEFINE_GRADIENT_PALETTE (barrens){
  0, 0, 4, 103,
  77, 53, 0, 241,
  120, 147, 0, 255,
  164, 53, 0, 241,
  255, 0, 4, 103
};

DEFINE_GRADIENT_PALETTE (glen){
  0, 0, 56, 11,
  77, 0, 207, 77,
  120, 0, 111, 230,
  164, 0, 207, 77,
  255, 0, 56, 11
};

DEFINE_GRADIENT_PALETTE (refuge){
  0, 88, 0, 16,
  77, 156, 0, 205,
  120, 222, 0, 119,
  164, 156, 0, 205,
  255, 88, 0, 16
};

DEFINE_GRADIENT_PALETTE (tower){
  0, 26, 0, 66,
  77, 144, 0, 190,
  120, 130, 0, 255,
  164, 144, 0, 190,
  255, 26, 0, 66
};

DEFINE_GRADIENT_PALETTE (journal){
  0, 29, 5, 91,
  98, 182, 41, 68,
  118, 255, 233, 0,
  153, 182, 41, 68,
  255, 29, 5, 91
};

//And some setup for the Palettes
CRGBPalette16 currentPalette = glen;
CRGB leds[NUM_LEDS];
uint8_t paletteIndex = 0;
int paletteCounter = 0;


void setup() {
  //Pinmodes
  pinMode(MIC_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  // Ledstrip setup
  FastLED.addLeds<LEDTYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();

  // Mic setup
  Serial.begin(9600);

  // Which Palette should be shown first
  changePalette(0);
}


void loop() {
  // put your main code here, to run repeatedly:
  fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, currentPalette, 255, LINEARBLEND);
  FastLED.show();

  int sensorData = analogRead(MIC_PIN);
  delay (300);
  Serial.println(sensorData);
 
 // How much should the mic pick up to change color
  if(sensorData > 110){ 
    soundInput();
  }
}

void soundInput(){
  changePalette(paletteCounter);
  paletteCounter++;

  if (paletteCounter > 5){
    paletteCounter = 0;
  }
}

void changePalette(int newMicInput) {
  switch(newMicInput){
    case 0:
      currentPalette = clover;
      break;

    case 1:
      currentPalette = barrens;
      break;

    case 2:
      currentPalette = glen;
      break;

    case 3:
      currentPalette = refuge;
      break;

    case 4:
      currentPalette = tower;
      break;

    case 5:
      currentPalette = journal;
      break;

    default:
      Serial.println("No matching case!");  //This one is for when something goes wrong
      break;
  }
}


