/****************************************************************
Manuel Córdoba Ramos 06/11/24
Tema 6. Problema 2: Uso de timers
Al pulsar el LED debe parpadear 5 veces con un periodo de medio segundo
Mi timer debe contar 0,25 segundos
En este ejemplo no se usa Ticker
****************************************************************/
// Definición de pines I/O
#define POT_IN 4
#define LED_PIN 33
#define BUTTON_IN 27
hw_timer_t *timerLed = NULL; // Se define el timer
// Variables
double t;
double freq;
volatile int cuenta;
volatile bool conmuta;
// Funciones
void IRAM_ATTR ISR_ConmutarLed(void) {
conmuta = true;
}
// Manejadores de interrupciones
void IRAM_ATTR ISR_Boton(void) {
timerRestart(timerLed); // Pone a 0 la cuenta
timerStart(timerLed); // Hace que el timer comience a contar
}
void setup() {
Serial.begin(9600);
Serial.println("Reading the potenciometer!");
// I/O configuration
pinMode(POT_IN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_IN, INPUT_PULLUP);
// Interrupciones
attachInterrupt(digitalPinToInterrupt(BUTTON_IN), &ISR_Boton, RISING);
// Timer
timerLed = timerBegin(1000000);
timerAttachInterrupt(timerLed, &ISR_ConmutarLed); // point to the ISR
timerAlarm(timerLed, 250000, true, 0);
timerStop(timerLed); // Para el timer
// Inicialización de variables
t = 0.0;
freq = 0.0;
cuenta = 1;
conmuta = false;
}
void loop() {
Serial.println("Lectura analogica:");
Serial.println(analogRead(POT_IN));
freq = 1 / double((millis() - t) / 1000);
t = millis();
Serial.println("Frecuencia:");
Serial.println(freq);
if (conmuta) {
conmuta = false;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
cuenta++;
if (cuenta == 11) {
timerStop(timerLed); // Para el timer
cuenta = 1;
}
}
delay(50);
}