#define boton 18
#define led 16
int tiempoI;
int duracion;
int contadorG;
int contadorP;
int paso30;
volatile bool boton_pulsado = false;
void IRAM_ATTR medida() {
if (digitalRead(boton) == LOW) {
tiempoI = millis();
boton_pulsado = true;
}
else if (digitalRead(boton) == HIGH && boton_pulsado) {
duracion = millis() - tiempoI;
boton_pulsado = false;
}
}
void setup() {
Serial.begin(115200);
pinMode(boton, INPUT_PULLUP);
pinMode(led, OUTPUT);
attachInterrupt(digitalPinToInterrupt(boton), medida, CHANGE);
}
void loop() {
if (!boton_pulsado && duracion > 0) {
if (duracion > 1000) {
contadorG++;
} else {
contadorP++;
}
Serial.print("Nº Cajas Grandes: ");
Serial.println(contadorG);
Serial.print("Nº Cajas Pequeñas: ");
Serial.println(contadorP);
// Resetear la duración para evitar doble conteo
duracion = 0;
}
// han pasado más de 30 segundos sin detección
if (millis() - paso30 > 30000) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
delay(10);
}