// Based on Adafruit Neopixel 'simple' sample

#include <Adafruit_NeoPixel.h>


// Config Leds
#define PIN          2
#define NUMPIXELS    8

// Config Photoresistance
#define LIGHT_SENSOR A0

// Config couleur
#define R 255
#define G 128
#define B 56

// Config seuil
#define ON_LEVEL 200
#define OFF_LEVEL 500



Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


void lightBoot(int r, int g, int n, int b)
{

  
  if ((b < 0) || (b > 255)) {
    return false;
  }

  int red = floor((r * b) / 100);
  int green = floor((g * b) / 100);
  int blue = floor((b * b) / 100);

  pixels.clear();
  for (int i = 0; i < NUMPIXELS; i++) { // For each pixel...
    pixels.setPixelColor(i, pixels.Color(red, green, blue));
  }
  pixels.show();   // Send the updated pixel colors to the hardware.

}

void lightLevel(int b)
{
  if ((b < 0) || (b > 255)) {
    return false;
  }

  int red = floor((R * b) / 100);
  int green = floor((G * b) / 100);
  int blue = floor((B * b) / 100);

  pixels.clear();
  for (int i = 0; i < NUMPIXELS; i++) { // For each pixel...
    pixels.setPixelColor(i, pixels.Color(red, green, blue));
  }
  pixels.show();   // Send the updated pixel colors to the hardware.

}

void lightFadeIn(int d) {
  pixels.clear();
  pixels.show();
  for (int i = 0; i < 100; i ++) {
    lightLevel(i);
    delay(d);
  }
}

void lightFadeOut(int d) {
  pixels.clear();
  pixels.show();
  for (int i = 100; i >= 0; i --) {
    lightLevel(i);
    delay(d);
  }
}


void setup() {
  int i;
  Serial.begin(9600);
  Serial.println("Booting");
  pixels.begin();
  pixels.clear();
  pixels.show();
  for (int i = 0; i <=100; i ++) {
    lightBoot(255,0,0,i);
    delay(10);
  }
  delay(1000);
  pixels.clear();
  pixels.show();
  for (int i = 100; i >= 0; i --) {
    lightBoot(255,0,0,i);
    delay(10);
  }
  Serial.println("Ready !");
}

int state = 0;

void loop() {
  int valeur = analogRead(A0);

  if ((valeur > OFF_LEVEL) && (state == 1)) {
    Serial.print("Switching OFF : val=");
    Serial.println(valeur);
    lightFadeOut(50);
    state = 0;
  }

  if ((valeur < ON_LEVEL) && (state == 0)) {
    Serial.print("Switching ON : val=");
    Serial.println(valeur);
    lightFadeIn(50);
    state = 1;
  }
  delay(1000);

}

