-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitial_Code.ino
368 lines (263 loc) · 10.8 KB
/
Initial_Code.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
//Spirometer testing v3
//___________________________
//Libraries
//SD card library (included in Arduino IDE)
//SD card library requires digital pins 10, 11, 12, 13
#include <SD.h>
//I2C communications library (included in Arduino IDE)
#include <Wire.h>
//Real-time clock library (uses Wire.h, from https://github.com/adafruit/RTClib)
//Set RTC clock via https://learn.adafruit.com/adafruit-pcf8523-real-time-clock/rtc-with-circuitpython?view=all#usage)
#include <RTClib.h>
//___________________________
//Definitions
//Empty data logger file
File logFile;
//Real Time Clock object
RTC_Millis rtc;
//Minimum time for applied voltage to flow sensor before readings are valid (30ms)
const int warmupFlowTime = 30;
//Minimum time for applied voltage to O2 sensor before readings are valid (20min = 1200000ms = 300000ms * 4 breaks )
const long warmupO2Time = 300000;
//Interval between readings (ms)
const int readingInterval = 0;
//Empty variable for analog readings
int flowAnalog;
int o2Analog;
//Empty variables for time (milliseconds)
unsigned long startTime = millis(); //Logger start time since power on
unsigned long readingTime; //Time of each reading
unsigned long endTime; //Logger stop time since power on
//Debouncing of button variables
int buttonVal = 0; //Store current button state
int buttonOldVal = 0; //Store previous button state
int buttonState = 0; //0 = 'dormant state', 1 = 'data logging state'
//___________________________
//Pin Definitions
//Analog (for reference, not needed in code)
//O2 sensor read
//airO2Pin = A2;
//Flow sensor read
//airFlowPin = A3;
//Real-time clock (I2C connection)
//serialDataPin = A4;
//serialClockPin = A5;
//Digital
//Button input
int startSwitchPin = 5;
//Flow sensor voltage supply
int flowSupplyPin = 4;
//O2 sensor voltage supply
int o2SupplyPin = 3;
//LEDs
int indicatorLedPin = 8;
int powerLedPin = 6;
//SD detector pin
int SdPresencePin = 10;
//___________________________
//Setup and initialization
void setup(){
//Start serial communication through at 115200 baud (bits/s)
Serial.begin(115200);
//Set led pins to output and off
pinMode(indicatorLedPin, OUTPUT);
digitalWrite(indicatorLedPin, LOW);
pinMode(powerLedPin, OUTPUT);
digitalWrite(powerLedPin, LOW);
//Set button pin to input
pinMode(startSwitchPin, INPUT);
//Set sensor pins to input
pinMode(A2, INPUT);
pinMode(A3, INPUT);
//Set sensor voltage supply pins to output
pinMode(flowSupplyPin, OUTPUT);
pinMode(o2SupplyPin, OUTPUT);
//Initialize RTC
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
//Initialize SD card
Serial.println("Initializing SD card...");
pinMode(SdPresencePin, OUTPUT);
//SD card detection
if (!SD.begin(SdPresencePin)){
Serial.println("No SD Card.");
delay(100);
} else {
Serial.println("SD Card Ready.");
}
//Wait before starting main loop
delay(150);
Serial.println("Waiting for input...");
}
//New SD card file
void newFile(){
//Create new data file
char fileName[] = "logger00.csv";
//Loop to create multiple new files
for (uint8_t i = 0; i < 100; i++){
fileName[6] = i/10 + '0';
fileName[7] = i%10 + '0';
//Open new file only if it doesn't exist
if (!SD.exists(fileName)){
logFile = SD.open(fileName, FILE_WRITE);
break;
}
}
//Check for logger file
if (!logFile){
Serial.println("Could not create file");
} else if (logFile){
Serial.print("Logging to: ");
Serial.println(fileName);
//Write new header if file exists
logFile.println("Date,ReadTime,Millis,FlowAnalog,FlowVoltage,FlowRate,O2Analog,O2Voltage,O2Percent");
}
}
//Main loop
void loop(){
//Read and store button input
buttonVal = digitalRead(startSwitchPin);
//Check for a transition in button
if ((buttonVal == HIGH) && (buttonOldVal == LOW)){
//Change state
buttonState = 1 - buttonState;
//Create and open new data file
newFile();
//Enter 'data logger state'
Serial.println("Logging initiated");
//Wait for minimum warmup time to equilibrate the o2 sensor
digitalWrite(o2SupplyPin, HIGH);
/*digitalWrite(indicatorLedPin, HIGH);
delay(warmupO2Time);
Serial.println("15 minutes left");
delay(warmupO2Time);
Serial.println("10 minutes left");
delay(warmupO2Time);
Serial.println("5 minutes left");
delay(warmupO2Time);*/
//Wait for minimum warmup time to equilibrate the flow sensor
digitalWrite(flowSupplyPin, HIGH);
delay(warmupFlowTime);
//Start Time (rtc reading)
DateTime now = rtc.now();
//Date
logFile.print(now.month(), DEC);
logFile.print('/');
logFile.print(now.day(), DEC);
logFile.print('/');
logFile.print(now.year(), DEC);
logFile.print(',');
//Time
logFile.print(now.hour(), DEC);
logFile.print(':');
logFile.print(now.minute(), DEC);
logFile.print(':');
logFile.print(now.second(), DEC);
logFile.print(',');
//Empty readings
logFile.println(" , , , , , , ");
delay(100);
}
if ((buttonVal == LOW) && (buttonOldVal == HIGH)){
//Change state
buttonState = 1 - buttonState;
//End time (rtc reading)
DateTime now = rtc.now();
//Date
logFile.print(now.month(), DEC);
logFile.print('/');
logFile.print(now.day(), DEC);
logFile.print('/');
logFile.print(now.year(), DEC);
logFile.print(',');
//Time
logFile.print(now.hour(), DEC);
logFile.print(':');
logFile.print(now.minute(), DEC);
logFile.print(':');
logFile.print(now.second(), DEC);
logFile.print(',');
//Empty readings
logFile.println(" , , , , , , ");
//Close data file.
logFile.close();
}
//Button value is old, store it for comparison
buttonOldVal = buttonVal;
if (buttonState == 1){
//Indicator (green) LED solid during data collection
digitalWrite(indicatorLedPin, HIGH);
//Read voltage from sensors
flowAnalog = analogRead(A3);
o2Analog = analogRead(A2);
readingTime = millis();
Serial.print("Reading Time (ms)");
Serial.print(readingTime);
Serial.print("\t");
Serial.print("Flow Analog Read: ");
Serial.print(flowAnalog);
Serial.print("\t");
//Calculate flow voltage from reading [analog * sensor reference voltage]/1023
float flowVolt = flowAnalog * 5.0;
flowVolt /= 1023.0;
Serial.print("Flow Voltage: ");
Serial.print(flowVolt);
Serial.print("\t");
//Calculate air flow rate from voltage in mL/minute for MEMS Flow Sensor D6F-P0001A1 (Bufo breathalyzer 2.0 - 01.31.2024)
float flowRate = 50 * flowVolt - 25;
Serial.print("Flow Rate: ");
Serial.print(flowRate);
Serial.print("\t");
//Calculate o2 voltage from reading [analog * sensor reference voltage]/1023
float o2Volt = o2Analog * 5.0;
o2Volt /= 1023.0;
Serial.print("O2 Analog Read: ");
Serial.print(o2Analog);
Serial.print("\t");
Serial.print("O2 Voltage: ");
Serial.print(o2Volt);
Serial.print("\t");
//Calculate o2 percentage from voltage
float o2Percent = ((o2Volt * 0.21)/2.0)*100;
Serial.print("O2 Percent: ");
Serial.print(o2Percent);
Serial.println("%");
//Write to sd
//Empty Date and ReadTime cells
logFile.print(" , ,");
//Millis
logFile.print(readingTime);
logFile.print(',');
//Flow
//Analog
logFile.print(flowAnalog);
logFile.print(',');
//Voltage
logFile.print(flowVolt);
logFile.print(',');
//Flow
logFile.print(flowRate);
logFile.print(',');
//O2
//Analog
logFile.print(o2Analog);
logFile.print(',');
//Voltage
logFile.print(o2Volt);
logFile.print(',');
//Flow
logFile.println(o2Percent);
//Delay between readings
delay(readingInterval);
} else if (buttonState ==0){
//Enter 'dormant state'
//Turn off power to sensors
digitalWrite(flowSupplyPin, LOW);
digitalWrite(o2SupplyPin, LOW);
digitalWrite(indicatorLedPin, LOW);
//Blink every 1.5s until logger started
digitalWrite(powerLedPin,LOW);
delay(1500);
digitalWrite(powerLedPin, HIGH);
delay(1500);
}
}