1
+ // Import necessary modules
2
+ #include " mbed.h"
3
+ #include " rtos.h"
4
+ #include " uLCD_4DGL.h"
5
+ #include " MMA8452.h"
6
+ #include " ultrasonic.h"
7
+
8
+ // Initializa variables
9
+ Serial pc (USBTX,USBRX);
10
+ Serial blue (p13,p14);
11
+ DigitalOut led4 (LED4);
12
+ DigitalOut led3 (LED3);
13
+ DigitalOut led2 (LED2);
14
+ DigitalOut led1 (LED1);
15
+ AnalogIn tmp (p15);
16
+ uLCD_4DGL uLCD (p9, p10, p11);
17
+ MMA8452 acc (p28, p27, 100000 );
18
+ PwmOut speaker (p23);
19
+ ultrasonic mu (p6, p7, .1 , 1 );
20
+ Thread thread1; Thread thread2;
21
+ Thread thread3; Thread thread4;
22
+ Thread thread5; Thread thread6;
23
+ Mutex m; DigitalOut camera_trigger (p18);
24
+ DigitalOut wifi_trigger (p18);
25
+ DigitalIn pb1 (p20);
26
+ DigitalIn pb2 (p19);
27
+ DigitalIn pb3 (p17);
28
+
29
+ float tempC, tempF;
30
+ double x, y, z, dist_accel;
31
+ int dist_sonar; int i;
32
+ volatile bool sound = false ;
33
+ volatile bool arm = false ;
34
+
35
+ void temp_thread () {
36
+ while (1 ) {
37
+ float vin = 5.0 * LM61 / 1024.0 ;
38
+ tempC = (1.8663 - vin) / 0.01169 ; tempF = 1.8 * tempC + 32.0 ;
39
+ tempC = ((tmp * 3.3 ) * 100 - 50 ); tempF = 1.8 * tempC + 32.0 ;
40
+
41
+ // mutex lock
42
+ m.lock ();
43
+ uLCD.locate (1 ,1 );
44
+ uLCD.printf (" %5.2F C %5.2F F \n\r " , tempC, tempF);
45
+ m.unlock ();
46
+
47
+ // Adjusted Thread wait time
48
+ Thread::wait (900 );
49
+ }
50
+ }
51
+
52
+ // Accelerometer Thread
53
+ void accel_thread () {
54
+ while (1 ) {
55
+ acc.readXYZGravity (&x,&y,&z);
56
+ m.lock ();
57
+ uLCD.locate (0 ,3 );
58
+ uLCD.printf (" x:%lf\n " ,x);
59
+ uLCD.locate (0 ,4 );
60
+ uLCD.printf (" y:%lf\n " ,y);
61
+ uLCD.locate (0 ,5 );
62
+ uLCD.printf (" z:%lf\n " ,z);
63
+ m.unlock ();
64
+ dist_accel = (x*x + y*y + z*z)^0.5 ;
65
+ uLCD.printf (" dist:%lf\n " , dist_accel);
66
+ Thread::wait (800 );
67
+ }
68
+ }
69
+
70
+ void dist (int distance) {
71
+ // put code here to execute when the distance has changed
72
+ uLCD.printf (" Distance %d mm\r\n " , distance);
73
+ // if less than 50mm, alarm activated
74
+ }
75
+
76
+ ultrasonic mu (p6, p7, .1 , 1 , &dist);
77
+
78
+ // Sonar Thread
79
+ void sonar_thread () {
80
+ while (1 ) {
81
+ mu.startUpdates ();
82
+ // start measuring the distance --SONAR
83
+ mu.checkDistance ();
84
+ m.lock ();
85
+ uLCD.locate (0 ,10 );
86
+ dist_sonar = mu.getCurrentDistance ();
87
+ uLCD.printf (" Distance: %d" , dist_sonar);
88
+ wait (0.2 ); uLCD.printf (" " );
89
+ m.unlock ();
90
+ Thread::wait (700 );
91
+ }
92
+ }
93
+
94
+ // Speaker Thread
95
+ void speaker_thread () {
96
+ while (1 ) {
97
+ led3 = !led3;
98
+ if (sound) {
99
+ for (i=0 ; i<10 ; i=i+2 ) {
100
+ // --SPEAKER
101
+ speaker.period (1.0 /969.0 );
102
+ speaker = float (i)/50.0 ;
103
+ wait (.5 );
104
+ speaker.period (1.0 /800.0 );
105
+ wait (.5 );
106
+ }
107
+ }
108
+ Thread::wait (600 );
109
+ }
110
+ }
111
+
112
+ // Thresholds for Alarm Trigger
113
+ void checker_thread () {
114
+ while (1 ) {
115
+ if (arm) {
116
+ if (tempC > 27 || x > 0.3 || y < -0.5 || dist_sonar < 50 ) {
117
+ sound = true ;
118
+ camera_trigger = 1 ;
119
+ m.lock ();
120
+ uLCD.printf (" DONE!" );
121
+ m.unlock ();
122
+
123
+ for (i=0 ; i<10 ; i=i+2 ) {
124
+ // --SPEAKER
125
+ speaker.period (1.0 /969.0 );
126
+ speaker = float (i)/50.0 ;
127
+ wait (.5 );
128
+ speaker.period (1.0 /800.0 );
129
+ wait (.5 );
130
+ }
131
+ led2 = !led2;
132
+ }
133
+ if (tempC < 27 || x > 0.3 || y < -0.5 || dist_sonar > 50 ) {
134
+ sound = false ;
135
+ speaker = 0.0 ;
136
+ camera_trigger = 0 ;
137
+ }
138
+ else {
139
+ sound = false ;
140
+ }
141
+ }
142
+ Thread::wait (200 );
143
+ }
144
+ }
145
+
146
+ int main () {
147
+ pb1.mode (PullUp);
148
+ pb2.mode (PullUp);
149
+ pb3.mode (PullUp);
150
+
151
+ uLCD.locate (0 ,0 );
152
+ uLCD.printf (" Alarm System Menu" );
153
+ uLCD.locate (0 ,3 );
154
+ uLCD.printf (" 1: Boot System" );
155
+ uLCD.locate (0 ,5 );
156
+ uLCD.printf (" 2: How to Guide" );
157
+
158
+ while (1 ) {
159
+ if (pb1 == 0 ) {
160
+ uLCD.cls ();
161
+ break ;
162
+ } if (pb2 == 0 ) {
163
+ uLCD.cls ();
164
+ uLCD.locate (0 ,0 );
165
+ uLCD.printf (" Connect via BT" );
166
+ uLCD.locate (0 ,3 );
167
+ uLCD.printf (" '1' to arm" );
168
+ uLCD.locate (0 ,5 );
169
+ uLCD.printf (" '2' to disarm" );
170
+ uLCD.locate (0 ,7 );
171
+ uLCD.printf (" 'PB1 to exit" );
172
+ }
173
+ }
174
+
175
+ // Begin the threads
176
+ thread1.start (temp_thread);
177
+ thread2.start (accel_thread);
178
+ thread3.start (sonar_thread);
179
+ thread4.start (speaker_thread);
180
+ thread5.start (checker_thread);
181
+ thread6.start (camera_thread);
182
+
183
+ char bnum=0 ;
184
+ char bhit=0 ;
185
+ while (1 ) {
186
+ m.lock ();
187
+ // Connect with the Bluetooth Module
188
+ if (blue.readable ()) {
189
+ if (blue.getc ()==' !' ) {
190
+ if (blue.getc ()==' B' ) {
191
+ button data packet bnum = blue.getc ();
192
+ button number bhit = blue.getc ();
193
+ // 1=hit, 0=release
194
+
195
+ switch (bnum) {
196
+ case ' 1' : // Arm the system
197
+ if (bhit==' 1' ) {
198
+ arm = true ;
199
+ pin = 0 ;
200
+ led1 = 1 ;
201
+ } break ;
202
+ case ' 2' : // Disarm the system
203
+ if (bhit==' 1' ) {
204
+ arm = false ;
205
+ led1 = 0 ;
206
+ } break ;
207
+ case ' 3' : // Email for help
208
+ if (bhit = ' 1' ) {
209
+ } break ;
210
+ default : break ;
211
+ }
212
+ }
213
+ }
214
+ }
215
+ m.unlock ();
216
+ Thread::wait (100 );
217
+ }
218
+ }
0 commit comments