forked from CSSALTlab/Open_Source_Ventilator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab8.c
More file actions
executable file
·153 lines (144 loc) · 5.61 KB
/
Copy pathlab8.c
File metadata and controls
executable file
·153 lines (144 loc) · 5.61 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
/******************************************************************************
Stephanie Lampotang - March 19, 2020
Open Source Contribution for Free Distribution
University of Southern California
Computer Science
Equipment: Arduino Uno, LCD display, wires, buttons, breadboard
Link to purchase:
*******************************************************************************/
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "lcd.h"
volatile int bpm_chosen; // current breaths per minute that system is operating at
volatile int inhale_time; // amount of time in 1/100 secs that the inhale valve should remain open
volatile int exhale_time; // amount of time in 1/100 secs that the exhale valve should remain open
volatile int inhale_complete; // flag signifying the end of an inhale
volatile int exhale_complete; // flag signifying the end of an exhale
volatile int secs; // hundredths of seconds that have passed in this inhale or exhale
volatile int inhale; // true if system is in the inhale state of the breath, false if in the exhale state of the breath
volatile int exhale_to_inhale; // ration of the duration of the exhale to the duration of the inhale
int main(void)
{
// Initialize the LCD
lcd_init();
// Write splash screen
lcd_writecommand(1);
lcd_stringout("fight me covid19 :(");
// breaths per minute from 10 - 30 in increments of 5
int bpms[5] = {10, 15, 20, 25, 30};
// tidal volumes in milliliters from 450 - 800 in increments of 50
int tidal_volumes[8] = {450, 500, 550, 600, 650, 700, 750, 800};
_delay_ms(2000);
lcd_writecommand(1);
// set up the default parameters
int bpm_index = 0;
int vt_index = 5;
int vt_chosen = tidal_volumes[vt_index];
exhale_to_inhale = 2;
// SET parameters
bpm_chosen = bpms[bpm_index];
// times are in tenths of a second, so 600 represents 60 secs or 1 min
inhale_time = 600/(bpm_chosen)/(exhale_to_inhale+1);
exhale_time = 600/(bpm_chosen)*(exhale_to_inhale)/(exhale_to_inhale+1);
inhale_complete = 0;
exhale_complete = 0;
secs = 0;
inhale = 1;
char bpm_string[17];
snprintf(bpm_string, 17, "bpm: %d, etoi: %d", bpm_chosen, exhale_to_inhale);
lcd_stringout(bpm_string);
// Initializing pull-up resistors on PC5, PC4, and PC3 of the arduino
PORTC |= ((1 << PC5) | (1 << PC4) | (1 << PC3));
// Set up interrupts for every 1/100th of a second
TCCR1B |= (1 << WGM12);
TIMSK1 |= (1 << OCIE1A);
OCR1A = 6250;
TCCR1B |= (1 << CS12);
// turn on interrupts
sei();
while (1)
{ // Loop forever
if ((PINC & (1 << PC5)) == 0) { // button pressed -> recalculate
// debouncing code
_delay_ms(5);
while ((PINC & (1 << PC5)) == 0) {}
bpm_index++;
if (bpm_index >= 5) { // goes out of bounds -> loop around
bpm_index = 0;
}
// RESET parameters
bpm_chosen = bpms[bpm_index];
// times are in tenths of a second, so 600 represents 60 secs or 1 min
inhale_time = 600/(bpm_chosen)/(exhale_to_inhale+1);
exhale_time = 600/(bpm_chosen)*(exhale_to_inhale)/(exhale_to_inhale+1);
inhale_complete = 0;
exhale_complete = 0;
secs = 0;
inhale = 1;
snprintf(bpm_string, 17, "bpm: %d, etoi: %d", bpm_chosen, exhale_to_inhale);
lcd_stringout(bpm_string);
}
if ((PINC & (1 << PC4)) == 0) { // button pressed -> switch exhale_to_inhale
_delay_ms(5);
while ((PINC & (1 << PC4)) == 0) {}
if (exhale_to_inhale == 2) {
exhale_to_inhale = 1;
} else if (exhale_to_inhale == 1) {
exhale_to_inhale = 2;
}
// RESET parameters
bpm_chosen = bpms[bpm_index];
// times are in tenths of a second, so 600 represents 60 secs or 1 min
inhale_time = 600/(bpm_chosen)/(exhale_to_inhale+1);
exhale_time = 600/(bpm_chosen)*(exhale_to_inhale)/(exhale_to_inhale+1);
inhale_complete = 0;
exhale_complete = 0;
secs = 0;
inhale = 1;
snprintf(bpm_string, 17, "bpm: %d, etoi: %d", bpm_chosen, exhale_to_inhale);
lcd_stringout(bpm_string);
}
if ((PINC & (1 << PC3)) == 0) { // button pressed -> increase tidal volume by 50 mL
_delay_ms(5);
while ((PINC & (1 << PC3)) == 0) {}
vt_index++;
if (vt_index >= 8) { // goes out of bounds -> loop around
vt_index = 0;
}
// print the change in tidal volume
char vt_string[8];
snprintf(vt_string, 8, "vt: %d", vt_chosen);
lcd_moveto(1, 7);
lcd_stringout(vt_string);
}
if (inhale_complete) { // inhale is complete -> switch to exhale state
lcd_moveto(1, 0);
lcd_stringout("EXHALE");
inhale_complete = 0;
secs = 0;
inhale = 0;
} else if (exhale_complete) { // exhale is complete -> switch to inhale state
lcd_moveto(1, 0);
lcd_stringout("INHALE");
exhale_complete = 0;
secs = 0;
inhale = 1;
}
}
return 0; /* never reached */
}
ISR(TIMER1_COMPA_vect) {
secs++;
if (inhale) { // inhaling
if (secs >= inhale_time) {
inhale_complete = 1;
}
} else { // must be exhaling
if (secs >= exhale_time) {
exhale_complete = 1;
}
}
}