Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT22.h>
- // Only used for sprintf
- #include <stdio.h>
- // Connect a 4.7K resistor between VCC and the data pin (strong pullup)
- #define DHT22_PIN 12 //blue/white
- // Setup a DHT22 instance
- DHT22 myDHT22(DHT22_PIN);
- //set variables
- //int tempF
- int HeaterPin = 8; // Heater Transistor is connected to Pin 2 orange/white
- int FanPin = 7; // Fan Transistor connected to Pin 3 green/white
- float temperature;
- float humidity;
- // For creating light averages
- float readingsT[10];
- int iT = 0;
- int readingsTcount = 0;
- float avgT;
- int readingsTtotalcount; //
- //heating logic
- int heatingup = 0;
- //fan logic
- int fanup = 0; //0=off 1 =on
- int timer; //counter
- int cycle; //0=off 1 =on
- void setup(void)
- {
- // start serial port
- Serial.begin(9600);
- //Serial.println("Moms Greenhouse");
- }
- void loop(void)
- {
- DHT22_ERROR_t errorCode;
- // The sensor can only be read from every 1-2s, and requires a minimum
- // 2s warm-up after power-on.
- delay(2000);
- //Serial.print("Requesting data...");
- errorCode = myDHT22.readData();
- switch(errorCode)
- {
- case DHT_ERROR_NONE:
- ////uncomment for debug
- //Serial.print("Got Data ");
- //Serial.print(myDHT22.getTemperatureC());
- //Serial.print("C ");
- //Serial.print(myDHT22.getHumidity());
- //Serial.println("%");
- // Alternately, with integer formatting which is clumsier but more compact to store and
- // can be compared reliably for equality:
- //
- //char buf[128];
- //sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH",
- // myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
- // myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
- //Serial.println(buf);
- //Begin Greenhouse Code -----------------------------------------------------------------------
- temperature = myDHT22.getTemperatureCInt()/10 + (myDHT22.getTemperatureCInt()%10 * .1);
- //Serial.println(avgT);
- readingsT[readingsTcount] = temperature;
- readingsTcount++;
- if (readingsTcount == 10) { readingsTcount = 0; }
- if (readingsTtotalcount > 9) {
- avgT = (readingsT[0] + readingsT[1] + readingsT[2] + readingsT[3] + readingsT[4] + readingsT[5] + readingsT[6] + readingsT[7] + readingsT[8] + readingsT[9])/10;
- } else {
- avgT = 5555;
- readingsTtotalcount++;
- }
- if (avgT < 20) { //if temperature is below 20c start the heating up cycle
- heatingup = 1; // Turn on Heater
- }
- if(avgT < 24.5 && heatingup == 1) { //if temperature is below 24 and we're in a heating up cycle, keep the heater on
- heatingup = 1; // Turn on Heater
- }
- if(avgT > 24.5) { //if temperature is above 24 turn off the heating up cycle
- heatingup = 0; // Turn off Heater
- }
- if (heatingup == 1) {
- analogWrite(HeaterPin,255); // Turn on Heater
- } else if (heatingup == 0) {
- analogWrite(HeaterPin,0); // Turn off Heater
- }
- // fan logic ----------------------
- // if humidity is above 60 - fan on 30 min rest 30 min
- // if humidity is below 60 - pulse it 10 min rest 50 min
- humidity = myDHT22.getHumidityInt()/10 + (myDHT22.getHumidityInt()%10 * .1);
- timer = timer + 2; //add 2 seconds to timer
- if (humidity <= 60) { //if humidity <= 60
- if (timer > (45*60)) { cycle = 1; } // minutes
- if (timer > 3600) { cycle = 0; timer = 0; }
- }
- if (humidity > 60) { // if humidity > 60
- if (timer > (30*60)) { cycle = 1; } // minutes
- if (timer > 3600) { cycle = 0; timer = 0; }
- }
- if (cycle == 1) { fanup = 1; } else { fanup = 0; } //if cycle is on, turn fan on, if cycle is off, turn fan off
- if (fanup == 1) {
- analogWrite(FanPin,255); // Turn on Fan
- } else if (fanup == 0) {
- analogWrite(FanPin,0); // Turn off Fan
- }
- // end fan logic ----------------------
- // Serial.print(Lightreading); Serial.println(" Light");
- // Serial.print(temperature); Serial.println(" Current Temp");
- // Serial.print(avgT); Serial.println(" Average Temp");
- // Serial.print(readingsTcount); Serial.println(" Integer");
- // Serial.print(heatingup); Serial.println(" Heater Pin");
- // Serial.println(" ");
- //End Greenhouse Code -----------------------------------------------------------------------
- break;
- case DHT_ERROR_CHECKSUM:
- Serial.print("check sum error ");
- Serial.print(myDHT22.getTemperatureC());
- Serial.print("C ");
- Serial.print(myDHT22.getHumidity());
- Serial.println("%");
- break;
- case DHT_BUS_HUNG:
- Serial.println("BUS Hung ");
- break;
- case DHT_ERROR_NOT_PRESENT:
- Serial.println("Not Present ");
- break;
- case DHT_ERROR_ACK_TOO_LONG:
- Serial.println("ACK time out ");
- break;
- case DHT_ERROR_SYNC_TIMEOUT:
- Serial.println("Sync Timeout ");
- break;
- case DHT_ERROR_DATA_TIMEOUT:
- Serial.println("Data Timeout ");
- break;
- case DHT_ERROR_TOOQUICK:
- Serial.println("Polled too quick ");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement