/*************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - Arduino MKR1000 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           
#define BLYNK_TEMPLATE_NAME         
#define BLYNK_AUTH_TOKEN            
 
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SerialUSB

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Adafruit_CircuitPlayground.h>
#include "Talkie.h"
#include "Vocab_US_Large.h"
#include <Proximity.h>
Talkie voice;
Proximity prox;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  SerialUSB.begin(115200);
  prox.begin(95);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  //  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass);
  // You can also specify server:
  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
}


long last_sent = 0;
int prev_light = -1;
bool woke_up = false;
bool getting_ready_to_go = false;
int delta_change_threshold = 50;

void loop()
{
  Blynk.run();
  if((millis() - last_sent) > 1000) {
    // waiting for the celebrating one to wake up
    if (!woke_up){
      int light = analogRead(A8);
      Serial.println(light);
      
      // only when not first measurement, check delta of volumes between now and a second ago
      if (prev_light >= 0 && light - prev_light > delta_change_threshold){
        Serial.println("turned on the lights!");
        triggerBirthdayAlerts();
        woke_up = true;
        Blynk.virtualWrite(V0, "turned on the lights, he woke up!");
      }
      
      prev_light = light;
    }

    // after woke up, waiting for celebrating one to get close
    else if (woke_up && !getting_ready_to_go){
      if (prox.close()){
        Serial.println("getting ready to go!");
        getting_ready_to_go = true;
        Blynk.virtualWrite(V0, "is getting ready to go!");
      }          
    }
    last_sent = millis();
  }
}

void triggerBirthdayAlerts(){
  voice.say(sp4_YOU);
  voice.say(sp2_R);
  voice.say(sp3_TWENTY);
  voice.say(sp2_SEVEN);
}
