-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
204 lines (158 loc) · 5.26 KB
/
main.cpp
File metadata and controls
204 lines (158 loc) · 5.26 KB
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
#include "mbed.h"
#include "TextLCD.h"
// motor pins
#define motor_pinA D7
#define motor_pinB D8
#define pwm_pin D5
// motor shield and potentiometer
#define enspeed_pin A0
#define pot_pin A2
// active buzzer
#define buzzer_pin D9
// lcd display pins
#define rs D12
#define en D11
#define d4 D6
#define d5 D4
#define d6 D3
#define d7 D2
// initialize lcd
TextLCD lcd(rs, en, d4, d5, d6, d7);
// constants to calculate the breaths per minute
const double kConst_1 = 0.4204;
const double kConst_2 = -17.414;
// variable to store potentiometer reading
int speedVal = 0;
// 16 byte buffers for lcd display
char buffer[16];
//set the breaths per minute (BPM) to an initial value of 0
double BPM=0;
// state machine with starting state as STOP
enum StateMachine {STOP,RUN,WARNING,LIMIT} ;
StateMachine state = STOP;
// function to map values from one range to another
int map(int x, int in_min, int in_max, int out_min, int out_max);
int main()
{
// setting up the pin modes
DigitalOut motor_pin1(motor_pinA);
DigitalOut motor_pin2(motor_pinB);
DigitalOut buzzer(buzzer_pin);
PwmOut pwm(pwm_pin);
DigitalOut enspeed(enspeed_pin);
AnalogIn pot(pot_pin);
while (true) {
// power the motor
enspeed = 1;
motor_pin1 = 0;
motor_pin2 = 1;
// get potentiometer reading
speedVal = pot.read();
speedVal = map(speedVal,0,4096,0,1023); // match speedVal to pwm
// value of 55 was established after calibrating the ventilator with a tachometer
if (speedVal<55)
{
BPM = 0;
}
else{
BPM = kConst_1*speedVal + kConst_2;
}
// state machine
switch(state)
{
// the motor is initially off
case STOP:
lcd.locate(1,0);
lcd.printf("VENTILATOR 001");
lcd.locate(0, 1);
//print the BPM
sprintf(buffer,"BPM: %3d",(int)BPM);
lcd.printf("%s", buffer);
// switch off motor of speed is below the minimum (i.e., 55)
if(speedVal<55)
{
enspeed = 0;
motor_pin1 = 0;
pwm.period(0.001);
pwm.write(0);
}
// switch state to run if speed is increased
if (speedVal>=55 && speedVal<120)
{
state=RUN;
}
break;
case RUN:
lcd.locate(1,0);
lcd.printf("VENTILATOR 001");
pwm.period(0.0001);
pwm.write(speedVal/1024);
sprintf(buffer,"BPM:%3d",(int)BPM);
// sound buzzer
buzzer = 1;
wait_us(1000000);
lcd.locate(0, 1);
lcd.printf("%s", buffer);
// if speed is too high, switch to state "WARNING"
if(speedVal>=120 && speedVal<160)
{
state=WARNING;
}
//if speed falls below 55, switch to state "STOP"
if(speedVal<55)
{
state=STOP;
}
break;
case WARNING:
lcd.locate(1,0);
lcd.printf("VENTILATOR 001");
pwm.period(0.0001);
pwm.write(speedVal/1024);
sprintf(buffer,"BPM:%3d",(int)BPM);
buzzer = 1;
wait_us(1000000);
lcd.locate(0, 1);
lcd.printf("%s", buffer);
lcd.locate (8,1);
lcd.printf("WARNING!");
// if speed is too high, switch to state "LIMIT'
if(speedVal>=160)
{
state=LIMIT;
}
// if speed is reduced, switch to state "RUN"
if(speedVal<120 && speedVal>=55)
{
lcd.cls();
state=RUN;
}
break;
case LIMIT:
//if speed is equal or exceeds 160, turn off the motor
if(speedVal>=160)
{
buzzer = 1;
wait_us(1000000);
pwm.period(0.0001);
pwm.write(0);
lcd.cls();
lcd.locate(2,0);
lcd.printf("REDUCE SPEED");
lcd.locate(0,1);
lcd.printf( "DANGEROUS BPM!!");
wait_us(1000000);
}
// if speed is reduced, switch to state "WARNING" and run the motor
if(speedVal<160 && speedVal>=120)
{
lcd.cls();
state=WARNING;
}
break;
}
}
}
int map(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}