-
Notifications
You must be signed in to change notification settings - Fork 0
/
FallDetection
110 lines (99 loc) · 3.16 KB
/
FallDetection
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
#define CUSTOM_SETTINGS
#define INCLUDE_EMAIL_SHIELD
#define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
#define INCLUDE_TOGGLE_BUTTON_SHIELD
#define INCLUDE_GPS_SHIELD
#define INCLUDE_NOTIFICATION_SHIELD
#define INCLUDE_ORIENTATION_SENSOR_SHIELD
#define INCLUDE_TWITTER_SHIELD
#define INCLUDE_SMS_SHIELD
#include <OneSheeld.h>
boolean stopGettingReadings = false;
unsigned long lastTimeTaken = 0;
unsigned long currentTime = 0;
//zero_G is the reading we expect from the sensor when it detects
//no acceleration. Subtract this value from the sensor reading to
//get a shifted sensor reading.
int scale = 9.81;
float x, y, z;
float lat ;
float lon ;
char charlat [30];
char charlon [30];
char readings [100];
int magnitudeThreshold = 2.3;
int xa = 0;
int ya = 0;
int za = 0;
void setup() {
// put your setup code here, to run once:
OneSheeld.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if(!stopGettingReadings)
{
x = AccelerometerSensor.getX();
y = AccelerometerSensor.getY();
z = AccelerometerSensor.getZ();
}
xa = x/scale; // calculates x acceleration
ya = y/scale; // calculates y acceleration
za = z/scale; // calculates z acceleration
if (sqrt(sq(xa) + sq(ya) + sq(za)) > magnitudeThreshold && (abs(OrientationSensor.getY()) < 45))
{
Notification.notifyPhone("detected fall | 10 seconds to cancel");
/* Wait for 300 ms. */
/* Stop reading the accelerometer values to keep looping on. */
stopGettingReadings = true;
/* Take the time once the accident happens only. */
if(!lastTimeTaken)
{
lastTimeTaken = millis();
currentTime = lastTimeTaken;
}
/* Loop on time and if time exceeds 10 seconds and no one pressed the toggle button in ToggleShield send the messages and Restart the System. */
if(currentTime - lastTimeTaken > 10000 && !ToggleButton.getStatus())
{
lat = GPS.getLatitude();
lon = GPS.getLongitude();
dtostrf(lat, 3, 6, charlat);
dtostrf(lon, 3, 6, charlon);
strcat(readings, "I fell. pick me up from: ");
strcat(readings, "http://maps.google.com/maps?q=");
strcat (readings, charlat);
strcat(readings, ",");
strcat (readings, charlon);
Notification.notifyPhone("You Fall MA DuDE?");
delay(10);
Phone.call("6179914637");
delay(300);
/* Wait for 300 ms. */
Email.send("samer.neergaard@gmail.com", "I Fell, Again.", readings);
delay(10);
SMS.send("6179914637", readings);
//String name = "@tyler_hayman97";
//String message = readings;
//Twitter.sendMessage(name,message);
//Facebook.post(readings);
resetSystem();
}
else
{
/* Check on the button if pressed Restart the whole System.*/
if(ToggleButton.getStatus())
{
resetSystem();
//Terminal.println("ButtonPressed"); //This line is just for debuggint
}
else currentTime = millis(); /* Keep Getting time to check on it.*/
}
}
}
/* Restart Function. */
void resetSystem()
{
stopGettingReadings = false;
lastTimeTaken = 0;
currentTime = 0;
}