-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollutionMap.ino
executable file
·91 lines (80 loc) · 2.32 KB
/
PollutionMap.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
const int dustPin = A5; // dust sensor - Arduino A5 pin
const int ledPin = 2; // IRED pin - 2
const int buzzerPin = 3 ; // buzzer pin - 3
float voltsMeasured = 0; // raw voltage data from sensor
float calcVoltage = 0; // refined voltage data range (0-5)volts
float dustDensity = 0; // dust density measured with refrence to refined voltage
char data; // data received form bluetooth
void setup()
{
Serial.begin(9600); // set baud rate to 9600
pinMode(ledPin, OUTPUT);
}
void s_read() // function to read data from the dust sensor
{
digitalWrite(ledPin, LOW); // power on the LED
delayMicroseconds(300);
voltsMeasured = analogRead(dustPin); // read the dust value
delayMicroseconds(40);
digitalWrite(ledPin, HIGH); // turn the LED off
delayMicroseconds(9680);
if (Serial.available()) // used for debugging, print any data received from bluetooth device via serial com
{
data = Serial.read();
Serial.println(data);
}
}
void s_calculate() // function to convert the sensor data to AQI unit ug/m3
{
calcVoltage = voltsMeasured * (5.0 / 1024.0);
dustDensity = 170 * ( calcVoltage - 2);
}
void s_print() // print the data to serial monitor (only for testing)
{
Serial.println("GP2Y1010AU0F readings");
Serial.print("Raw Signal Value = ");
Serial.println(voltsMeasured);
Serial.print("Voltage = ");
Serial.println(calcVoltage);
Serial.print("Dust Density = ");
Serial.println(dustDensity); // ug/m3
Serial.println("");
}
void s_send() // send data to Bluetooth device
{
// same function is used to send data to Bluetooth and print to serial device
// as they both lie in same serial com line
Serial.println(dustDensity);
}
void dangerCheck() // trigger the buzzer for higher value of pollution
{
if (dustDensity > 180)
{
tone(buzzerPin, 1000);
delay(250);
tone(buzzerPin, 800);
delay(250);
tone(buzzerPin, 1000);
delay(250);
tone(buzzerPin, 900);
delay(250);
tone(buzzerPin, 1000);
delay(250);
tone(buzzerPin, 700);
delay(250);
tone(buzzerPin, 1000);
delay(250);
tone(buzzerPin, 800);
delay(250);
noTone(buzzerPin);
}
}
void loop()
{
s_read();
s_calculate();
s_send();
s_print(); // use only for debugging and during sensor calibration
dangerCheck();
delay(2000); // wait for 2 seconds before next cycle
}