//for motorshield library
#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

#include <Servo.h>

//NeoPixel Set Up using FastLED library
#include<FastLED.h>
#define NUM_LEDS 12 // number of LEDs in NEOPIXEL String
#define CHIPSET WS2812B
#define LED_PIN 11 // Which pin LEDs are controlled
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS]; // storing and manipulating the leds
#define BRIGHTNESS 128

//define temperature
// FastLED provides these pre-conigured incandescent color profiles:
//     Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc,
//     HighNoonSun, DirectSunlight, OvercastSky, ClearBlueSky,
// FastLED provides these pre-configured gaseous-light color profiles:
//     WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent,
//     FullSpectrumFluorescent, GrowLightFluorescent, BlackLightFluorescent,
//     MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium,
#define TEMPERATURE_1 Tungsten100W
#define TEMPERATURE_2 MetalHalide
#define TEMPERATURE_3 BlackLightFluorescent
#define TEMPERATURE_4 ClearBlueSky
#define TEMPERATURE_5 HighNoonSun
#define TEMPERATURE_6 Candle

// How many seconds to show each temperature before switching
#define DISPLAYTIME 6
// How many seconds to show black between switches
#define BLACKTIME   1

Servo servo1; // create servo object to control a servo
int pos = 80;

int flex_pin1 = A0; //fore finger
int flex_pin2 = A1; // middle finger
int flex_pin3 = A2; // pinkie finger
int flex_value1 = 0;
int flex_value2 = 0;
int flex_value3 = 0;

void setup(){
  pinMode(flex_pin1, INPUT);
  pinMode(flex_pin2, INPUT);
  pinMode(flex_pin3, INPUT);

  servo1.attach(9);  // Set left servo to digital pin 3

  delay( 3000 ); // power-up safety delay
  // It's important to set the color correction for your LED strip here,
  // so that colors can be more accurately rendered through the 'temperature' profiles
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 );
  FastLED.setBrightness( BRIGHTNESS );

  Serial.begin(9600);
}

void loop(){
  flex_value1 = analogRead(flex_pin1);
  flex_value2 = analogRead(flex_pin2);
  flex_value3 = analogRead(flex_pin3);

//forefinger flexsensor
  if (flex_value1 > 600 && pos>80){
    Serial.println("bending my forefinger");
      pos--;
      servo1.write(pos);
      Serial.println(pos);
    }
  
//middlefinger flexsensor
  if (flex_value2 > 800 && pos<130){
    Serial.println("bending my middle finger");
      pos++;
      servo1.write(pos);
      Serial.println(pos);
    }

//pinkiefinger flexsensor
if (flex_value3 > 700){
    Serial.println("bending my pinkie");
    //delay(10);
      // Choose which 'color temperature' profile to enable.
  uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 5);
  if( secs>0 && secs<5) {
    FastLED.setTemperature( TEMPERATURE_1 ); // first temperature
    leds[12] = TEMPERATURE_1; // show indicator pixel
  } else if (secs>5 && secs<10){
    FastLED.setTemperature( TEMPERATURE_2 ); // second temperature
    leds[12] = TEMPERATURE_2; // show indicator pixel
  } else if (secs>10 && secs<15){
    FastLED.setTemperature( TEMPERATURE_3 ); // second temperature
    leds[12] = TEMPERATURE_3; // show indicator pixel
  } else if (secs>15 && secs<20){
    FastLED.setTemperature( TEMPERATURE_4 ); // second temperature
    leds[12] = TEMPERATURE_4; // show indicator pixel
  } else if (secs>20 && secs<25){
    FastLED.setTemperature( TEMPERATURE_5 ); // second temperature
    leds[12] = TEMPERATURE_5; // show indicator pixel
  } else if (secs>25 && secs<30){
    FastLED.setTemperature( TEMPERATURE_6 ); // second temperature
    leds[12] = TEMPERATURE_6; // show indicator pixel
  }

  // Black out the LEDs for a few secnds between color changes
  // to let the eyes and brains adjust
  if( (secs % DISPLAYTIME) < BLACKTIME) {
    memset8( leds, 12, NUM_LEDS * sizeof(CRGB));
  }
  
  FastLED.show();
  FastLED.delay(8);
  } 

}