/*
 * ***********************************************************************************************
 * *     PROGRAMA PARA APRENDER A CONFIGURAR PATILLAS COMO ENTRADA DIGITAL Ver-2                 *
 * *                                                                                             *
 * *     ¿QUE HACE? Este programa va a hacer que al darle al pulsador encienda el led            *
 * *                 si está apagado, o lo apague si está encendido                              *
 * *                                                                                             *
 * *  ¿QUE TENEMOS QUE SABER?                                                                    *
 * *               El microprocesador es tan rápido, que en el tiempo que cerramos el pulsador   *
 * *               ejecuta miles de operaciones                                                  *
 * *                                                                                             *
 * *     *NOTA: UNA PATILLA AL AIRE (Sin conectar a ningún potencial) NO ES NI ALTO NI BAJO,     *
 * *            ES UN RUIDO                                                                      *
 * *                                                                                             *
 *                                                                                               *
 *                     INICIO                                                                    *
 *                        |                                                                      *
 *                   Apaga el Led                                                                *
 *                 Pon EstadoLed = 0                                                             *
 *       ----------------> |<--------------------                                                *
 *       |               / \                    |                                                *
 *       |              /   \                   |                                                *
 *       |    ¿ Esta activado Pulsador?  >-- No--                                                *
 *       |              \   /                                                                    *
 *       |                Si                                                                     *
 *       |                |                                                                      *
 *       |               / \                                                                     *
 *       |              /   \                                                                    *
 *       |         EstadoLed = 0? >- No ------                                                   *
 *       |              \   /                |                                                   *
 *       |                si                 |                                                   *
 *       |                |                  |                                                   *
 *       |         Enciende Led          Apaga Led                                               *
 *       |         Espera 250ms        Espera 250ms                                              *
 *       |                |                  |                                                   *
 *       |                |<-----------------                                                    *
 *       |                |                                                                      *
 *       |                |                                                                      *
 *       |                |                                                                      *
 *       ----------------<|                                                                      *
 *                       Fin                                                                     *
 * ***********************************************************************************************
 */

#define Led  13 // El led se conecta al pin 13
#define Pulsador 2 // Pin donde conectamos la entrada

int Lectura; // Variable donde guardaremos la entrada
int EstadoLed =0; // Guarda el estado del Led

void setup() 
  {
  pinMode(Led,OUTPUT);    // Patilla del Led como salida
  pinMode(Pulsador,INPUT); // Patilla del Pulsador como entrada 
  }

void loop() 
  {
  Lectura = digitalRead(Pulsador); // Lee el contenido del pulsador, HIGH o LOW
  
  if(digitalRead(Pulsador)) // Si esta a nivel alto
    {
      if (EstadoLed == 0)
        {
       digitalWrite(Led,HIGH); // Enciende el Led
       EstadoLed = 1; // Recuerda que se a encendido el Led
       delay(250);
        }
      else
        {
        digitalWrite(Led,LOW); 
        EstadoLed = 0; // Recuerda que se a apagado el Led
        delay(250);
        }
    }
 
}