Hello,
Right now, my ideas would be to use 5 sensors: MQ5, Sound sensor, Dust Sensor, DHT11, Photocell.
Next step would be to get a better rendering of the air quality by adding sensor and add a barometric sensor
I was wondering if you got ideas on how can I make this code cleaner and may get the most of it by adding new functions.
Here is the code
#include <DHT.h>
//Dust Sensor Configuration
int measurePin = A6;
int ledPower = 12;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
// Photocell Configuration
int photocellPin = A0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
//MQ5 Sensor
int sensor=A4;
int gas_value;
//Microphone Sensor
int soundDetectedPin = A2; // Use Pin 3 as our Input
int soundDetectedVal = HIGH; // This is where we record our Sound Measurement
boolean bAlarm = false;
unsigned long lastSoundDetectTime; // Record the time that we measured a sound
int soundAlarmTime = 500; // Number of milli seconds to keep the sound alarm high
//DHT Sensor
#define DHTPIN A3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(115200);
Serial.println("DHT11 TEST PROGRAM ");
delay(1000);
dht.begin();
pinMode(ledPower,OUTPUT);
pinMode(sensor,INPUT);
pinMode (soundDetectedPin, INPUT) ; // input from the Sound Detection Module
}
void loop() {
Serial.println("\n");
//Dust Sensor
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 3.3V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (3.3 / 1024);
dustDensity = 0.17 * calcVoltage - 0.1;
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" - Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Dust Density: ");
Serial.println(dustDensity);
delay(1000);
//SoundSensor
soundDetectedVal = analogRead (soundDetectedPin) ; // read the sound alarm time
//Photocell
photocellReading = analogRead(photocellPin);
delay(1000);
//MQ5 Sensor
gas_value=analogRead(sensor);
//DHT11
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celsius Humidity: ");
Serial.print(h);
Serial.print("Gas Sensor Value:");
Serial.println(gas_value);
Serial.print("Photocell Reading = ");
Serial.println(photocellReading); // the raw analog reading
Serial.print("Sound Reading = ");
Serial.println(soundDetectedVal); // the raw analog reading
}