-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_Sensor_Alarm.ino
90 lines (80 loc) · 2.48 KB
/
CO2_Sensor_Alarm.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
// Import libraries
#include "Adafruit_CCS811.h" // Incluir la librería del sensor
Adafruit_CCS811 ccs; // Crear un objeto sensor
// set digital pins in Arduino Nano for RGB LED and Buzzer
int buzzer = 7;
int redLed= 4;
int greenLed = 5;
int BlueLed = 6;
// Set threshold values
int sensorThres1 = 400;
int sensorThres2 = 600;
int sensorThres3 = 800;
int sensorThres4 = 1000;
// Set pin modes (IN/OUT) and begin to search sensors
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(BlueLed, OUTPUT);
Serial.begin(115200); // Initialize serial port
// Serial.println("Print CCS811 data");
Serial.println("CO2, TVOC");
if(!ccs.begin()){ // Initialize sensor
Serial.println("Check wiring");
while(1);
}
while(!ccs.available());
}
void loop() {
if(ccs.available()){
if(!ccs.readData()){
int analogSensor=ccs.geteCO2();
if (analogSensor > sensorThres4)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(BlueLed, LOW);
tone(buzzer, 2000, 500);
//tone(buzzer, 500, 200);
//tone(buzzer, 200, 200);
}
else if ((analogSensor < sensorThres4) && (analogSensor > sensorThres3))
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
digitalWrite(BlueLed, HIGH);
tone(buzzer, 500, 200);
}
else if ((analogSensor < sensorThres3) && (analogSensor > sensorThres2))
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW); //HIGH);
digitalWrite(BlueLed, LOW);
noTone(buzzer);
}
else if ((analogSensor < sensorThres2) && (analogSensor > sensorThres1))
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(BlueLed, LOW); //HIGH);
noTone(buzzer);
}
else
{
digitalWrite(redLed, LOW); //HIGH);
digitalWrite(greenLed, LOW); //HIGH);
digitalWrite(BlueLed, LOW); // HIGH);
noTone(buzzer);
}
Serial.print(ccs.geteCO2()); // Get and print and plot CO2 signal
Serial.print(", "); // split values with comma
Serial.println(ccs.getTVOC()); // Get and print TVOC
}
else{
Serial.println("ERROR!"); // Check for errors
while(1);
}
}
delay(100); // Make measurements each 0.1 s
}