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;
}