Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <dht.h>
- #include <SPI.h>
- #include <SD.h>
- /*---- DHT -----*/
- dht DHT;
- const int DHT11_PIN = 3; //DHT11_PIN = 12;//Changed
- /*--------Display----------*/
- // initialize the library by associating any needed LCD interface pin
- // with the arduino pin number it is connected to
- const int rs = 5, en = 6, d4 = 7, d5 = 8, d6 = 9, d7 = 10;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- /*----Sound Sensor-----*/
- #define SOUNDANALOG_PIN A0
- const int SOUNDDIGITAL_PIN = 1; //SOUNDDIGITAL_PIN = 11;//Changed
- boolean soundDigitalVal = 0;
- int soundAnalogVal = 0;
- /*-------Dust-------*/
- const int DUSTANALOG_PIN = A5;
- unsigned int samplingTime = 280;
- unsigned int deltaTime = 40;
- unsigned int sleepTime = 9680;
- float dustVoMeasured = 0;
- float calcVoltage = 0;
- float dustDensity = 0;
- /*----SD_Card-----*/
- /*
- SD card attached to SPI bus as follows:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4
- */
- File sensorDataFile;
- const int chipSelect = 4;
- int serialNum = 1;//for storing data serially in file
- void setup(){
- Serial.begin (9600);
- pinMode(SOUNDANALOG_PIN, INPUT);
- pinMode(SOUNDDIGITAL_PIN, INPUT);
- //Setup of the LCD's number of column and rows
- lcd.begin(20, 4);
- //Displaying welcome to the LCD
- lcd.print("Welcome!");
- Serial.print("Initializing SD card...");
- if(!SD.begin(chipSelect)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
- sensorDataFile = SD.open("sensorData.txt", FILE_WRITE);
- if (sensorDataFile) {
- Serial.println("File opened successfully");
- sensorDataFile.println("SI,Temperature,Humidity,DustDensity, db");
- }
- sensorDataFile.close();
- }
- void loop (){
- lcd.clear();
- //delayMicroseconds(samplingTime);
- /***-------------- SD-card ------------***/
- sensorDataFile = SD.open("sensorData.txt", FILE_WRITE);
- if (sensorDataFile){
- sensorDataFile.print(serialNum);
- serialNum = serialNum + 1;
- sensorDataFile.print(",");
- }
- /***-------- DHT11 Sensor -----------***/
- int chk = DHT.read11(DHT11_PIN);
- lcd.setCursor(0,0);
- lcd.print("Temp: " + String(DHT.temperature) + ((char)223) + "C");
- lcd.setCursor(0,1);
- lcd.print("Humidity: " + String(DHT.humidity)+"%");
- //Serial.println("Temp: " + String(DHT.temperature) + ((char)223) + "C");
- //Serial.println("Humidity: " + String(DHT.humidity)+"%");
- sensorDataFile.print(DHT.temperature);
- sensorDataFile.print(",");
- sensorDataFile.print(DHT.humidity);
- sensorDataFile.print(",");
- /*-----------------------------------*/
- /***-------- Dust Sensor ---------***/
- dustVoMeasured = analogRead(DUSTANALOG_PIN);
- calcVoltage = dustVoMeasured*(5.0/1024);
- dustDensity = 0.17*calcVoltage-0.1;
- if ( dustDensity < 0)
- {
- dustDensity = 0.00;
- }
- lcd.setCursor(0,2);
- lcd.print("Dust: "+String(dustDensity)+"ng/m3");
- //Serial.println("Dust: "+String(dustDensity)+"ng/m3");
- sensorDataFile.print(dustDensity);
- sensorDataFile.print(",");
- /*-----------------------------------*/
- /***---------- Sound Sensor ----------***/
- soundAnalogVal = analogRead(SOUNDANALOG_PIN);
- soundDigitalVal = digitalRead(SOUNDDIGITAL_PIN);
- double soundAnalogValDB = 20 * log10(soundAnalogVal);
- //double soundAnalogValDB = (soundAnalogVal + 83.2073 ) / 11.003;
- //Serial.println (analogVal);
- //When the sensor detects a signal above the threshold value, LED flashes
- if (soundDigitalVal==HIGH) {
- lcd.setCursor(0,3);//set the cursor at first row
- lcd.print("Sound: "+String(soundAnalogValDB)+"db" + " (High)");//Sound Intensity level
- //Serial.println("Sound: "+String(soundAnalogValDB)+"db" + " (High)");
- sensorDataFile.print(soundAnalogValDB);
- }
- else {
- lcd.setCursor(0,3);//set the cursor at first row
- lcd.print("Sound: "+String(soundAnalogValDB)+"db" + " (Low)");//Sound Intensity level
- //Serial.println ("Sound: "+String(soundAnalogValDB)+"db" + " (Low)");
- sensorDataFile.print(soundAnalogValDB);
- }
- /*-----------------------------------*/
- //Serial.println();
- sensorDataFile.println();
- sensorDataFile.close();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement