-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart_Watch.ino
382 lines (345 loc) · 18.2 KB
/
Smart_Watch.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Adding Libraries
#include <Wire.h>
#include <SPI.h>
#include <SimpleKalmanFilter.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MPU6050.h>
// Display Properties
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
long refresh_time = 0;
// Intialized Kalman Filter
SimpleKalmanFilter kalmanFilter(2.5, 2.5, 0.1);
// MPU6050 Properties
MPU6050 mpu;
const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ, Temp = 0.0;
float AccFX, AccFY, AccFZ = 0.0;
float AccErrorX, AccErrorY, AccErrorZ = 0.0;
int c = 0;
// Steps Detector Properties
int stepThreshold = 1.8;
boolean isPassedThreshold = false;
long passedThresholdTimestamp = 0;
int steps = 0;
boolean walking = false;
// Display Values Properties
#define interval1 1000
#define interval2 2000
#define interval3 1000
#define interval4 2000
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
unsigned long previousMillis = 0;
unsigned long curtime = 0;
unsigned long prvtime = 0;
unsigned long delaytime = 30;
int count = 0;
// Bitmaps
const unsigned char onScreenBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x3e, 0x00, 0x00,
0x00, 0x00, 0xf0, 0x1f, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x03, 0xc1, 0xff, 0xff, 0x83, 0xc0, 0x00,
0x00, 0x07, 0x0f, 0xc1, 0x83, 0xf0, 0xe0, 0x00, 0x00, 0x1c, 0x3e, 0x01, 0x80, 0x7c, 0x38, 0x00,
0x00, 0x38, 0x78, 0x01, 0x80, 0x1e, 0x1c, 0x00, 0x00, 0x71, 0xf0, 0x00, 0x00, 0x0f, 0x0e, 0x00,
0x00, 0xe3, 0xb8, 0x00, 0x00, 0x18, 0x07, 0x00, 0x01, 0xc7, 0x18, 0x03, 0xc0, 0x18, 0x63, 0x80,
0x01, 0x8e, 0x00, 0x07, 0xe0, 0x00, 0x71, 0x80, 0x03, 0x1c, 0x00, 0x06, 0x60, 0x00, 0x38, 0xc0,
0x06, 0x38, 0x00, 0x06, 0x60, 0x00, 0x18, 0x60, 0x06, 0x70, 0x00, 0x06, 0x60, 0x00, 0x00, 0x60,
0x0c, 0x60, 0x00, 0x06, 0x60, 0x00, 0x02, 0x30, 0x1c, 0xc0, 0x00, 0x06, 0x60, 0x00, 0x03, 0x38,
0x19, 0xe0, 0x00, 0x06, 0x60, 0x00, 0x03, 0x98, 0x19, 0xf0, 0x00, 0x06, 0x60, 0x00, 0x0f, 0x98,
0x33, 0xb0, 0x00, 0x06, 0x60, 0x00, 0x0d, 0xcc, 0x33, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0xcc,
0x63, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0xc6, 0x66, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x66,
0x66, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x66,
0xc4, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x23, 0xcc, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x33,
0xcc, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x33, 0xcc, 0x00, 0x00, 0x03, 0xc3, 0xff, 0x80, 0x33,
0xcc, 0x00, 0x00, 0x07, 0xe7, 0xff, 0xc0, 0x33, 0xcf, 0x80, 0x00, 0x06, 0x7e, 0x00, 0xc1, 0xf3,
0xcf, 0x80, 0x00, 0x06, 0x7e, 0x00, 0xc1, 0xf3, 0xcc, 0x00, 0x00, 0x07, 0xe7, 0xff, 0xc0, 0x33,
0xcc, 0x00, 0x00, 0x03, 0xc3, 0xff, 0x80, 0x33, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33,
0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23,
0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6,
0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x31, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xcc,
0x19, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x98, 0x18, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0x98,
0x1c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x38, 0x0c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x06, 0x30,
0x06, 0x70, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x60, 0x06, 0x38, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x60,
0x03, 0x18, 0x00, 0x00, 0x00, 0x00, 0x38, 0xc0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x71, 0x80,
0x01, 0xc3, 0x18, 0x00, 0x00, 0x18, 0xe3, 0x80, 0x00, 0xe3, 0xb8, 0x00, 0x00, 0x1d, 0xc7, 0x00,
0x00, 0x71, 0xf0, 0x00, 0x00, 0x0f, 0x8e, 0x00, 0x00, 0x38, 0x78, 0x01, 0x80, 0x1e, 0x1c, 0x00,
0x00, 0x1c, 0x3e, 0x01, 0x80, 0x7c, 0x38, 0x00, 0x00, 0x07, 0x0f, 0xc1, 0x83, 0xf0, 0xe0, 0x00,
0x00, 0x03, 0xc1, 0xff, 0xff, 0x83, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0x1f, 0xf8, 0x0f, 0x00, 0x00,
0x00, 0x00, 0x7c, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00,
0x00, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00
};
const unsigned char tempBitmap [] PROGMEM = {
0x00, 0x00, 0x30, 0x00, 0x78, 0x1e, 0xcc, 0x7f, 0x8c, 0xf3, 0xcd, 0xc0, 0x79, 0xc0, 0x01, 0x80,
0x03, 0x80, 0x01, 0x80, 0x01, 0xc0, 0x01, 0xe0, 0x00, 0xff, 0x00, 0x7f, 0x00, 0x1c, 0x00, 0x00
};
const unsigned char stepsBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x18, 0x00, 0x3c, 0x00, 0x3e, 0x1e, 0x7e, 0x1f, 0x7e, 0x3f, 0x7e, 0x3f, 0x7c,
0x3e, 0x7c, 0x3c, 0x48, 0x3c, 0x78, 0x08, 0x70, 0x78, 0x70, 0x78, 0x00, 0x38, 0x00, 0x00, 0x00
};
const unsigned char walkBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0x03, 0xff, 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0x80, 0x00, 0x00,
0x00, 0x00, 0xff, 0x9f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0xff, 0x03, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x03, 0xfc, 0x01, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xf8, 0x03, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x03, 0xf0, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x07, 0xff, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xff, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0x10, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1f, 0xfe, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfe, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0x7f, 0xfe, 0x00,
0x00, 0x00, 0x00, 0x7f, 0xf8, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf8, 0x3f, 0xff, 0x00,
0x00, 0x00, 0x00, 0xff, 0xf0, 0x3f, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf0, 0x1f, 0xfe, 0x00,
0x00, 0x00, 0x00, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x1f, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x0f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x83, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xe0, 0xff, 0xe0, 0x00, 0x00, 0x01, 0xff, 0xff, 0xe0, 0x7f, 0xe0, 0x00, 0x00,
0x01, 0xff, 0xff, 0xc0, 0x1f, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0x80, 0x0f, 0xe0, 0x00, 0x00,
0x03, 0xff, 0xff, 0x80, 0x0f, 0xe0, 0x00, 0x00, 0x01, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0x00, 0x00,
0x00, 0xff, 0xfc, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char alarmBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x06, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xf0, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x0f, 0xe0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x1f, 0xf8, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x87, 0xe0, 0x00,
0x00, 0x00, 0x07, 0xff, 0xff, 0xe1, 0xc0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x40, 0x00,
0x00, 0x00, 0x7f, 0xe3, 0xc7, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xc0, 0xff, 0x00, 0x00,
0x00, 0x01, 0xfc, 0x03, 0xc0, 0x3f, 0x80, 0x00, 0x00, 0x03, 0xf0, 0x01, 0x80, 0x0f, 0xc0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x01, 0xf0, 0x00,
0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00,
0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0xf9, 0xc0, 0x00, 0x00, 0x03, 0x9f, 0x00,
0x00, 0xf3, 0xf0, 0x00, 0x00, 0x0f, 0xcf, 0x00, 0x01, 0xf3, 0xf8, 0x00, 0x00, 0x1f, 0xcf, 0x80,
0x01, 0xe1, 0xfe, 0x00, 0x00, 0x7f, 0x87, 0x80, 0x01, 0xe0, 0x7f, 0x80, 0x01, 0xfe, 0x07, 0x80,
0x01, 0xe0, 0x3f, 0xc0, 0x03, 0xfc, 0x07, 0x80, 0x03, 0xc0, 0x0f, 0xf0, 0x0f, 0xf0, 0x03, 0xc0,
0x03, 0xc0, 0x03, 0xfc, 0x3f, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x00, 0xff, 0xff, 0x00, 0x03, 0xc0,
0x03, 0xf8, 0x00, 0x7f, 0xfe, 0x00, 0x1f, 0xc0, 0x03, 0xfc, 0x00, 0x1f, 0xf8, 0x00, 0x3f, 0xc0,
0x03, 0xfc, 0x00, 0x07, 0xe0, 0x00, 0x3f, 0xc0, 0x03, 0xf8, 0x00, 0x01, 0x80, 0x00, 0x1f, 0xc0,
0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0,
0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80,
0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80,
0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,
0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00,
0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00,
0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00,
0x00, 0x0f, 0x80, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00,
0x00, 0x03, 0xf0, 0x01, 0x80, 0x0f, 0xc0, 0x00, 0x00, 0x01, 0xfc, 0x03, 0xc0, 0x3f, 0x80, 0x00,
0x00, 0x00, 0xff, 0x03, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe3, 0xc7, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(19200); // Make sure Serial Monitor or Serial Plotter set to this Value
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
}
if(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a MPU6050 sensor");
}
Wire.begin(); // Initialize comunication
Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); // end the transmission
// Call this function if you need to get the IMU error values for your module
calculate_IMU_error();
delay(50);
display.clearDisplay();
display.drawBitmap(32, 0, onScreenBitmap, 64, 64, WHITE);
display.display();
delay(3000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(50, 25);
display.println("BE");
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(35, 25);
display.println("SMART");
display.display();
delay(1500);
/*
// Configure Accelerometer Sensitivity - Full Scale Range (default +/- 2g)
Wire.beginTransmission(MPU);
Wire.write(0x1C); //Talk to the ACCEL_CONFIG register (1C hex)
Wire.write(0x10); //Set the register bits as 00010000 (+/- 8g full scale range)
Wire.endTransmission(true);
// Configure Gyro Sensitivity - Full Scale Range (default +/- 250deg/s)
Wire.beginTransmission(MPU);
Wire.write(0x1B); // Talk to the GYRO_CONFIG register (1B hex)
Wire.write(0x10); // Set the register bits as 00010000 (1000deg/s full scale)
Wire.endTransmission(true);
delay(20);
*/
}
void loop() {
//Read acceleromter data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // Read 6 registers total, each axis value is stored in 2 registers
//For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis value
AccY = (Wire.read() << 8 | Wire.read()) / 16384.0; // Y-axis value
AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0; // Z-axis value
Temp = mpu.readTemperature();
// AccFX = AccX - AccErrorX; // AccErrorX See the calculate_IMU_error()custom function for more details
// AccFY = AccY - AccErrorY; // AccErrorY See the calculate_IMU_error()custom function for more details
AccFZ = AccZ - AccErrorZ; // AccErrorZ See the calculate_IMU_error()custom function for more details
float filtered_value = kalmanFilter.updateEstimate(AccFZ); // Used kalman filter for filtering input data (Redused input data noices)
// Serial.print(" X: ");
// Serial.println(AccFX);
// Serial.print(" Y: ");
// Serial.println(AccFY);
// Serial.print(" Z: ");
// Serial.println(AccFZ);
// Serial.print(" Temp: ");
// Serial.println(Temp);
Serial.print("Filtered: ");
Serial.println(filtered_value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Steps detector
if(filtered_value > stepThreshold) {
if(! isPassedThreshold) {
isPassedThreshold = true;
passedThresholdTimestamp = millis();
steps += 1;
walking = true;
// Serial.println("Walking");
}
}
else{
if(isPassedThreshold && millis() - passedThresholdTimestamp < 2000){
isPassedThreshold = false;
}
else if (! isPassedThreshold && millis() - passedThresholdTimestamp > 1000){
isPassedThreshold = false;
walking = false;
// Serial.println("Not Walking");
}
}
// Serial.print("Steps :");
// Serial.println(steps);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(walking){
// Serial.println("walking");
count = 0;
if(millis() - previousMillis > 1000){
previousMillis = millis();
display.clearDisplay();
display.drawBitmap(32, 0, walkBitmap, 64, 64, WHITE);
display.display();
}
}
else{
while(!walking){
walking = true;
curtime = millis();
if(curtime - prvtime > 1000){
prvtime = curtime;
count ++;
}
if( count <= delaytime){
// Serial.println("you can Rest");
if(millis() >= time1 + interval1){
time1 += interval1;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.drawBitmap(56, 0, tempBitmap, 16, 16, WHITE);
display.print("Temp: ");
display.println(Temp, 1);
display.display();
}
if(millis() >= time2 + interval2){
time2 += interval2;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 30);
display.drawBitmap(56, 0, stepsBitmap, 16, 16, WHITE);
display.print("Steps: ");
display.println(steps);
display.display();
}
}
else{
// Serial.println("you have to walking");
if(millis() >= time1 + interval1){
time1 += interval1;
display.clearDisplay();
display.drawBitmap(32, 0, alarmBitmap, 64, 64, WHITE);
display.display();
}
if(millis() >= time2 + interval2){
time2 += interval2;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 30);
display.print("Times Up");
display.display();
}
}
}
}
} // end loop
void calculate_IMU_error() {
// We can call this funtion in the setup section to calculate the accelerometer data error. From here we will get the error values used in the above equations printed on the Serial Monitor.
// Note that we should place the IMU flat in order to get the proper values, so that we then can the correct values
// Read accelerometer values 200 times
while (c < 200) {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
AccY = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
// Sum all readings
// AccErrorX = AccErrorX + AccX;
// AccErrorY = AccErrorY + AccY;
AccErrorZ = AccErrorZ + AccZ;
c++;
}
//Divide the sum by 500 to get the error value
// AccErrorX = AccErrorX / 200;
// AccErrorY = AccErrorY / 200;
AccErrorZ = AccErrorZ / 200;
c = 0;
// Serial.print("AccErrorX: ");
// Serial.println(AccErrorX);
// Serial.print("AccErrorY: ");
// Serial.println(AccErrorY);
// Serial.print("AccErrorZ: ");
// Serial.println(AccErrorZ);
}