-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoptica-co2sensor.ino
290 lines (247 loc) · 7.56 KB
/
toptica-co2sensor.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
#include <Wire.h>
#include <SparkFun_SCD30_Arduino_Library.h>
#include <I2C.h>
#include <II2C.h>
#include <MPU6050.h>
#include <MultiFuncShield.h>
#include <Wire.h>
//object to interface with Sensor/use the functions in the SCD30 library
SCD30 airSensor;
//variables, which later contain the diffrent measurement values
int co2Value;
float humidity;
float temperature;
//choose the CO2-value at which the device starts to buzz and blink
const int ALARMVALUE = 1000;
//variable, which controls the buzzer through 0 and 1
char buzzer;
//by default the device starts with measuring CO2 ^= mode 0
char mode = 0;
//variable for input through buttons
byte btn;
//define how long the buzzer should be interupted with Button 3
long buzzerInterupt = 60000;
//variables used to perform delays without stopping the programm
long timerBuzzer;
int timerBuzzerDelay = 1000;
int timerCalDelay = 1000;
long timerStartBuzzer;
long timerStartBuzzerDelay;
long timerStartCal;
//function predefinition to claim some storage space beforehand
void displayCo2();
void displayTemperature();
void displayHumidity();
void newCalibrationSCD30();
void setup()
{
int baudrate = 9600;
Serial.begin(baudrate);
//initializations of the Multifunctionboard and the I2C-Arduino Port
Wire.begin();
MFS.initialize();
//bootingsequence
startingSequence();
//checking the sensor connection
if (airSensor.begin() == false)
{
Serial.println("Error. SCD30 not detected. Please check the Connection. Press RESET to proceed.");
}
}
void loop()
{
//switching modes through pressing the first button
btn = MFS.getButton();
//By pressing the first button, one can choose between the three diffrent modes
if (btn == BUTTON_1_PRESSED)
{
mode++;
if (mode == 4)
{
mode = 0;
}
//defining the different modes names
switch (mode)
{
case 0:
MFS.write("Co2");
delay(500);
break;
case 1:
MFS.write("TENN");
MFS.writeLeds(LED_ALL, OFF);
delay(500);
break;
case 2:
MFS.write("HUNN");
delay(500);
break;
}
}
//through long-pressing the second button the device can be calibrated
if (btn == BUTTON_2_LONG_RELEASE)
{
newCalibrationSCD30();
}
//defining the different modes: measuring co2, temperature or humidity
switch (mode)
{
case 0:
displayCo2();
break;
case 1:
displayTemperature();
break;
case 2:
displayHumidity();
break;
}
}
//*************************************************************************************************************************************************************
//the starting sequence used in void setup()
void startingSequence()
{
MFS.beep();
MFS.write("top");
MFS.writeLeds(LED_4, ON);
delay(1500);
MFS.write("tica");
MFS.writeLeds(LED_3, ON);
delay(1500);
MFS.write("C02");
MFS.writeLeds(LED_2, ON);
delay(1500);
MFS.write("SENS");
MFS.writeLeds(LED_1, ON);
delay(2000);
MFS.writeLeds(LED_ALL, OFF);
MFS.beep();
delay(250);
MFS.writeLeds(LED_ALL, ON);
MFS.beep();
delay(250);
MFS.writeLeds(LED_ALL, OFF);
delay(100);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
//The devices main function: measuring CO2
void displayCo2()
{
//the SCD30 only provides new data every 2 seconds. This if-clause prevents the usage of unessecary processing power
if (airSensor.dataAvailable())
{
Serial.print("CO2: ");
Serial.println(airSensor.getCO2());
//the co2 value is transfered into the variable and shown on the display
co2Value = airSensor.getCO2();
MFS.write(co2Value);
}
//checking whether the co2-value has surpassed the threshold
if (co2Value >= ALARMVALUE)
{
buzzer = 1;
}
else
{
buzzer = 0;
}
//function for buzzering the buzzer
if (buzzer == 1)
{
MFS.writeLeds(LED_ALL, ON);
MFS.blinkLeds(LED_ALL, ON);
//by pressing the third button, while the co2-value has surpassed the threshold, one can pause the buzzering
if (btn == BUTTON_3_PRESSED)
{
timerStartBuzzer = millis();
//in order to prevent, that the buzzer won't work for the duration of buzzerInterupt when the device ist newly booted, the variable timerStartBuzzer is just now defined
timerBuzzer = buzzerInterupt;
Serial.print("Timerstart Pause: ");
Serial.println(timerStartBuzzer);
}
if (millis() < (timerStartBuzzer + timerBuzzer))
{
}
else
{
//pausing the buzzer for a second without using any delay too ensure optimal button input
if (millis() > (timerStartBuzzerDelay + timerBuzzerDelay))
{
MFS.beep();
timerStartBuzzerDelay = millis();
}
}
}
else
{
MFS.writeLeds(LED_ALL, OFF);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
//diplaying the temperature the same way the co2-value is diplayed (without buzzer and threshold)
void displayTemperature()
{
if (airSensor.dataAvailable())
{
temperature = airSensor.getTemperature(), 1;
Serial.print("Temperatur: ");
Serial.print(temperature);
Serial.println("°C");
MFS.write(temperature);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//diplaying the humidity the same way the co2-value is displayed (without buzzer and threshold)
void displayHumidity()
{
if (airSensor.dataAvailable())
{
humidity = airSensor.getHumidity(), 1;
Serial.print("Luftfeuchtigkeit: ");
Serial.print(humidity);
Serial.println("%");
MFS.write(humidity);
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//function the enforce a new calibration
void newCalibrationSCD30()
{
MFS.writeLeds(LED_ALL, OFF);
MFS.write("NEW");
delay(850);
MFS.write("CALI");
delay(850);
MFS.write("BRA");
delay(850);
MFS.write("TION");
delay(850);
MFS.write("EX");
delay(850);
MFS.write("POSE");
delay(850);
MFS.write("TO");
delay(850);
MFS.write("FRE");
delay(850);
MFS.write("SH");
delay(850);
MFS.write("AIR");
delay(850);
//countdown until the new calibration
for (int i = 120; i > 0; i--)
{
Serial.println(i);
MFS.write(i);
timerStartCal = millis();
while (millis() < (timerStartCal + timerCalDelay))
{
}
}
airSensor.setForcedRecalibrationFactor(400);
MFS.write("COMP");
delay(850);
MFS.write("LETE");
delay(850);
Serial.println("Calibration complete");
}