#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
#define SENSOR_READ_INTERVAL 50
unsigned long prevSensoredTime = 0;
unsigned long curSensoredTime = 0;
// Define el pin del LED
const int ledPin = 13; // Puedes cambiar el número del pin según tu conexión
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.initialize();
// Configura el pin del LED como salida
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Apaga el LED al inicio
}
void loop() {
curSensoredTime = millis();
if (curSensoredTime - prevSensoredTime > SENSOR_READ_INTERVAL) {
// Detecta movimiento basado en los datos del acelerómetro
if (motionDetected()) {
// Si se detecta movimiento, enciende el LED
digitalWrite(ledPin, HIGH);
} else {
// Si no se detecta movimiento, apaga el LED
digitalWrite(ledPin, LOW);
}
prevSensoredTime = curSensoredTime;
}
}
bool motionDetected() {
// Calcula la aceleración total como la suma de las aceleraciones en los ejes X, Y y Z
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
float totalAcceleration = sqrt(ax * ax + ay * ay + az * az);
Serial.println ("ax");
Serial.print(ax);
Serial.print ("\t ay");
Serial.print(ay);
Serial.print ("\t az");
Serial.print(az);
// Define un umbral de aceleración para detectar movimiento (ajusta este valor según tus necesidades)
float motionThreshold = 1000.0; // Puedes cambiar este valor
// Comprueba si la aceleración total supera el umbral
return totalAcceleration > motionThreshold;
}