Entiendo tu proyecto. Vamos a crear un sketch de Arduino para un ESP32 que cumpla con tus requerimientos. Te proporcionaré un código base que puedes adaptar según tus necesidades específicas. Aquí está el código:
#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BluetoothSerial.h>
#define IR_RECEIVE_PIN 15 // Pin del receptor IR
#define BUZZER_PIN 12 // Pin del buzzer para la alarma
#define SCREEN_WIDTH 128 // Ancho de la pantalla OLED
#define SCREEN_HEIGHT 64 // Alto de la pantalla OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
BluetoothSerial SerialBT;
bool alarmActive = false;
unsigned long startTime = 0;
const long countdownDuration = 5000; // 5 segundos en milisegundos
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
pinMode(BUZZER_PIN, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
SerialBT.begin("ESP32_Alarm"); // Nombre del dispositivo Bluetooth
}
void loop() {
if (IrReceiver.decode()) {
startCountdown();
IrReceiver.resume();
}
if (alarmActive) {
unsigned long currentTime = millis();
long timeLeft = countdownDuration - (currentTime - startTime);
if (timeLeft > 0) {
displayCountdown(timeLeft / 1000);
} else {
triggerAlarm();
}
}
checkBluetoothCommand();
}
void startCountdown() {
alarmActive = true;
startTime = millis();
}
void displayCountdown(int seconds) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Tiempo: ");
display.print(seconds);
display.display();
}
void triggerAlarm() {
digitalWrite(BUZZER_PIN, HIGH);
display.clearDisplay();
display.setCursor(0, 0);
display.print("ALARMA!");
display.display();
}
void checkBluetoothCommand() {
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n');
if (command == "OFF") {
alarmActive = false;
digitalWrite(BUZZER_PIN, LOW);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Alarma OFF");
display.display();
}
}
}
Este código hace lo siguiente:
Utiliza la librería IRremote para recibir señales del control infrarrojo. Implementa un contador de 5 segundos cuando se recibe una señal IR. Muestra el conteo en una pantalla OLED usando comunicación I2C. Activa una alarma (buzzer) después de los 5 segundos. Permite desactivar la alarma a través de Bluetooth.
Para usar este código, necesitarás instalar las siguientes librerías en tu Arduino IDE:
IRremote
Adafruit GFX Library
Adafruit SSD1306
Asegúrate de ajustar los pines según tu configuración específica. Para desactivar la alarma, conecta tu smartphone al ESP32 vía Bluetooth y envía el comando "OFF". ¿Te gustaría que explique alguna parte específica del código o que haga alguna modificación?