#include <Wire.h>
#include <MPU6050.h>
#define TRIGGER_PIN 13
#define ECHO_PIN 12
#define BUZZER_PIN 14
#define BUZZER_PIN1 27
#define TEMPERATURE_PIN 34
#define RED_LED_PIN 4
#define BLUE_LED_PIN 2
String inputString = "";
bool stringComplete = false;
MPU6050 mpu;
// Variables para la detección de movimientos bruscos
float last_ax = 0, last_ay = 0, last_az = 0;
const float threshold = 0.5; // Umbral para detectar cambios bruscos
void setup() {
Serial.begin(115200);
Serial.println("Setup iniciado..."); // Mensaje inicial
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUZZER_PIN1, OUTPUT);
pinMode(TEMPERATURE_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
// Iniciar MPU6050
Wire.begin();
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 conectado correctamente.");
} else {
Serial.println("Error al conectar con MPU6050.");
while (1);
}
}
void loop() {
if (stringComplete) {
Serial.println("Comando recibido: " + inputString);
if (inputString == "T") {
Serial.println("Leyendo temperatura...");
int lectura = analogRead(TEMPERATURE_PIN);
float voltaje = (lectura / 4095.0) * 3.3; // ESP32 has 12-bit resolution (0-4095) and 3.3V reference voltage
float tempC = voltaje * 100.0; // LM35 provides 10mV per degree Celsius
if (tempC >= 39.0) {
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue LED
} else if (tempC <= 37.0) {
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(BLUE_LED_PIN, HIGH); // Turn on blue LED
} else {
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue LED
}
Serial.print("Temperatura: ");
Serial.print(tempC);
Serial.println(" °C");
} else if (inputString == "D") {
Serial.println("Midiendo distancia...");
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration / 58.2; // Convert time to distance in cm
if (distance <= 100) {
tone(BUZZER_PIN1, 1000); // Set buzzer tone to 1000Hz
} else {
noTone(BUZZER_PIN1); // Stop buzzer tone
}
Serial.print("Distancia: ");
Serial.print(distance);
Serial.println(" cm");
} else if (inputString == "A") {
Serial.println("Midiendo aceleración...");
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
float acceleration = sqrt(ax * ax + ay * ay + az * az);
float speed_kmh = acceleration * 3.6;
if (speed_kmh <= 200) {
tone(BUZZER_PIN, 1000); // Set buzzer tone to 1000Hz
} else {
noTone(BUZZER_PIN); // Stop buzzer tone
}
Serial.print("Velocidad: ");
Serial.print(speed_kmh);
Serial.println(" km/h");
} else if (inputString == "M") {
Serial.println("Detectando movimientos bruscos...");
detectMovimientosBruscos();
}
delay(100); // Short delay before next reading
inputString = "";
stringComplete = false;
}
}
void detectMovimientosBruscos() {
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
float ax_g = ax / 16384.0;
float ay_g = ay / 16384.0;
float az_g = az / 16384.0;
if (abs(ax_g - last_ax) > threshold || abs(ay_g - last_ay) > threshold || abs(az_g - last_az) > threshold) {
Serial.println("¡Movimiento brusco detectado!");
tone(BUZZER_PIN, 2000); // Set buzzer tone to 2000Hz
delay(100); // Buzzer on for 100ms
noTone(BUZZER_PIN); // Stop buzzer tone
}
last_ax = ax_g;
last_ay = ay_g;
last_az = az_g;
// Imprimir las lecturas actuales para depuración
Serial.print("Aceleracion en X: ");
Serial.print(ax_g);
Serial.print(" g, Y: ");
Serial.print(ay_g);
Serial.print(" g, Z: ");
Serial.print(az_g);
Serial.println(" g");
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
inputString.trim(); // Eliminar posibles espacios o caracteres no deseados
}
}
}