/*
Author: Juan M. Gandarias
Date: 03/11/2023
Description: ejemplo_interrup_problema2
*/
#define LED_PIN 26 // LED conectado a GPIO26
#define BUTTON_PIN 27 // Botón conectado a GPIO27
#define POT_PIN 4 // Potenciómetro conectado a GPIO04
double t; // Variable para contar tiempo
bool button_state = false; // Variable para guardar el estado del botón
volatile bool button_pressed = false; // Variable volátil booleana que es TRUE si se ha pulsado el botón
// Callback de la interrupción
void IRAM_ATTR blinkFiveTimes()
{
button_pressed = true; // Almacenamos que se ha pulsado el botón
}
void setup()
{
Serial.begin(115200); // Inicialización puerto serie
pinMode(LED_PIN, OUTPUT); // Configurar LED como output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Configurar botón como input
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), blinkFiveTimes, RISING); // Configurar la interrupción
t = 0.0; // Inicialización tiempo
}
void loop()
{
// Si se ha pulsado el botón
if (button_pressed)
{
for (uint8_t i = 0; i < 5; i++) // Repetir 5 veces
{
digitalWrite(LED_PIN, HIGH); // Encender
delay(500); // Esperar medio segundo
digitalWrite(LED_PIN, LOW); // Apagar
delay(500); // Esperar medio segundo
}
button_pressed = false; // Pongo a false la variable del botón presionado
}
// Mandar datos por puerto serie (frecuencia y valor del potenciómetro)
Serial.println("freq: ");
Serial.println(1 / double((millis() - t) / 1e3));
Serial.println("Sensor value: ");
Serial.println(analogRead(POT_PIN));
t = millis();
// Loop cada 50ms => 20Hz
delay(50);
}