This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPU6050_Buzzer_Alert.ino
93 lines (76 loc) · 1.75 KB
/
MPU6050_Buzzer_Alert.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
vcc = 3.3v
Gnd = gnd
int = 2
scl = a5
sda = a4
*/
#include "I2Cdev.h"
#include "MPU6050.h"
int buzzer = 13;
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int val;
int prevVal;
int valax;
int valay;
int valaz;
// Tracks the time since last event fired
unsigned long previousMillis = 0;
unsigned long int previoussecs = 0;
unsigned long int currentsecs = 0;
unsigned long currentMillis = 0;
int secs = 0;
int interval = 1; // updated every 1 second
void setup()
{
Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
pinMode(buzzer, OUTPUT);
}
void loop()
{
currentMillis = millis();
currentsecs = currentMillis / 1000;
if ((unsigned long)(currentsecs - previoussecs) >= interval)
{
secs = secs + 1;
previoussecs = currentsecs;
if (secs >= 59)
{
secs = 0;
}
// Serial.println("Seconds:");
// Serial.println(secs);
}
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
valax = map(ax, -17000, 17000, 0, 255);
valay = map(ay, -17000, 17000, 0, 255);
valaz = map(az, -17000, 17000, 0, 255);
Serial.println("ax: ");
Serial.println(valax);
// Serial.println("ay:");
// Serial.println(valay);
// Serial.println("az: ");
// Serial.println(valaz);
// Serial.println("");
// Serial.println("");
delay(200);
if ((valax > 130) && (secs > 2))
{
digitalWrite(buzzer, HIGH);
}
if ((valax >= 60) && (valax <= 100))
{
digitalWrite(buzzer, LOW);
secs = 0;
}
if ((valax < 50) && (secs > 2))
{
digitalWrite(buzzer, HIGH);
}
}