#include <TM1637.h> // Grove 4-Digit Display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int CLK = 19;
const int DIO = 18;
TM1637 tm(CLK, DIO);
// Configuración básica
#define I2C_ADDR 0x27
LiquidCrystal_I2C lcd(I2C_ADDR, 20, 4);
const unsigned int digitos[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
struct Button {
const uint8_t PIN;
bool pressed;
uint32_t lastDebounceTime; // <-- NUEVO: para hacer antirrebote
};
Button button_up = {17, false,0};
Button button_down = {16, false, 0};
Button button_enter = {4, false, 0};
static uint32_t numberKeyPresses = 0;
const uint32_t debounceDelay = 20; // 50 ms para ignorar rebotes
void IRAM_ATTR isr_button_up() {
uint32_t currentTime = millis();
if ((currentTime - button_up.lastDebounceTime) > debounceDelay) {
if (digitalRead(button_up.PIN) == LOW) { // Confirmamos que realmente se presionó
numberKeyPresses += 1;
button_up.pressed = true;
button_up.lastDebounceTime = currentTime; // Actualizamos el tiempo de último rebote
}
}
}
void IRAM_ATTR isr_button_down() {
uint32_t currentTime = millis();
if ((currentTime - button_down.lastDebounceTime) > debounceDelay) {
if (digitalRead(button_down.PIN) == LOW) { // Confirmamos que realmente se presionó
numberKeyPresses -= 1;
button_down.pressed = true;
button_down.lastDebounceTime = currentTime; // Actualizamos el tiempo de último rebote
}
}
}
void setup() {
// tm1637
tm.init();
tm.set(BRIGHT_TYPICAL);
tm.displayStr((char *)"hola");
// Serial
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.println("Control con interrupción externa");
// Configuracion del pulsador de UP
pinMode(button_down.PIN, INPUT_PULLUP);
attachInterrupt(button_down.PIN, isr_button_down, FALLING);
// Configuracion del pulsador de DOWN
pinMode(button_up.PIN, INPUT_PULLUP);
attachInterrupt(button_up.PIN, isr_button_up, FALLING);
//-----------
// Iniciar LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor:");
}
void loop() {
// tarea principal
//tm.displayStr((char *)"hola");
//delay(100);
//tm.displayStr((char *)"Peru");
//delay(100);
// loop
//tm.displayStr((char *)"Curso de micros 2025 xdxd...", 300);
//delay(100);
if (button_up.pressed) {
Serial.printf("Button UP has been pressed %u times\n", numberKeyPresses);
button_up.pressed = false;
}
if (button_down.pressed) {
Serial.printf("Button DOWN has been pressed %u times\n", numberKeyPresses);
button_down.pressed = false;
}
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 60000) {
lastMillis = millis();
detachInterrupt(button_up.PIN);
Serial.println("Interrupt Detached!");
}
}