Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. /* ritten by Jonathan Lee Paul. Updated Wednesday, 7/18/18 */
  2.  
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. #include <dht.h>
  7. #define DHT_PIN 8 // Defines the DHT Data pin
  8.  
  9. /*CO2 Definitions START*/
  10. #define MG_PIN (1) //define the CO2 analog Pin
  11. #define DC_GAIN (8.5) //define the DC gain of amplifier
  12.  
  13. #define READ_SAMPLE_INTERVAL (32) //define how many samples you are going to take in normal operation
  14. #define READ_SAMPLE_TIMES (1) //define the time interval(in milisecond) between each samples in normal operation
  15.  
  16. #define V400 (582) //測量室外空氣得到的AnalogValue(400ppm)
  17. #define V40000 (345) //測量呼氣得到的AnalogValue(40000ppm)
  18. /*CO2 Definitions END*/
  19.  
  20. dht DHT; // Creates a DHT object
  21.  
  22. const int loopDelay = 1000;
  23.  
  24. int soilval = 0; //value for storing moisture value
  25. int newval = 0;
  26. int soilPin = A0;//Declare a variable for the soil moisture sensor
  27. int soilPower = 7;
  28.  
  29. int ledPin = 52;
  30.  
  31. int breakerPin = 23; //This pin connects to the relay that will momentarily cut power to the usb power hub during setup()
  32. int humidifierPin = 25; //This pin connects to the humidifier-circuit relay's data pin
  33. int fanPin = 27; //This pin connects to the fan-circuit relay's data pin
  34.  
  35. /*The humidifier will be on if h is between 0% and */
  36. float lowThresh = 75.0; //This is the lower limit of our incubator's humidity threshold. If h is lower than this value, turn on the humidifier
  37. float humidifierStopGap = 82.0; //This is the percentage that the humidifier will try to achieve while on. If h is higher than this value, turn off the humidifier.
  38. float fanStopGap = 83; //this is the percentage that the fan will try to achieve while on. If h is lower than this value, turn the fan off
  39. float highThresh = 85.0; //This is the upper limit of our incubator's humidity threshold. If h is higher than this value, turn on the fan
  40.  
  41. float slope=(V40000-V400)/(4.602-2.602);
  42. //校正線的斜率計算
  43. //log40000=4.602
  44. //log400=2.602
  45.  
  46. const float VRefer = 5; // voltage of O2 adc reference
  47. const int O2Pin = A14;
  48.  
  49. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //confirm LCD I2C configuration
  50.  
  51.  
  52. int oldh = 666;
  53. String statush = "off";
  54.  
  55. void setup() {
  56. Serial.begin(9600);
  57. lcd.begin(16, 2); //initialize a 16x2 lcd matrix
  58.  
  59. pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
  60. digitalWrite(soilPower, LOW);
  61.  
  62. pinMode(MG_PIN, OUTPUT);
  63.  
  64. pinMode(humidifierPin, OUTPUT);
  65. pinMode(breakerPin, OUTPUT);
  66. pinMode(ledPin, OUTPUT);
  67. pinMode(fanPin, OUTPUT);
  68.  
  69. /*
  70. //flash backlight 3 times
  71. for(int i = 0; i < 3; i++) {
  72. lcd.backlight();
  73. delay(150);
  74. lcd.noBacklight();
  75. delay(150);
  76. }
  77. */
  78.  
  79. lcd.clear();
  80. lcd.backlight();
  81.  
  82. //Cut the breaker once
  83. lcd.setCursor(0,0);lcd.print("Cutting Breaker");
  84. delay(200);
  85. digitalWrite(breakerPin, HIGH);
  86. delay(200);
  87. digitalWrite(breakerPin, LOW);
  88. lcd.setCursor(0,1);lcd.print("Done");
  89. delay(200);
  90. tone(53,1000,100);
  91.  
  92.  
  93. }
  94.  
  95. void loop() {
  96. int readData = DHT.read22(DHT_PIN); // Reads the data from the DHT
  97. float t = DHT.temperature; // Isolates the DHT temp
  98. int h = DHT.humidity; // Isolates the values of the humidity
  99.  
  100. int percentage;
  101. float analogValue;
  102. float volts;
  103.  
  104. analogValue = MGRead(MG_PIN);
  105. volts=analogValue*5/1024;
  106. percentage = MGGetPercentage(analogValue);
  107.  
  108. float O2Vout = 0;
  109. O2Vout = readO2O2Vout();
  110. float O2Percent = readConcentration();
  111. //int td = 2;tone(53,1000,td);delay(td);
  112.  
  113.  
  114. /*Toggling the humidifier based on air humidity input from the DHT22*/
  115. if (h != oldh){
  116. if (h < lowThresh && statush == "off"){ //turn on the humidifier. Beep the relay once
  117. digitalWrite(humidifierPin, HIGH);
  118. delay(100);
  119. digitalWrite(humidifierPin, LOW);
  120. statush = "on";
  121. tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("HUM TURNING ON");delay(1000);
  122. }
  123. if (h > humidifierStopGap && statush == "on"){ //turn off the humidifier. Beep the relay twice
  124. digitalWrite(humidifierPin, HIGH);
  125. delay(100);
  126. digitalWrite(humidifierPin, LOW);
  127. delay(600);
  128. digitalWrite(humidifierPin, HIGH);
  129. delay(100);
  130. digitalWrite(humidifierPin, LOW);
  131. statush = "off";
  132. tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("HUM TURNING OFF");delay(1000);
  133. }
  134. if (h > highThresh){
  135. digitalWrite(fanPin, HIGH);
  136. tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("FAN TURNING ON");delay(1000);
  137. }
  138. if (h < fanStopGap){
  139. digitalWrite(fanPin, LOW);
  140. tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("FAN TURNING OFF");delay(1000);
  141. }
  142. }
  143. oldh = h;
  144.  
  145.  
  146. digitalWrite(ledPin, HIGH); //Flash the LED once the notify user that data collection is done and fan/humidifieres have been toggled
  147. delay(50);
  148. digitalWrite(ledPin, LOW);
  149.  
  150. lcd.clear();
  151. lcd.setCursor(0,0);lcd.print("The HUM is " + statush);
  152. lcd.setCursor(0, 1);lcd.print("Temp: " + String(t,1) + char(223) + "C");
  153.  
  154. delay(loopDelay);
  155.  
  156. lcd.clear();
  157. lcd.setCursor(0, 0);lcd.print("Humidity: " + String(h) + char(37));
  158. lcd.setCursor(0, 1);lcd.print("Soil: " + String(readSoil()));
  159. ///lcd.print("Soil Hum:N/A");
  160.  
  161. delay(loopDelay);
  162.  
  163. lcd.clear();
  164. lcd.setCursor(0, 0);lcd.print("CO2: " + String(percentage));
  165. lcd.setCursor(13,0);lcd.print("ppm");
  166. lcd.setCursor(0, 1);lcd.print("Oxygen: " + String(O2Percent,1) + char(37));
  167.  
  168.  
  169. Serial.print("==Run stats==\nO2:" + String(O2Percent) + char(37) + "\nTemp:" + String(t) + " " + char(223) + "C" + "\nSoil:" + String(readSoil()) + "\nAir:" + String(h) + char(37) + "\nCO2:" + String(percentage) + "ppm" + "\n\n\n");
  170. delay(loopDelay-100);
  171. }
  172.  
  173.  
  174. //This is a function used to get the soil moisture content
  175. int readSoil() {
  176. digitalWrite(soilPower, HIGH);//turn D7 "On"
  177. delay(10);//wait 10 milliseconds
  178. soilval = analogRead(soilPin);//Read the SIG value form sensor
  179. digitalWrite(soilPower, LOW);//turn D7 "Off"
  180. newval = map(soilval, 595, 596, 0, 1);//"remap" the soil value to have a new zero value
  181. return newval;//send new moisture value
  182. }
  183.  
  184. //This is a function used to calculate the CO2 ppm
  185. int MGGetPercentage(float analogValue) {
  186. float logConc=2.602 +(analogValue-V400)/slope; //計算目前二氧化碳的濃度log值
  187. return pow(10,logConc );
  188.  
  189. }
  190.  
  191. //This is a function used to retrieve the CO2 sensor resistance
  192. float MGRead(int mg_pin) {
  193. int i;
  194. float v=0;
  195.  
  196. for (i=0;i<READ_SAMPLE_TIMES;i++) {
  197. v += analogRead(mg_pin);
  198. delay(READ_SAMPLE_INTERVAL);
  199. }
  200. v =v/READ_SAMPLE_TIMES ;
  201. return v;
  202. }
  203.  
  204. float readO2O2Vout()
  205. {
  206. long sum = 0;
  207. for(int i=0; i<32; i++)
  208. {
  209. sum += analogRead(O2Pin);
  210. }
  211.  
  212. sum >>= 5;
  213.  
  214. float MeasuredO2Vout = sum * (VRefer / 1023.0);
  215. return MeasuredO2Vout;
  216. }
  217.  
  218. float readConcentration()
  219. {
  220. // O2Vout samples are with reference to 3.3V
  221. float MeasuredO2Vout = readO2O2Vout();
  222.  
  223. //float Concentration = FmultiMap(MeasuredO2O2Vout, O2O2VoutArray,O2ConArray, 6);
  224. //when its output voltage is 2.0V,
  225. float Concentration = MeasuredO2Vout * 0.21 / 2.0;
  226. float Concentration_Percentage=Concentration*100;
  227. return Concentration_Percentage;
  228. }
Add Comment
Please, Sign In to add comment