Need help with code (MPU6050 and bluetooth)

hi there I don't have much experience and need help with this code.
basically i am using a MPU6050 to detect fall (like when arduino setup is dropped on the floor), the buzzer will sound.

Is this code below ok (it works weirdly in the simulator)? And I want to add HC 05 module to it but I can't figure out how.

big thanks to whoever is willing to help!!!

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>


const Adafruit_MPU6050 mpu;
const int BUZZER = 10;

void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

  pinMode(BUZZER, OUTPUT);


  Serial.println("Setup over");

  delay(100);
  
}


bool fallen = 0, beep = 0;
unsigned long timeOfFall=0;


void loop() {
  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);


  sensors_vec_t s = a.acceleration;



  Serial.println(sqrt((s.x*s.x + s.y*s.y + s.z*s.z)));

  if (s.x*s.x + s.y*s.y + s.z*s.z > 3*3 || (fallen && !beep))
    accelerationDetected();
  
  if (beep)
    tone(BUZZER, 1000); 
  else noTone(BUZZER);

void resetBeep(){
  beep=false;
}

void accelerationDetected(){
  if(!fallen){
    fallen = true;
    timeOfFall = millis();
  }
  if ((millis()-timeOfFall)>3000)
    beep=1;
}

rather than adding an external bluetooth module to a UNO or similar microcontroller move to an ESP32 which has on board WiFi, Bluetooth Classic and BLE
the ESP32 is supported by the Arduino IDE
I have used the MPU6050 with the ESP32 without problems

what are you transmitting the Bluetooth/BLE data too? e.g. a smartphone?

a smartphone app (which will call emergency contacts like a fall detector). i only have HC 05 and ZS040 (dont have time to buy a ESP32)

a simple serial connection to a Bluetooth terminal app on a smartphone should give you a start
have a look at hc05-bluetooth-arduino-tutorial

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.