-
Notifications
You must be signed in to change notification settings - Fork 0
/
total_v2.1.ino
363 lines (341 loc) · 13.6 KB
/
total_v2.1.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
/*
使用思路:
1. 活化酵母
2. 混合血浆与过敏物质,打开电源(电源键一次)
2. 等待计时结束(或者使用别的计时器,按下GO按钮跳过计时),取混合物放入比色皿中,并加入酵母溶液
3. 打开盖子,将比色皿放入孔中,按下GO按钮,等待屏幕提示
4. 屏幕提示继续时,将震荡装置贴在比色皿上,放入电池,按下GO按钮,等待反应完成
4. 待计时完成后,揭下震荡装置,然后按下GO按键//
5. 等待屏幕显示结果
6. 关闭电源,处理废品
*. 充电:左侧microUSB接口
程序思路:
1. t 计时1 过敏物质与血液的反应 GO按键跳过
2. b 空白测试 测空白的光强
3. u 计时2 组胺与酵母的反应 GO按键跳过
4. e 浓度测试 测量OD值和FL强度,并给出结果
5. w 空白等待
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//hardware
#define PIN_LED_OD A1
#define PIN_LED_FL A0
#define PIN_SEN_LIGHT_OD 0
#define PIN_SEN_LIGHT_FL 1
#define PIN_SEN_TEMP A2//Analog
#define PIN_BTN_GOON 11
#define PIN_POWER 10
//setting
#define REACTION_TIMER_1 9000 //half hour
#define REACTION_TIMER_2 1800 //half hour
#define MEASURE_TIMES 8
#define LIGHT_INTEN_OD 300
#define LIGHT_INTEN_FL 300
#define TEMP_HIGH 35
#define TEMP_LOW 20
#define THREHOLD 1
static const unsigned char PROGMEM logo[] ={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,
0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFE,0x60,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFE,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,
0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFC,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFC,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,
0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,
0xFC,0x7F,0xEC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFB,0x3F,0xE6,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xFD,0xFF,0xFF,0xFB,0x9F,0xF7,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF8,0xFF,0xFF,
0xFD,0x8F,0xF7,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xF6,0x3F,0xFF,0xFC,0xC7,0xF7,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0xF3,0x9F,0xFF,0xFE,0x67,0xC7,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xEC,0xE7,0xFF,
0xFF,0xB0,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xDE,0x79,0xFF,0xFF,0x98,0x3F,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0F,0x9F,0x9E,0xFF,0xFF,0xD8,0x3F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x3F,0xE7,0x7F,
0xFF,0xE4,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x7F,0xFB,0xBF,0xFF,0xE1,0xFF,0xF0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0E,0xFF,0xFD,0xFF,0xFF,0xF1,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0xFF,0xFE,0x3F,
0xFF,0xF9,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,0xFF,0xFF,0x3F,0xFF,0xF9,0xFF,0xF0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1C,0xFF,0xFF,0x3F,0xFF,0xFD,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0xFF,0xFE,0x3F,
0xFF,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x7F,0xFE,0x3F,0xFF,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3E,0x7F,0xFE,0x1F,0xFF,0xFE,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x3F,0xF0,0x1F,
0xFF,0xFE,0xFF,0xC8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x86,0x1F,0xFF,0xF8,0xFF,0xC8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3F,0x1C,0x0C,0x0F,0xFF,0xE3,0xFF,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x81,0xFC,0x00,
0x3F,0xDF,0xFF,0xB0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xCF,0xF0,0x7F,0xBF,0xDF,0xFF,0xA0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0xEF,0x03,0xFF,0xBF,0xDF,0xFF,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xE0,0x7F,0xFF,
0x7F,0xFF,0xFF,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0x7F,0xA7,0xFE,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0x7F,0xB7,0xFE,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFE,
0x20,0x07,0x7C,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xE0,0xB6,0xB9,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xFB,0xDB,0x93,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,
0xF3,0xFB,0xC7,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xF7,0xFB,0xCF,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xE7,0x99,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,
0xFF,0x99,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xE6,0x1D,0xFF,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xE7,0xE6,0x1C,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xC1,
0xCB,0xC8,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0x3C,0x41,0xE1,0xFE,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0xFE,0x7F,0x9E,0xF3,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x5C,0x7F,
0xFF,0xCF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0xFF,0xFF,0xBF,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x07,0x80,0xFF,0xFF,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0xFF,
0xFE,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xCD,0xFF,0xFE,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x4D,0xFF,0xFF,0x9E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,0xFF,
0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,
0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define ACTIVE_GAP 15000
unsigned long oldMillis = 0;
bool btn_old_stat = false;
char state = 'i';
float getTemp(int anv=8) // defaut sample times is 8
{
int sum = 0;
for(int i = 0; i < anv; i++){
sum+=analogRead(PIN_SEN_TEMP);
delay(100);// this can change
}
return sum*500.0/anv/1024;
}
volatile unsigned long cnt = 0;
void interrupt(){
cnt++;
}
unsigned long blank_fl = 0;
unsigned long blank_od = 0;
unsigned long black_fl = 0;
unsigned long black_od = 0;
bool Allergen ;
unsigned long oldMicros = 0;
unsigned long _getLightIntenFreq(int Pin)//Led_pin
//100ms - 10Hz
{
int num = PIN_SEN_LIGHT_OD ;
if(Pin == PIN_LED_FL)num = PIN_SEN_LIGHT_FL;
attachInterrupt(num+2,interrupt,CHANGE);//twice
oldMicros = micros();
cnt = 0;
while(micros()-oldMicros<100000){}
unsigned long detect_cnt = cnt;
return detect_cnt;
}
unsigned long getLightIntenFreq(int int_Pin,int Inten){
analogWrite(int_Pin,Inten);
delay(10);
unsigned long sum = 0;
for(int i = 0; i < MEASURE_TIMES; i++){
sum += _getLightIntenFreq(int_Pin);
}
digitalWrite(int_Pin,LOW);
return sum / MEASURE_TIMES;
}
bool goon(){
bool now_stat = digitalRead(PIN_BTN_GOON);
if(now_stat!=btn_old_stat){
btn_old_stat = now_stat;
return true;
}
return false;
}
unsigned int timer_cnt = 0;
unsigned long timer_oldMilis = 0;
void time_display(unsigned int seconds){
unsigned short min = seconds/60;
unsigned short sec = seconds-min*60;
display.clearDisplay();
display.setTextSize(3);
display.setCursor(10,10);
display.print(min);
display.print(":");
display.print(sec);
display.display();
}
void setup(){
pinMode(PIN_LED_FL,OUTPUT);
pinMode(PIN_LED_OD,OUTPUT);
pinMode(PIN_SEN_TEMP,INPUT);
pinMode(PIN_SEN_LIGHT_FL,INPUT);
pinMode(PIN_SEN_LIGHT_OD,INPUT);
pinMode(PIN_BTN_GOON,INPUT_PULLUP);
pinMode(PIN_POWER,OUTPUT);
digitalWrite(PIN_LED_FL,LOW);
digitalWrite(PIN_LED_OD,LOW);
digitalWrite(PIN_POWER,HIGH);
oldMillis = millis();
//Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
btn_old_stat = digitalRead(PIN_BTN_GOON);
state = 'i';
//show logo
display.clearDisplay();
display.drawBitmap(0, 0, logo, 128, 64, 1);
display.display();
delay(2000);
display.clearDisplay();
display.setCursor(0,4);
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.println("Tsinghua-A"); //输出字符
display.print("Welcome!"); //输出字符
display.display();
delay(2000);
}
void loop(){
switch (state){
case 'i':{//initial
float tp = getTemp();
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,0);
display.println("Temp: 28-30C is the best");
display.print(tp);
display.display();
delay(1000);
if(tp > TEMP_HIGH || tp < TEMP_LOW){
display.clearDisplay();
display.setCursor(0,0);
display.println("Temp not fit.28-30C is the best");
display.display();
delay(2000);
}else{
if(goon()){
state = 't';
timer_cnt = REACTION_TIMER_1;
timer_oldMilis = millis();
time_display(timer_cnt);
delay(400);
black_fl = _getLightIntenFreq(PIN_LED_FL);
black_od = _getLightIntenFreq(PIN_LED_OD);
break;
}
display.clearDisplay();
display.setCursor(0,0);
display.println("Mix blood & allergen. Close Box.Then GO");
display.display();
delay(1000);
}
break;}
case 't':{//timing
if(timer_cnt == 0 || goon()){
state = 'b';
break;
}
unsigned long now_mili = millis();
if(now_mili-timer_oldMilis>1000){
timer_cnt -= (now_mili-timer_oldMilis)/1000;
timer_oldMilis = now_mili;
time_display(timer_cnt);
}
delay(400);
break;}
case 'b'://blank
display.setTextSize(2);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,0);
display.println("Add mixture and yeast to cuvette.");
display.display();
delay(2000);
display.clearDisplay();
display.setCursor(0,0);
display.println("Put cuvette in the hole Then GO");
display.display();
delay(2000);
if(goon()){
state = 'm';
blank_fl = getLightIntenFreq(PIN_LED_FL,LIGHT_INTEN_FL);
blank_od = getLightIntenFreq(PIN_LED_OD,LIGHT_INTEN_OD);
}
break;
case 'm':{
display.clearDisplay();
display.setCursor(0,0);
display.println("stick the oscillomotor to cuvette.GO");
display.display();
delay(2000);
if(goon()){
state = 'u';
timer_cnt = REACTION_TIMER_2;
timer_oldMilis = millis();
time_display(timer_cnt);
delay(400);
}
break;
}
case 'u':{//timing2
if(timer_cnt == 0 || goon()){
state = 'o';
break;
}
unsigned long now_mili = millis();
if(now_mili-timer_oldMilis>1000){
timer_cnt -= (now_mili-timer_oldMilis)/1000;
timer_oldMilis = now_mili;
time_display(timer_cnt);
delay(400);
}
break;}
case 'o':{
display.setTextSize(2);
if(goon()){state = 'e';break;}
display.clearDisplay();
display.setCursor(0,0);
display.println("dettach oscillomotor from cuvette.");
display.display();
delay(2000);
display.clearDisplay();
display.setCursor(0,0);
display.println("Close the box. GO!");
display.display();
delay(2000);
break;
}
case 'e': {
display.setTextSize(3);
//measure OD600
double value_OD = log10((getLightIntenFreq(PIN_LED_OD,LIGHT_INTEN_OD) - black_od)/(blank_od - black_od));
//measure Flourence
double value_FL = log10( (getLightIntenFreq(PIN_LED_FL,LIGHT_INTEN_FL) - black_fl)/(blank_fl - black_fl));
//calculate&diplay
//allergy
if(value_FL/value_OD > THREHOLD){
Allergen = true;
display.clearDisplay();
display.setCursor(0,0);
display.println("An");
display.println("allergy");
display.display();
}else{
Allergen = false;
display.clearDisplay();
display.setCursor(0,0);
display.println("Not");
display.println("allergy");
display.display();
}
state = 'n';
break;}
case 'n':
display.clearDisplay();
display.setCursor(0,0);
if(Allergen) display.println("An");
else display.println("Not");
display.println("allergy");
display.display();
if(goon()) state = 'd';
break;
case 'd':
//显示已完成
display.clearDisplay();
display.drawBitmap(0, 0, logo, 128, 64, 1);
display.display();
if(goon()) state = 'i';
delay(1000);
break;
}
if( millis() - oldMillis > ACTIVE_GAP){
digitalWrite(PIN_POWER,LOW);
delay(100);
digitalWrite(PIN_POWER,HIGH);
oldMillis = millis();
}
}