/*
 * 
 * Modder: @red9030
 * Title: Uso de botones con esp32
 * Date:  3/07/2024
 * 
 * REFERENCE: 
 * 
 */

/*
 * NOTAS: 
 * 
 */

/*
 *****************************************************
 *    LIBRERIAS
 *****************************************************
 */

/*
 *****************************************************
 *    VARIABLES
 *****************************************************
 */ 

int LED_01= 2; // Onboard led conected to GPIO2 PIN4 (D2) on 30pin kit

#define BUTTON_PIN1 19  // GPIO19 pin connected to button (use intern pullup resistence )
#define BUTTON_PIN2 18  // GPIO18 pin connected to button (use extern pullup resistence )
#define BUTTON_PIN3 21  // GPIO21 pin connected to button (use extern pulldown resistence )

// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState;     // the current reading from the input pin
/*
 *****************************************************
 *    FUNCIONES
 *****************************************************
 */
 
  /*
 *****************************************************
 *    INICIO
 *****************************************************
 */
void setup () {
    Serial.begin(9600);

   // initialize the pushbutton pin as input (external pull-down) 
  pinMode(BUTTON_PIN3, INPUT);
   // initialize the pushbutton pin as input (external pull-up)
  pinMode(BUTTON_PIN2, INPUT);
   // initialize the pushbutton pin as an pull-up input
  pinMode(BUTTON_PIN1, INPUT_PULLUP);

   // initialize the led pin as output (external pull-up)
  pinMode(LED_01, OUTPUT);
  digitalWrite(LED_01, lastState);
}
 /*
 *****************************************************
 *    REPETICIÓN
 *****************************************************
 */
void loop() {
  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN2);

 if (lastState == HIGH && currentState == LOW){
    Serial.println("The button is pressed");
    digitalWrite(LED_01, currentState);
}else{ if (lastState == LOW && currentState == HIGH)
    Serial.println("The button is released");
    digitalWrite(LED_01, currentState);
}
  // save the last state
  lastState = currentState;

}