#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define LED_PIN 12
#define TFT_CS 15
#define TFT_RST 4
#define TFT_DC 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
hw_timer_t *timer = NULL;
volatile int32_t secondCounter = 0;
volatile bool ledState = false;
int32_t lastSecond = -1;
void IRAM_ATTR onTimer() {
secondCounter++;
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Demo Temporizador ESP32");
tft.println("+ LED en Pin 12");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
timer = timerBegin(1000000);
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, 1000000, true, 0);
}
void loop() {
if (secondCounter != lastSecond) {
lastSecond = secondCounter;
tft.fillRect(50, 80, 150, 40, ILI9341_BLACK);
tft.setCursor(50, 80);
tft.print("Seg: ");
tft.print(secondCounter);
// Imprimir también en el Monitor Serie para depurar
Serial.print("Interrupcion! Segundo: ");
Serial.println(secondCounter);
}
}