-
Notifications
You must be signed in to change notification settings - Fork 0
/
Energia_Project_01.ino
307 lines (269 loc) · 9.52 KB
/
Energia_Project_01.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
/*
Semester Project (Ambulator)
------------------------------------------------------------------
This is C language program that controls the ball screw mechanism
for various coditions of Tidal Volume, Respiratory Rate, And
Inhale Exhale Ratio (IE Ratio), Allowing the user to select 27
diiferent configurations for pressing AMBU bag.
------------------------------------------------------------------
Written by:
Hurraira Adrees (2021-MC-01)
Muhammad Saad Yaseen (2021-MC-37)
Muhammad Rafay (2021-MC-38)
Muhammad Mujahid Iqbal (2021-MC-39)
Abiam Asif Khalid (2021-MC-40)
Compiler and IDE: Energia (A modified version of Ardiuno for Tiva C series Launchpad Kit)
*/
#include <LiquidCrystal.h> //Library for LCD module
int Dir = 2; //Direction Bit of Motor (PB5)
int Step = 3; //Steps for motor (PB0)
// On Board LED
#define LEDR RED_LED //Onboard RED led
#define LEDB BLUE_LED //Onboard BLUE led
#define LEDG GREEN_LED //Onboard GREEN led
//For External LED's
#define RS PB_1 //Register Selector
#define EN PD_0 //Enabe Pin of LCD
#define D4 PD_1 //Data Pin 4
#define D5 PD_2 //Data Pin 5
#define D6 PD_3 //Data Pin 6
#define D7 PE_1 //Data Pin 7
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); //Initialising LCD 1602
int Steps = 0; //Defines Steps for Tidal Volume
int stepDelay = 0; //Time gap between each step
int Inhale = 0; //Inhale Part of cycle
int Exhale = 0; //Exhale Part of cycle
int stopAmbu =0 ; //For Resetting
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(PB_2, INPUT_PULLUP); //Tidal Volume of 200
pinMode(PC_4, INPUT_PULLUP); //Tidal Volume of 300
pinMode(PA_2, INPUT_PULLUP); //Tidal Volume of 400
pinMode(PE_0, INPUT_PULLUP); //Breath Cycle of 08 pm
pinMode(PC_5, INPUT_PULLUP); //Breath Cycle of 12 pm
pinMode(PC_7, INPUT_PULLUP); //Breath Cycle of 16 pm
pinMode(PB_3, INPUT_PULLUP); //Inhale Exhale 1:2
pinMode(PC_6, INPUT_PULLUP); //Inhale Exhale 1:3
pinMode(PA_4, INPUT_PULLUP); //Inhale Exhale 1:4
pinMode(PA_3, INPUT_PULLUP); //Reset
pinMode(Step, OUTPUT); //Step to Motor Driver
pinMode(Dir, OUTPUT); //Direction To Motor Driver
pinMode(LEDR, OUTPUT); //RED led
pinMode(LEDB, OUTPUT); //BLUE led
pinMode(LEDG, OUTPUT); //GREEN led
lcd.begin(16, 2); //Starting LCD
lcd.setCursor(3, 0); //Placing Cursor
lcd.print("AMBULATOR"); //Display Text
delay(3500);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
if (Steps == 0) //Red LED Will Be On Until Tidal Volume Is Selected
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, LOW);
digitalWrite(LEDG, LOW);
lcd.setCursor(0, 0); //Placing Cursor
lcd.print("Tidal Volume: "); //Display Text
delay(500);
}
if ((digitalRead(PB_2) == LOW) && (Steps==0)) //Tidal Volume Corresponding to 200
{
Steps = 20480; //3.2 revolutions = 3.2*6400 = 20480
lcd.setCursor(6, 1); //Placing Cursor
lcd.print("200"); //Display Text
delay(1000);
lcd.clear();
}
if ((digitalRead(PC_4) == LOW) && (Steps==0)) //Tidal Volume Corresponding of 300
{
Steps = 30720; //4.8 revolutions = 4.8*6400 = 30720
lcd.setCursor(6, 1); //Placing Cursor
lcd.print("300"); //Display Text
delay(1000);
lcd.clear();
}
if ((digitalRead(PA_2) == LOW) && (Steps==0)) //Tidal Volume Correspinding of 400
{
Steps = 40960; //6.4 revolutions = 6.4*6400 = 40960
lcd.setCursor(6, 1); //Placing Cursor
lcd.print("400"); //Display Text
delay(1000);
lcd.clear();
}
if ((Steps != 0) && (stepDelay==0)) //Blue LED Will Be On Until Breaths Per Minute Is Selected
{
digitalWrite(LEDB, HIGH);
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
lcd.setCursor(0, 0); //Placing Cursor
lcd.print("Breaths PM: "); //Display Text
delay(500);
}
if ((digitalRead(PE_0) == LOW) && (stepDelay==0) && (Steps != 0)) //Based on the previous choice the Delay will be selected
{ //for 8 Breath Per Minute
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("08"); //Display Text
delay(1000);
lcd.clear();
switch (Steps) //8 BPM = 7.5seconds for each breath
{ //Delay for each breath = 7.5/(2*Steps)
case 20480: //For Tidal Volume 200
stepDelay = 184;
break;
case 30720: //For Tidal Volume 300
stepDelay = 122;
break;
case 40960: //For Tidal Volume 200
stepDelay = 92;
break;
default:
break;
}
}
if ((digitalRead(PC_5) == LOW) && (stepDelay==0) && (Steps != 0)) //Based on the previous choice the Delay will be selected
{ //for 12 Breath Per Minute
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("12"); //Display Text
delay(1000);
lcd.clear();
switch (Steps) //12 BPM = 5 seconds for each breath
{ //Delay for each breath = 5/(2*Steps)
case 20480: //For Tidal Volume 200
stepDelay = 122;
break;
case 30720: //For Tidal Volume 300
stepDelay = 82;
break;
case 40960: //For Tidal Volume 400
stepDelay = 62;
break;
default:
break;
}
}
if ((digitalRead(PC_7) == LOW) && (stepDelay==0) && (Steps != 0)) //Based on the previous choice the Delay will be selected
{ //for 16 Breath Per Minute
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("16"); //Display Text
delay(1000);
lcd.clear();
switch (Steps) //16 BPM = 3.75 seconds for each breath
{ //Delay for each breath = 3.75/(2*Steps)
case 20480: //For Tidal Volume 200
stepDelay = 92;
break;
case 30720: //For Tidal Volume 300
stepDelay = 62;
break;
case 40960: //For Tidal Volume 400
stepDelay = 46;
break;
default:
break;
}
}
if ((stepDelay !=0)&&(Steps !=0)) //Green LED Will Be On Until Tidal Volume Is Selected
{
lcd.setCursor(0, 0); //Placing Cursor
lcd.print("IE Ratio: "); //Display Text
delay(500);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDR, LOW);
digitalWrite(LEDB, LOW);
}
if ((digitalRead(PB_3)==LOW) && (Inhale==0) && (stepDelay !=0)) //Divison of breath in Inhale And Exhale Part Ratio 1:2
{
Inhale = stepDelay/3; //Inhale part = Delay/(1+2)
Exhale = Inhale*2; //EXhale part = Inhale*2
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("1:2"); //Display Text
delay(1000);
lcd.clear();
}
if ((digitalRead(PC_6)==LOW) && (Inhale==0) && (stepDelay !=0)) //Divison of breath in Inhale And Exhale Part Ratio 1:3
{
Inhale = stepDelay/4; //Inhale part = Delay/(1+3)
Exhale = Inhale*3; //EXhale part = Inhale*3
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("1:3"); //Display Text
delay(1000);
lcd.clear();
}
if ((digitalRead(PA_4)==LOW)&& (Inhale==0) && (stepDelay !=0)) //Divison of breath in Inhale And Exhale Part Ratio 1:4
{
Inhale = stepDelay/5; //Inhale part = Delay/(1+4)
Exhale = Inhale*4; //EXhale part = Inhale*4
lcd.setCursor(7, 1); //Placing Cursor
lcd.print("1:4"); //Display Text
delay(1000);
lcd.clear();
}
if (digitalRead(PA_3) == LOW) //Reset Condition for All Variables
{
delay(1000);
Steps = 0;
stopAmbu = 0;
stepDelay = 0;
Inhale = 0;
Exhale = 0;
lcd.begin(16, 2); //Starting LCD
lcd.setCursor(3, 0); //Placing Cursor
lcd.print("AMBULATOR"); //Display Text
delay(1000);
lcd.clear();
}
while ((Steps != 0)&&(stepDelay != 0)&&(Inhale != 0))
{
if (Inhale < 20)
{
Inhale = 20;
}
digitalWrite(LEDG, HIGH); //Purple Colour for inhale
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, LOW);
digitalWrite(Dir, HIGH); //Inhale cycle has High Direction
for (int x=0; x< Steps; x++)
{
if (digitalRead(PA_3) == LOW) //Stopping Condition
{
stopAmbu = 1;
lcd.print("Stopping...");
lcd.setCursor(0, 0);
}
digitalWrite(Step, HIGH); //Stepping Part
delayMicroseconds(Inhale);
digitalWrite(Step, LOW);
delayMicroseconds(Inhale);
}
digitalWrite(LEDG, LOW); //Yellow Colour for inhale
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(Dir, LOW); //Exhale cycle has Low Direction
for (int x=0; x< Steps; x++)
{
if (digitalRead(PA_3) == LOW) //Stopping Condition
{
stopAmbu = 1;
lcd.print("Stopping...");
lcd.setCursor(0, 0);
}
digitalWrite(Step, HIGH); //Stepping Part
delayMicroseconds(Exhale);
digitalWrite(Step, LOW);
delayMicroseconds(Exhale);
}
if (stopAmbu == 1) //Resetting Variables after Stopping
{
Steps = 0;
stopAmbu = 0;
stepDelay = 0;
Inhale = 0;
Exhale = 0;
lcd.clear();
break;
}
}
}