-
Notifications
You must be signed in to change notification settings - Fork 0
/
FallDetector
70 lines (58 loc) · 2.14 KB
/
FallDetector
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
// these constants describe the pins. They won't change:
const int xpin = A0; // x-axis of the accelerometer
const int ypin = A1; // y-axis
const int zpin = A2; // z-axis (only on 3-axis models)
int ledPin = 6;
const int piezo = 9;
int xa = 0;
int ya = 0;
int za = 0;
int sampleDelay = 100; //number of milliseconds between readings
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
Serial.println("Starting up");
tone(piezo, 2000, 500);
//Make sure the analog-to-digital converter takes its reference voltage from
// the AREF pin
analogReference(EXTERNAL);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
}
void loop()
{
int x = analogRead(xpin);
//add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
int y = analogRead(ypin);
//add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
int z = analogRead(zpin);
//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.
float zero_G = 512.0;
//scale is the number of units we expect the sensor reading to
//change when the acceleration along an axis changes by 1G.
//Divide the shifted sensor reading by scale to get acceleration in Gs.
float scale = 102.3;
xa = ((float)x - zero_G)/scale; // calculates x acceleration
ya =((float)y - zero_G)/scale; // calculates y acceleration
za = ((float)z - zero_G)/scale; // calculates z acceleration
Serial.print(((float)x - zero_G)/scale);
Serial.print("\t"); Serial.print(((float)y - zero_G)/scale);
Serial.print("\t"); Serial.print(((float)z - zero_G)/scale);
Serial.print("\n"); // delay before next reading:
delay(sampleDelay);
if (sqrt(sq(xa) + sq(ya) + sq(za)) > 2.5) {
digitalWrite(ledPin, HIGH);
tone(piezo, 4000, 1500);
Serial.print("Fall");
delay(3000); //this code keeps LED on for 3 seconds just for interaction purposes
}
else digitalWrite(ledPin, LOW);
}