// Information about Adafruit IO
// Link: https://learn.adafruit.com/welcome-to-adafruit-io/overview

// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// In the config.h you should set the Adafruit IO credentials and configure the 
// network settings. 

#include "config.h"

/************************ Example Starts Here *******************************/


// Variables
bool valor;
int count = 0;
bool flag = false;
unsigned long previo;
unsigned long actual;
unsigned long tiempo;
unsigned long ttotal = 0;
unsigned long intervalo = 1000;

unsigned long duracion = 0;
const int puerta = 2; // GPIO2
const int led = 0; // GPIO0


// set up the feeds
AdafruitIO_Feed *tempo = io.feed("tempo");
AdafruitIO_Feed *cuenta = io.feed("cuenta");

void setup() {

  // start the serial connection
  Serial.begin(115200);

  pinMode(puerta, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led_conteo, OUTPUT);
  pinMode(led_puerta, OUTPUT);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

  

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  digitalWrite(led, LOW);

  valor = digitalRead(puerta); // Read the sensor. If the door is close valor = High or true. If the door is open valor = low or false.

  if (valor == false & flag == false) {
    
    delay(200);

    valor = digitalRead(puerta);
          
    if (valor == false){
      count++;            // Increase the opening counter
      flag = true;
      previo = millis();  // Get the time at that moment
      
      }
   }
      
  if (valor == true & flag == true) {
    
    delay(200);

    valor = digitalRead(puerta);

    if (valor == true){
      flag = false;
      actual = millis();  // Get the time at that moment.
    
      // Increase the time variable after the door was closed.    
      if (actual - previo > intervalo){
        tiempo = actual - previo;

        tiempo = tiempo/1000;  // Change time from miliseconds to seconds.

        ttotal = tiempo + ttotal;  // Add time to the total timer.
        }
              
      }
    }
  
  
  duracion++; // increase by one the duracion variable
  delay(400);
  

  if (duracion > 3800){ //Set time between data sending.

    // Show the variable ttotal in the serial monitor and send it to Adafruit
    Serial.print("sending -> ");
    Serial.println(ttotal);
    tempo->save(ttotal);

    // Show the variable count in the serial monitor and send it to Adafruit
    Serial.print("sending -> ");
    Serial.println(count);
    cuenta->save(count);
    
    // Reset variables
    duracion = 0;
    ttotal = 0;
    count = 0;

    digitalWrite(led, HIGH);  // turn on the light to show sending data

    delay(1000); //Delay 1 second
    
  }
}
