-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2.ino
355 lines (269 loc) · 9.16 KB
/
CO2.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
/*
* SIO 190 - CO2 Analyzer
*
* Coded by: Stazia Tronboll && Colin "MrSwirlyEyes" Keef ([email protected])
*
* Version: 1
*/
#include "Pump.h"
#include "Pinch.h"
#include "K30.h"
#include "Benchtop.h"
#include "Vector.h"
#include <SD.h>
/***** Global Constants & Vars *****/
#define OUT 1 //Set direction out to be 1
#define IN 0 //Set direction in to be 0
/***** microSD Card Vars *****/
#define _sd 53
File myFile;
String sample_name = "sample";
String file_name = "sample.txt";
/***** CLI vars *****/
int input; //Initialize serial input
#define MAIN 0
#define PARAMETER 1
byte ack=0;
#define NONE 130
#define NOT_YET_STR "\rNot Implemented Yet"
int menu = MAIN;
typedef float (Benchtop::*getter)();
getter getters[9] = { &Benchtop::get_syringe_rinse_speed,
&Benchtop::get_rinse_volume,
&Benchtop::get_rinse_time,
&Benchtop::get_acid_volume,
&Benchtop::get_acid_wait_time,
&Benchtop::get_sample_volume,
&Benchtop::get_syringe_sample_speed,
&Benchtop::get_sample_wait_time,
&Benchtop::get_integration_time
};
typedef void (Benchtop::*setter)(float);
setter setters[9] = { &Benchtop::set_syringe_rinse_speed,
&Benchtop::set_rinse_volume,
&Benchtop::set_rinse_time,
&Benchtop::set_acid_volume,
&Benchtop::set_acid_wait_time,
&Benchtop::set_sample_volume,
&Benchtop::set_syringe_sample_speed,
&Benchtop::set_sample_wait_time,
&Benchtop::set_integration_time
};
/***** Control Syringe Pump object (1 Stepper Motor + 2 valves) *****/
#define cs_mtr_drv 3 // control syringe steppin stepper
#define cs_mtr_dir 5 // control syringe dirpin stepper
#define cs_input_pin 6 // control syringe input valve valve
#define cs_output_pin 7 // control syringe output valve valve
Pump control_syringe(cs_mtr_drv,cs_mtr_dir,cs_input_pin,cs_output_pin); // Initialize pump object
/***** Relay for Acid Pump *****/
#define acid_pump_drv 9
Pinch acid_pump(acid_pump_drv); // Initialize acid pump object
/***** Relay for Strip Chamber (sc) *****/
#define sc_mtr_drv 8
Pinch strip_chamber(sc_mtr_drv); // Initialize strip chamber object
/***** K30 vars *****/
#define K30_rx 12
#define K30_tx 13
K30 k30(K30_rx,K30_tx);
/***** Benchtop Object *****/
Benchtop benchtop;
/***** SETUP *****/
void setup() {
// pinMode(acid_pump, OUTPUT);
Serial.begin(9600);
Serial.print("\rInitializing SD card...");
if (!SD.begin(_sd)) {
Serial.println("Initialization failed!\r");
return;
}
Serial.println("Initialization done.");
benchtop.flush(control_syringe);
}
/***** LOOP *****/
void loop() {
while(!ack) {
ack=get_ack();
delay(3000);
}
if (menu == MAIN)
do_serial_cmd(get_serial_cmd());
else
do_parameter_cmd(get_serial_cmd());
}
// Test K30
void detect() {
k30.sendRequest();
unsigned long valCO2 = k30.getValue();
Serial.print("\rCo2 ppm = ");
Serial.println(valCO2);
}
// Test Control Syringe
void actuatePump() {
int stepSize = 500;
int stepDelay = 11;
control_syringe.set_valve_dirs(HIGH,LOW); // OPEN INPUT VALVE (VALVE 1), CLOSE OUTPUT VALVE (VALVE 2)
control_syringe.pump(stepSize,stepDelay,IN);
Serial.println("\rI pumped in");
control_syringe.set_valve_dirs(LOW,HIGH); // CLOSE INPUT VALVE (VALVE 1), OPEN OUTPUT VALVE (VALVE 2)
control_syringe.pump(stepSize,stepDelay,OUT);
Serial.println("\rI pumped out");
delay(200);
}
/* -------- COMMAND LINE FUNCTIONS -------- */
// Prints the command list
void print_cmd_list() {
Serial.print("\r\nCommands:\r\n");
// new commands here
Serial.print("\r\t<p>\tParameters Menu\n");
Serial.print("\r\t<r>\tRun\n");
Serial.print("\r\t<c>\tClean\n");
Serial.print("\r\t<0>\tTest Syringe Pump\n");
Serial.print("\r\t<1>\tTest Relays (Acid pump and strip chamber)\n");
Serial.print("\r\t<2>\tTest K30\n");
Serial.print("\r\t<h>\tPrint Command List\n");
Serial.println("\r");
}
// Prints the Parameter Menu
void print_parameter_menu() {
Serial.print("\r\nParameters:\r\n");
// new commands here
Serial.print("\r\t<1>\tSyringe Rinse Speed (ul/min)\t\t\t"); Serial.println(benchtop.get_syringe_rinse_speed());
Serial.print("\r\t<2>\tRinse Volume (uL)\t\t\t"); Serial.println(benchtop.get_rinse_volume());
Serial.print("\r\t<3>\tRinse Time (ms)\t\t\t\t"); Serial.println(benchtop.get_rinse_time());
Serial.print("\r\t<4>\tAcid Volume (uL)\t\t\t"); Serial.println(benchtop.get_acid_volume());
Serial.print("\r\t<5>\tAcid Wait Time (ms)\t\t\t"); Serial.println(benchtop.get_acid_wait_time());
Serial.print("\r\t<6>\tSample Volume (uL)\t\t\t"); Serial.println(benchtop.get_sample_volume());
Serial.print("\r\t<7>\tSyringe Sample Speed (uL/m)\t\t"); Serial.println(benchtop.get_syringe_sample_speed());
Serial.print("\r\t<8>\tSample Wait Time(ms)\t\t\t"); Serial.println(benchtop.get_sample_wait_time());
Serial.print("\r\t<9>\tIntegration Time(ms)\t"); Serial.println(benchtop.get_integration_time());
Serial.print("\r\t<h>\tReturn to main menu\n");
Serial.println("\r");
}
// Performs a particular serial command from the command list
void do_serial_cmd(byte cmd) {
switch(cmd) {
// Add more commands
case('p'): // Set parameters menu
menu = PARAMETER;
print_parameter_menu();
print_new_cmd_line();
break;
case('r'): // run analysis
myFile = SD.open(file_name, FILE_WRITE);
myFile.println(sample_name);
if (!myFile) {
Serial.println("\rGarbage.");
break;
}
Serial.println("\rBEGIN RINSE\n");
benchtop.rinse(strip_chamber,control_syringe);
Serial.println("\r\nEND RINSE\n");
Serial.println("\r\nBEGIN ANALYSIS\n");
benchtop.analysis(strip_chamber,control_syringe,k30,acid_pump,myFile);
Serial.println("\r\nEND ANALYSIS\n");
myFile.close();
print_new_cmd_line();
break;
case('c'): // run cleaning cycle
Serial.println("\rBEGIN RINSE\n");
benchtop.rinse(strip_chamber,control_syringe);
Serial.println("\r\nEND RINSE\n");
print_new_cmd_line();
break;
case('0'): // test the syringe pump system
for (int i = 0; i < 3; i++) {
actuatePump();
}
print_new_cmd_line();
break;
case('1'): // test the relays
Serial.println("\rTURNING ON ACID PUMP");
acid_pump.open();
delay(500);
Serial.println("\rTURNING OFF ACID PUMP");
acid_pump.close();
delay(500);
Serial.println("\rOPENING STRIP CHAMBER TO WASTE");
strip_chamber.open();
delay(500);
Serial.println("\rCLOSING STRIP CHAMBER TO WASTE (OPEN TO SAMPLE)");
strip_chamber.close();
delay(500);
break;
case('2'): // test the k30
for (int i = 0; i < 5; i++) {
detect();
delay(2000);
}
print_new_cmd_line();
break;
case('h'):
print_cmd_list();
print_new_cmd_line();
break;
case(NONE):
break;
}
}
// Updates variables from user into CLI
void updateVariable(byte idx) {
Serial.print("\r\tEnter the value you want to set.\n");
while (!Serial.available()) {
delay(5);
}
Serial.print("\tUpdating... ");
(benchtop.*(setters[idx]))(Serial.readString().toFloat());
Serial.println((benchtop.*(getters[idx]))());
print_parameter_menu();
print_new_cmd_line();
}
// Performs the parameter command from the user
void do_parameter_cmd(byte cmd) {
switch(cmd) {
case('h'):
menu = MAIN;
print_cmd_list();
print_new_cmd_line();
break;
case(NONE):
break;
default:
byte idx = cmd-'1';
if (idx > -1 && idx < 9) {
updateVariable(idx);
break;
}
Serial.println("That isn't a command");
print_parameter_menu();
print_new_cmd_line();
break;
}
}
// Waits for acknowledgement from the user on first boot
bool get_ack() {
Serial.println("\r\nPress <h> for command list");
Serial.print("\r>");
byte in_byte=Serial.read();
if(in_byte == 'h') {
Serial.print((char)in_byte);
print_cmd_list();
print_new_cmd_line();
return true;
}
return false;
}
// Fetches and executes new serial command from user
byte get_serial_cmd() {
byte in_byte;
if(Serial.available()) {
in_byte=Serial.read();
Serial.println((char)in_byte);
return in_byte;
} else {
return NONE;
}
}
// Prints new command line delimiter
void print_new_cmd_line() {
Serial.print("\r>");
}