-
Notifications
You must be signed in to change notification settings - Fork 1
/
pacetracker.cpp
152 lines (113 loc) · 2.89 KB
/
pacetracker.cpp
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
#include "mbed.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx
AnalogIn a1(p18); //
Serial xbee1(p9, p10);
DigitalOut rst1(p21);
Timer timer;
int s_count;
float t;
int flag;
int curr_pace, prev_pace;
void program_select()
{
int time;
int program;
int num;
xbee1.printf("P\n");
wait(0.5);
flag = 1;
//ee
pc.printf("Welcome to Program Select\r\nChoose from the following speeds\r\n");
pc.printf("Slow (80 bps): 1 tap\r\nMedium (120 bps): 2 taps\r\nFast (150 bps): 3 taps\r\n");
while(a1.read()<0.5)
wait(0.2);
timer.reset();
timer.reset();
flag =1;
num = 0;
while (timer.read() < 2) {
wait(0.05);
if (a1.read()>0.5) {
if (flag == 0) {
num++;
flag = 1;
}
}
if (a1.read()<0.1)
flag = 0;
}
program = num + 1;
if (program > 3)
program = 3;
pc.printf("You have selected program %d\r\n", program);
xbee1.printf("%d\n", program);
timer.reset();
}
int detect_speed()
{
int num = 0;
float step = 0.00;
float prev_step = -10.00;
int pace;
flag = 1;
timer.reset();
timer.start();
while (num<4) {
/* Reset timer if above 15 seconds*/
if (timer.read() > 15) {
while (a1.read()<0.5)
wait(0.2);
timer.reset();
timer.start();
flag = 1;
num = 0;
}
wait(0.05);
if (a1.read()>0.5) { // Step sensed
if (flag == 0) {
if (num>0)
prev_step = step;
step = timer.read();
/* Switch to Program Select*/
if (step - prev_step < 0.4) { // Quick 2-step signals Program Select
program_select();
return 0;
}
num++;
flag++;
s_count++;
pc.printf("%f\r\n",step);
}
}
if (a1.read()<0.01) { // Clear flag
flag = 0;
}
}
pace = 8/(step/60);
pc.printf("Pace:%d bpm\r\n",pace,s_count);
return pace;
}
int main()
{
rst1 = 0;
wait_ms(1);
rst1 = 1;
s_count= 0;
/* Step once to begin*/
while (a1.read()<0.5)
wait(0.2); // Won't send anything until sensor is pressed at least once!
timer.reset();
timer.start();
while(1) {
prev_pace = curr_pace;
curr_pace = detect_speed();
s_count+=10;
if (abs(curr_pace-prev_pace) <= 15)
xbee1.printf("%d %d\r\n", curr_pace, s_count);
}
return 0;
}