Advertisement
Guest User

FCM#93 - Chicken Coop

a guest
Jun 10th, 2014
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. //Celsius to Fahrenheit conversion
  2. double Fahrenheit(double celsius)
  3. {
  4.     return 1.8 * celsius + 32;
  5. }
  6.  
  7. // fast integer version with rounding
  8. //int Celcius2Fahrenheit(int celcius)
  9. //{
  10. //  return (celsius * 18 + 5)/10 + 32;
  11. //}
  12.  
  13.  
  14. //Celsius to Kelvin conversion
  15. double Kelvin(double celsius)
  16. {
  17.     return celsius + 273.15;
  18. }
  19.  
  20. // dewPoint function NOAA
  21. // reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
  22. // reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
  23. //
  24. double dewPoint(double celsius, double humidity)
  25. {
  26.     // (1) Saturation Vapor Pressure = ESGG(T)
  27.     double RATIO = 373.15 / (273.15 + celsius);
  28.     double RHS = -7.90298 * (RATIO - 1);
  29.     RHS += 5.02808 * log10(RATIO);
  30.     RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  31.     RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  32.     RHS += log10(1013.246);
  33.  
  34.         // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  35.     double VP = pow(10, RHS - 3) * humidity;
  36.  
  37.         // (2) DEWPOINT = F(Vapor Pressure)
  38.     double T = log(VP/0.61078);   // temp var
  39.     return (241.88 * T) / (17.558 - T);
  40. }
  41.  
  42. // delta max = 0.6544 wrt dewPoint()
  43. // 6.9 x faster than dewPoint()
  44. // reference: http://en.wikipedia.org/wiki/Dew_point
  45. double dewPointFast(double celsius, double humidity)
  46. {
  47.     double a = 17.271;
  48.     double b = 237.7;
  49.     double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
  50.     double Td = (b * temp) / (a - temp);
  51.     return Td;
  52. }
  53.  
  54. // include the servo library
  55. #include <Servo.h>
  56. #include <LiquidCrystal.h>
  57. #include <dht11.h>
  58.  
  59. Servo myServo;      // create a servo object
  60.  
  61. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  62.  
  63. dht11 DHT11;
  64. #define DHT11PIN 6
  65.  
  66. int light;          // LDR value - how light/dark is it
  67.  
  68. int sunUp=790;      // LDR setting for daytime
  69. int sunDown=400;    // LDR setting for night time
  70.  
  71. int reverse=0;      // only used in the map funtion to close door
  72. int coopOpen=0;     // only really used in door opening command
  73. int openDoor=1;     // open coop
  74. int closeTime=3000; // 2secs to close door
  75.  
  76. int coopDanger=8;   // Red LED pin - signifies coop open
  77. int coopSafe=7;     // Green LED pin - signifies coop closed
  78.  
  79. //  ----
  80.  
  81. void setup() {
  82.   myServo.attach(9); // attaches the servo on pin 9 to the servo object
  83.  
  84.   pinMode(coopDanger,OUTPUT);  // Red LED pin is an output
  85.   pinMode(coopSafe,OUTPUT);    // Green LED pin is an output
  86.  
  87.   digitalWrite(coopDanger, HIGH);  // Set Red LED off on start
  88.   digitalWrite(coopSafe, LOW);     // Set Green LED on at start (coop is closed on start)
  89.  
  90.   lcd.begin(16, 2);
  91.  
  92.   Serial.begin(9600);              // open a serial connection to your computer
  93. }
  94.  
  95. void loop() {
  96.   light=analogRead(A1);           // read LDR
  97.  
  98.   int chk = DHT11.read(DHT11PIN);
  99.  
  100. //  CLOSE COOP
  101.   if (light < sunDown && coopOpen==1){  // sun is down and coop is open then:
  102.       myServo.attach(9);                // attach servo
  103.       myServo.write(openDoor);          // close coop
  104.       digitalWrite(coopDanger, LOW);    // red LED off
  105.       digitalWrite(coopSafe, HIGH);     // light green LED
  106.       delay(closeTime);                 // delay while moving servo
  107.       myServo.detach();                 // turn off servo
  108.       coopOpen=0;                       // set open to false
  109.   }
  110.  
  111. //  OPEN COOP
  112.   if (light > sunUp && coopOpen==0){        // sun is up and coop is closed then:
  113.       myServo.attach(9);                    // attach servo
  114.       reverse=map(openDoor,0,1023,180,0);   // map 180-1 to reverse servo
  115.       myServo.write(reverse);               // close coop
  116.       digitalWrite(coopDanger, HIGH);       // red LED off
  117.       digitalWrite(coopSafe, LOW);          // light green LED
  118.       delay(closeTime);                     // delay while moving servo
  119.       myServo.detach();                     // stop servo
  120.       coopOpen=1;                           // set open to true
  121.   }
  122.  
  123.   Serial.print("Read sensor: ");
  124.   switch (chk)
  125.   {
  126.     case DHTLIB_OK:
  127.         Serial.println("OK");
  128.         break;
  129.     case DHTLIB_ERROR_CHECKSUM:
  130.         Serial.println("Checksum error");
  131.         break;
  132.     case DHTLIB_ERROR_TIMEOUT:
  133.         Serial.println("Time out error");
  134.         break;
  135.     default:
  136.         Serial.println("Unknown error");
  137.         break;
  138.   }
  139.  
  140.   Serial.print("Humidity (%): ");
  141.   Serial.println((float)DHT11.humidity, 2);
  142.  
  143.   Serial.print("Temperature (°C): ");
  144.   Serial.println((float)DHT11.temperature, 2);
  145.  
  146.   Serial.print("Temperature (°F): ");
  147.   Serial.println(Fahrenheit(DHT11.temperature), 2);
  148.  
  149.   Serial.print("Temperature (°K): ");
  150.   Serial.println(Kelvin(DHT11.temperature), 2);
  151.  
  152.   Serial.print("Dew Point (°C): ");
  153.   Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
  154.  
  155.   Serial.print("Dew PointFast (°C): ");
  156.   Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  157.  
  158.   // set the cursor to column 0, line 0
  159.   lcd.setCursor(0, 0);
  160.   lcd.print("Humidity (%): ");
  161.   lcd.print(DHT11.humidity);
  162.   // set the cursor to column 0, line 1
  163.   // (note: line 1 is the second row, since counting begins with 0):
  164.   lcd.setCursor(0, 1);
  165.   lcd.print("Temp. ('C): ");
  166.   lcd.print(DHT11.temperature);
  167.  
  168.  
  169. // print light level
  170.   Serial.print(", light: ");
  171.   Serial.println(light);
  172.  
  173.   delay(3000);
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement