-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino.ino
87 lines (82 loc) · 1.97 KB
/
Arduino.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
#include <Wire.h>
#include <JY901.h>
#include <SoftwareSerial.h>
SoftwareSerial sim(10, 11);
String number = "Your Number";
int _timeout;
String _buffer;
const int buzzer = 12;
float x;
float y;
float z;
float vectorsum ;
/*
Test on Uno R3.
JY901 UnoR3
SDA <---> SDA
SCL <---> SCL
*/
void setup()
{
JY901.StartIIC();
delay(7000); //delay for 7 seconds to make sure the modules get the signal
Serial.begin(9600);
pinMode(2, INPUT);
_buffer.reserve(50);
Serial.println("Sistem Started...");
sim.begin(9600);
delay(1000);
}
void loop()
{
JY901.GetAcc();
int button = digitalRead(2);
Serial.print(button);
x = (float)JY901.stcAcc.a[0]/32768*16;
y = (float)JY901.stcAcc.a[1]/32768*16;
z = (float)JY901.stcAcc.a[2]/32768*16;
vectorsum = sqrt(sq(x)+sq(y)+sq(z));
// Serial.print(vectorsum);
//
// Serial.print("x : "); Serial.print(x); Serial.print(", ");
// Serial.print("y : "); Serial.print(y); Serial.print(", ");
// Serial.print("z : "); Serial.print(z); Serial.print(", ");
Serial.print("Acc : "); Serial.print(vectorsum); Serial.print(", ");
if((vectorsum > 5.0) || (button == 1)){
Serial.print("Check");
buzz();
SendMessage();
}
}
void SendMessage()
{
Serial.println ("Sending Message");
sim.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set SMS Number");
sim.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
delay(1000);
String SMS = "**FALL ALERT**";
sim.println(SMS);
delay(100);
sim.println((char)26);// ASCII code of CTRL+Z
delay(1000);
_buffer = _readSerial();
}
void buzz(){
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(3000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
}
String _readSerial() {
_timeout = 0;
while (!sim.available() && _timeout < 12000 )
{
delay(13);
_timeout++;
}
if (sim.available()) {
return sim.readString();
}
}