Advertisement
dpauld

ECMSM_Sujon_Jan22_9PM_2020

Jan 22nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <dht.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5.  
  6. /*---- DHT -----*/
  7. dht DHT;
  8. const int DHT11_PIN = 3; //DHT11_PIN = 12;//Changed
  9.  
  10. /*--------Display----------*/
  11. // initialize the library by associating any needed LCD interface pin
  12. // with the arduino pin number it is connected to
  13. const int rs = 5, en = 6, d4 = 7, d5 = 8, d6 = 9, d7 = 10;
  14. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  15.  
  16. /*----Sound Sensor-----*/
  17. #define SOUNDANALOG_PIN A0
  18. const int SOUNDDIGITAL_PIN = 1; //SOUNDDIGITAL_PIN = 11;//Changed
  19. boolean soundDigitalVal = 0;
  20. int soundAnalogVal = 0;
  21.  
  22. /*-------Dust-------*/
  23. const int DUSTANALOG_PIN = A5;
  24.  
  25. unsigned int samplingTime = 280;
  26. unsigned int deltaTime = 40;
  27. unsigned int sleepTime = 9680;
  28.  
  29. float dustVoMeasured = 0;
  30. float calcVoltage = 0;
  31. float dustDensity = 0;
  32.  
  33. /*----SD_Card-----*/
  34. /*
  35. SD card attached to SPI bus as follows:
  36. ** MOSI - pin 11
  37. ** MISO - pin 12
  38. ** CLK - pin 13
  39. ** CS - pin 4
  40. */
  41. File sensorDataFile;
  42. const int chipSelect = 4;
  43.  
  44. int serialNum = 1;//for storing data serially in file
  45. void setup(){
  46. Serial.begin (9600);
  47.  
  48. pinMode(SOUNDANALOG_PIN, INPUT);
  49. pinMode(SOUNDDIGITAL_PIN, INPUT);
  50.  
  51. //Setup of the LCD's number of column and rows
  52. lcd.begin(20, 4);
  53. //Displaying welcome to the LCD
  54. lcd.print("Welcome!");
  55.  
  56. Serial.print("Initializing SD card...");
  57. if(!SD.begin(chipSelect)) {
  58. Serial.println("initialization failed!");
  59. return;
  60. }
  61. Serial.println("initialization done.");
  62. sensorDataFile = SD.open("sensorData.txt", FILE_WRITE);
  63. if (sensorDataFile) {
  64. Serial.println("File opened successfully");
  65. sensorDataFile.println("SI,Temperature,Humidity,DustDensity, db");
  66. }
  67. sensorDataFile.close();
  68. }
  69.  
  70. void loop (){
  71. lcd.clear();
  72. //delayMicroseconds(samplingTime);
  73.  
  74. /***-------------- SD-card ------------***/
  75. sensorDataFile = SD.open("sensorData.txt", FILE_WRITE);
  76. if (sensorDataFile){
  77. sensorDataFile.print(serialNum);
  78. serialNum = serialNum + 1;
  79. sensorDataFile.print(",");
  80. }
  81.  
  82. /***-------- DHT11 Sensor -----------***/
  83. int chk = DHT.read11(DHT11_PIN);
  84. lcd.setCursor(0,0);
  85. lcd.print("Temp: " + String(DHT.temperature) + ((char)223) + "C");
  86. lcd.setCursor(0,1);
  87. lcd.print("Humidity: " + String(DHT.humidity)+"%");
  88. //Serial.println("Temp: " + String(DHT.temperature) + ((char)223) + "C");
  89. //Serial.println("Humidity: " + String(DHT.humidity)+"%");
  90. sensorDataFile.print(DHT.temperature);
  91. sensorDataFile.print(",");
  92. sensorDataFile.print(DHT.humidity);
  93. sensorDataFile.print(",");
  94. /*-----------------------------------*/
  95.  
  96.  
  97. /***-------- Dust Sensor ---------***/
  98. dustVoMeasured = analogRead(DUSTANALOG_PIN);
  99. calcVoltage = dustVoMeasured*(5.0/1024);
  100. dustDensity = 0.17*calcVoltage-0.1;
  101. if ( dustDensity < 0)
  102. {
  103. dustDensity = 0.00;
  104. }
  105. lcd.setCursor(0,2);
  106. lcd.print("Dust: "+String(dustDensity)+"ng/m3");
  107. //Serial.println("Dust: "+String(dustDensity)+"ng/m3");
  108. sensorDataFile.print(dustDensity);
  109. sensorDataFile.print(",");
  110. /*-----------------------------------*/
  111.  
  112.  
  113. /***---------- Sound Sensor ----------***/
  114. soundAnalogVal = analogRead(SOUNDANALOG_PIN);
  115. soundDigitalVal = digitalRead(SOUNDDIGITAL_PIN);
  116.  
  117. double soundAnalogValDB = 20 * log10(soundAnalogVal);
  118. //double soundAnalogValDB = (soundAnalogVal + 83.2073 ) / 11.003;
  119.  
  120. //Serial.println (analogVal);
  121. //When the sensor detects a signal above the threshold value, LED flashes
  122. if (soundDigitalVal==HIGH) {
  123. lcd.setCursor(0,3);//set the cursor at first row
  124. lcd.print("Sound: "+String(soundAnalogValDB)+"db" + " (High)");//Sound Intensity level
  125. //Serial.println("Sound: "+String(soundAnalogValDB)+"db" + " (High)");
  126. sensorDataFile.print(soundAnalogValDB);
  127. }
  128. else {
  129. lcd.setCursor(0,3);//set the cursor at first row
  130. lcd.print("Sound: "+String(soundAnalogValDB)+"db" + " (Low)");//Sound Intensity level
  131. //Serial.println ("Sound: "+String(soundAnalogValDB)+"db" + " (Low)");
  132. sensorDataFile.print(soundAnalogValDB);
  133. }
  134. /*-----------------------------------*/
  135.  
  136. //Serial.println();
  137. sensorDataFile.println();
  138. sensorDataFile.close();
  139.  
  140. delay(1000);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement