#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>
#include <HCSR04.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define NUM_ARCS 4
#define TRIGGER_PIN 4 // Pin del trigger
#define ECHO_PIN 5 // Pin del echo
#define LED_PIN 14 // Pin del LED
// Declaración de objetos
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo radarServo;
UltraSonicDistanceSensor sensor(TRIGGER_PIN, ECHO_PIN);
// Variables globales
int angle = 0; // Variable global para el ángulo
const int MOSQUITO_THRESHOLD = 5; // Distancia en cm para detectar mosquitos
bool mosquitoDetected = false; // Bandera para indicar si se ha detectado un mosquito
// Variables globales para el arco
int centerX = SCREEN_WIDTH / 2;
int centerY = SCREEN_HEIGHT;
int radius = 64;
// Función para dibujar un arco
void drawArc(int x, int y, int radius, float startAngle, float endAngle, uint16_t color) {
float angleStep = 0.1; // Ajusta la precisión del arco
float angle = startAngle;
while (angle < endAngle) {
float x1 = x + cos(angle * PI / 180.0) * radius;
float y1 = y - sin(angle * PI / 180.0) * radius;
display.drawPixel(x1, y1, color);
angle += angleStep;
}
}
// Función para dibujar los arcos del radar
void drawRadarArcs() {
for (int i = 0; i < NUM_ARCS; i++) {
float startAngle = i * 360.0 / NUM_ARCS;
float endAngle = (i + 1) * 360.0 / NUM_ARCS;
drawArc(centerX, centerY, radius, startAngle, endAngle, SSD1306_WHITE);
}
}
// Función para mover el radar
void moveRadar() {
angle++;
angle %= 360;
// Mueve el servo
radarServo.write(angle);
// Actualiza la posición del radar en la pantalla OLED
drawRadar(centerX, centerY, radius, angle - 90, angle + 90, SSD1306_WHITE);
}
// Función para detectar mosquitos
bool detectMosquito(int distancia) {
if (distancia <= MOSQUITO_THRESHOLD) {
// Buscar objetos en la zona cercana al radar
// ... (Función para analizar la imagen del radar)
return true;
}
return false;
}
// Función para dibujar un círculo simulando el radar
void drawRadarCircle() {
display.drawCircle(centerX, centerY, radius, SSD1306_WHITE);
}
void setup() {
Serial.begin(115200);
// Inicialización de la pantalla OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.display();
delay(2000);
// Inicialización del servo
radarServo.attach(5); // Ajusta el pin del servo aquí
// Inicialización del LED
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int distancia = sensor.measureDistanceCm();
// Detecta mosquito y enciende el LED
if (detectMosquito(distancia)) {
digitalWrite(LED_PIN, HIGH);
mosquitoDetected = true;
} else {
digitalWrite(LED_PIN, LOW);
mosquitoDetected = false;
}
// Muestra información en la pantalla OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Distancia: ");
display.print(distancia);
display.setCursor(0, 10);
display.print("Mosquito detectado: ");
display.print(mosquitoDetected ? "Sí" : "No");
// Muestra el estado del LED
if (mosquitoDetected) {
display.fillRect(0, 20, 10, 10, SSD1306_WHITE);
} else {
display.fillRect(0, 20, 10, 10, SSD1306_BLACK);
}
display.display();