-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPU6050Script.ino
156 lines (124 loc) · 3.57 KB
/
MPU6050Script.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <MKRGSM.h>
#include "arduino_secrets.h"
Adafruit_MPU6050 mpu;
int x = 0;
int y = 0;
bool go = true;
int z = 0;
#include <SDHCI.h>
#include <Audio.h>
SDClass theSD;
AudioClass *theAudio;
File myFile;
bool ErrEnd = false;
// PIN Number for SIM Card
const char PINNUMBER[] = SECRET_PINNUMBER;
lte_initialize sms;
static void audio_attention_cb(const ErrorAttentionParam *atprm)
{
puts("Attention!");
if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
{
ErrEnd = true;
}
}
void setup(void) {
Serial.begin(115200);
//SMS init
while (!connected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
//MPU init
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_21_HZ);
//Audio and SD init
while (!theSD.begin())
{
Serial.println("Insert SD card.");
}
// start audio system
theAudio = AudioClass::getInstance();
theAudio->begin(audio_attention_cb);
puts("initialization Audio Library");
theAudio->setRenderingClockMode(AS_CLKMODE_NORMAL);
theAudio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
err_t err = theAudio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_MONO);
/* Verify player initialize */
if (err != AUDIOLIB_ECODE_OK)
{
printf("Player0 initialize error\n");
exit(1);
}
/* Open file placed on SD card */
myFile = theSD.open("init.mp3"); //plays an audio saying "initalizing"
/* Verify file open */
if (!myFile)
{
printf("File open error\n");
exit(1);
}
printf("Open! 0x%08lx\n", (uint32_t)myFile);
/* Send first frames to be decoded */
err = theAudio->writeFrames(AudioClass::Player0, myFile);
if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND))
{
printf("File Read Error! =%d\n",err);
myFile.close();
exit(1);
}
puts("Play!");
/* Main volume set to -16.0 dB MAXIMM is 120*/
theAudio->setVolume(120);
theAudio->startPlayer(AudioClass::Player0);
}
void loop() {
if(x >= 8 || x <= -8){
Serial.println("Fall detected!");
go = true;
}
while (go) {
int err = theAudio->writeFrames(AudioClass::Player0, myFile);
if (err == AUDIOLIB_ECODE_FILEEND) {
Serial.println("File ended!\n");
myFile.close();
myFile = theSD.open("ask_help.mp3"); //prompt begins here
long int t1 = millis();
while (digitalRead(A4) == HIGH) {
long int t2 = millis();
long int t3 = t1-t2;
if (t3 >= 20000) {
//sms send
sms.beginSMS("2035559081"); //phone numbers must be preloaded
sms.print("John fell off their bike and has not prevented this message from being sent!"); //message must be preloaded
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
}
go = false;
}
usleep(40000);
}
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
x = g.gyro.x;
y = g.gyro.y;
z = g.gyro.z;
}