Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ritten by Jonathan Lee Paul. Updated Wednesday, 7/18/18 */
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <dht.h>
- #define DHT_PIN 8 // Defines the DHT Data pin
- /*CO2 Definitions START*/
- #define MG_PIN (1) //define the CO2 analog Pin
- #define DC_GAIN (8.5) //define the DC gain of amplifier
- #define READ_SAMPLE_INTERVAL (32) //define how many samples you are going to take in normal operation
- #define READ_SAMPLE_TIMES (1) //define the time interval(in milisecond) between each samples in normal operation
- #define V400 (582) //測量室外空氣得到的AnalogValue(400ppm)
- #define V40000 (345) //測量呼氣得到的AnalogValue(40000ppm)
- /*CO2 Definitions END*/
- dht DHT; // Creates a DHT object
- const int loopDelay = 1000;
- int soilval = 0; //value for storing moisture value
- int newval = 0;
- int soilPin = A0;//Declare a variable for the soil moisture sensor
- int soilPower = 7;
- int ledPin = 52;
- int breakerPin = 23; //This pin connects to the relay that will momentarily cut power to the usb power hub during setup()
- int humidifierPin = 25; //This pin connects to the humidifier-circuit relay's data pin
- int fanPin = 27; //This pin connects to the fan-circuit relay's data pin
- /*The humidifier will be on if h is between 0% and */
- 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
- 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.
- 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
- 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
- float slope=(V40000-V400)/(4.602-2.602);
- //校正線的斜率計算
- //log40000=4.602
- //log400=2.602
- const float VRefer = 5; // voltage of O2 adc reference
- const int O2Pin = A14;
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //confirm LCD I2C configuration
- int oldh = 666;
- String statush = "off";
- void setup() {
- Serial.begin(9600);
- lcd.begin(16, 2); //initialize a 16x2 lcd matrix
- pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
- digitalWrite(soilPower, LOW);
- pinMode(MG_PIN, OUTPUT);
- pinMode(humidifierPin, OUTPUT);
- pinMode(breakerPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
- pinMode(fanPin, OUTPUT);
- /*
- //flash backlight 3 times
- for(int i = 0; i < 3; i++) {
- lcd.backlight();
- delay(150);
- lcd.noBacklight();
- delay(150);
- }
- */
- lcd.clear();
- lcd.backlight();
- //Cut the breaker once
- lcd.setCursor(0,0);lcd.print("Cutting Breaker");
- delay(200);
- digitalWrite(breakerPin, HIGH);
- delay(200);
- digitalWrite(breakerPin, LOW);
- lcd.setCursor(0,1);lcd.print("Done");
- delay(200);
- tone(53,1000,100);
- }
- void loop() {
- int readData = DHT.read22(DHT_PIN); // Reads the data from the DHT
- float t = DHT.temperature; // Isolates the DHT temp
- int h = DHT.humidity; // Isolates the values of the humidity
- int percentage;
- float analogValue;
- float volts;
- analogValue = MGRead(MG_PIN);
- volts=analogValue*5/1024;
- percentage = MGGetPercentage(analogValue);
- float O2Vout = 0;
- O2Vout = readO2O2Vout();
- float O2Percent = readConcentration();
- //int td = 2;tone(53,1000,td);delay(td);
- /*Toggling the humidifier based on air humidity input from the DHT22*/
- if (h != oldh){
- if (h < lowThresh && statush == "off"){ //turn on the humidifier. Beep the relay once
- digitalWrite(humidifierPin, HIGH);
- delay(100);
- digitalWrite(humidifierPin, LOW);
- statush = "on";
- tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("HUM TURNING ON");delay(1000);
- }
- if (h > humidifierStopGap && statush == "on"){ //turn off the humidifier. Beep the relay twice
- digitalWrite(humidifierPin, HIGH);
- delay(100);
- digitalWrite(humidifierPin, LOW);
- delay(600);
- digitalWrite(humidifierPin, HIGH);
- delay(100);
- digitalWrite(humidifierPin, LOW);
- statush = "off";
- tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("HUM TURNING OFF");delay(1000);
- }
- if (h > highThresh){
- digitalWrite(fanPin, HIGH);
- tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("FAN TURNING ON");delay(1000);
- }
- if (h < fanStopGap){
- digitalWrite(fanPin, LOW);
- tone(53,1000,100);lcd.clear();lcd.setCursor(0,0);lcd.print("FAN TURNING OFF");delay(1000);
- }
- }
- oldh = h;
- digitalWrite(ledPin, HIGH); //Flash the LED once the notify user that data collection is done and fan/humidifieres have been toggled
- delay(50);
- digitalWrite(ledPin, LOW);
- lcd.clear();
- lcd.setCursor(0,0);lcd.print("The HUM is " + statush);
- lcd.setCursor(0, 1);lcd.print("Temp: " + String(t,1) + char(223) + "C");
- delay(loopDelay);
- lcd.clear();
- lcd.setCursor(0, 0);lcd.print("Humidity: " + String(h) + char(37));
- lcd.setCursor(0, 1);lcd.print("Soil: " + String(readSoil()));
- ///lcd.print("Soil Hum:N/A");
- delay(loopDelay);
- lcd.clear();
- lcd.setCursor(0, 0);lcd.print("CO2: " + String(percentage));
- lcd.setCursor(13,0);lcd.print("ppm");
- lcd.setCursor(0, 1);lcd.print("Oxygen: " + String(O2Percent,1) + char(37));
- 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");
- delay(loopDelay-100);
- }
- //This is a function used to get the soil moisture content
- int readSoil() {
- digitalWrite(soilPower, HIGH);//turn D7 "On"
- delay(10);//wait 10 milliseconds
- soilval = analogRead(soilPin);//Read the SIG value form sensor
- digitalWrite(soilPower, LOW);//turn D7 "Off"
- newval = map(soilval, 595, 596, 0, 1);//"remap" the soil value to have a new zero value
- return newval;//send new moisture value
- }
- //This is a function used to calculate the CO2 ppm
- int MGGetPercentage(float analogValue) {
- float logConc=2.602 +(analogValue-V400)/slope; //計算目前二氧化碳的濃度log值
- return pow(10,logConc );
- }
- //This is a function used to retrieve the CO2 sensor resistance
- float MGRead(int mg_pin) {
- int i;
- float v=0;
- for (i=0;i<READ_SAMPLE_TIMES;i++) {
- v += analogRead(mg_pin);
- delay(READ_SAMPLE_INTERVAL);
- }
- v =v/READ_SAMPLE_TIMES ;
- return v;
- }
- float readO2O2Vout()
- {
- long sum = 0;
- for(int i=0; i<32; i++)
- {
- sum += analogRead(O2Pin);
- }
- sum >>= 5;
- float MeasuredO2Vout = sum * (VRefer / 1023.0);
- return MeasuredO2Vout;
- }
- float readConcentration()
- {
- // O2Vout samples are with reference to 3.3V
- float MeasuredO2Vout = readO2O2Vout();
- //float Concentration = FmultiMap(MeasuredO2O2Vout, O2O2VoutArray,O2ConArray, 6);
- //when its output voltage is 2.0V,
- float Concentration = MeasuredO2Vout * 0.21 / 2.0;
- float Concentration_Percentage=Concentration*100;
- return Concentration_Percentage;
- }
Add Comment
Please, Sign In to add comment