Advertisement
Guest User

Arduino Greenhouse code

a guest
Mar 21st, 2013
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.29 KB | None | 0 0
  1. /*
  2.  * main.cpp
  3.  *
  4.  *  Created on: Feb 18, 2013
  5.  *       Author: madned
  6.  */
  7.  
  8.  
  9. #include <Arduino.h>
  10. #include <avr/eeprom.h>
  11. #include <util/delay.h>
  12. //  include the library code:
  13. #include <LiquidCrystal.h>
  14. #include "SimpleTimer.h"
  15. #include "DHT.h"
  16.  
  17. // Device Control Pins
  18. int lightPin =  3;
  19. int fanPin = 0;
  20. int pumpPin = 1;
  21.  
  22. int screen = 0;      // LCD screen ID
  23. float current_temp = 0;
  24. float current_humidity = 0;
  25. float set_temp = 75;
  26. float set_humidity = 50;
  27.  
  28. long current_count = 0; // number of intervals we counted
  29. long next_count = 0; // number we want to get to
  30. long set_daylight_hours = 12;
  31. long set_night_hours = 12;
  32.  
  33. boolean lightsOn = false;
  34. boolean select = false;
  35.  
  36. long action_interval = 600; // respond every 600 seconds (10 minutes.)
  37. int pump_interval = 3; // number of seconds to run mister
  38.  
  39. SimpleTimer timer;
  40. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  41. DHT dht(2,DHT11);
  42. // define some values used by the panel and buttons
  43. int lcd_key     = 0;
  44. int adc_key_in  = 0;
  45. #define btnRIGHT  0
  46. #define btnUP     1
  47. #define btnDOWN   2
  48. #define btnLEFT   3
  49. #define btnSELECT 4
  50. #define btnNONE   5
  51.  
  52. #define MAX_SCREEN 5  // number of screens
  53. void timerCallback() {
  54.     //Serial.println("callback!");
  55.     screen=0;
  56.     int pumpInterval = pump_interval * 1000;
  57.  
  58.     // run the misting pump for a set period if humidity is low.
  59.     if (current_humidity < set_humidity) {
  60.         digitalWrite(pumpPin, LOW);
  61.         delay(pumpInterval);
  62.         digitalWrite(pumpPin, HIGH);
  63.     }
  64.     if (current_temp > set_temp) {
  65.         digitalWrite (fanPin, LOW); // too hot, fan on
  66.     } else {
  67.         digitalWrite (fanPin, HIGH);
  68.     }
  69.  
  70.     current_count = current_count + 1;
  71.  
  72.     if (current_count > next_count) {
  73.         current_count = 0;
  74.  
  75.         if (lightsOn) {
  76.             lightsOn = false;
  77.             digitalWrite(lightPin,HIGH); // lights off
  78.             next_count = (set_night_hours * 3600) / action_interval;
  79.         } else {
  80.             lightsOn = true;
  81.             digitalWrite(lightPin,LOW); // lights on
  82.             next_count = (set_daylight_hours * 3600) / action_interval;
  83.         }
  84.     }
  85. }
  86.  
  87. // read the buttons
  88. int read_LCD_buttons()
  89. {
  90.  adc_key_in = analogRead(0);
  91.  // read the value from the sensor
  92.  // my buttons when read are centered at these values: 0, 144, 329, 504, 741
  93.  // we add approx 50 to those values and check to see if we are close
  94.  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  95.  if (adc_key_in < 50)   return btnRIGHT;
  96.  if (adc_key_in < 195)  return btnUP;
  97.  if (adc_key_in < 380)  return btnDOWN;
  98.  if (adc_key_in < 555)  return btnLEFT;
  99.  if (adc_key_in < 790)  return btnSELECT;
  100.  return btnNONE;  // when all others fail, return this...
  101. }
  102.  
  103. // The setup() method runs once, when the sketch starts
  104. void setup()   {
  105.     long checkInterval = action_interval * 1000;
  106.     //set_daylight_hours = eeprom_read_byte(0);
  107.     //set_night_hours = 24-set_daylight_hours;
  108.     //next_count = set_night_hours * 60 *60 / action_interval;
  109.     //next_count = set_daylight_hours * 60 *60 / action_interval;
  110.     //Serial.begin(9600);
  111.  
  112.   // initialize the control pins as outputs:
  113.   pinMode (lightPin, OUTPUT);
  114.   pinMode (fanPin, OUTPUT);
  115.   pinMode (pumpPin, OUTPUT);
  116.   digitalWrite(lightPin, HIGH);
  117.   digitalWrite(fanPin, HIGH);
  118.   digitalWrite(pumpPin, HIGH);
  119.   // set up the LCD's number of columns and rows:
  120.   lcd.begin(16, 2);
  121.    // Print welcome message to the LCD.
  122.    //lcd.print("Greenhouse Data:");
  123.  
  124.    timer.setInterval(checkInterval,timerCallback);
  125. }
  126.  
  127. void set_value(int delta, int num) {
  128.     switch (num)               // depending on which button was pushed, we perform an action
  129.            {
  130.              case 1:
  131.                {
  132.                  set_temp += delta;
  133.                break;
  134.                }
  135.              case 2:
  136.              {
  137.                  set_humidity += delta;
  138.                break;
  139.              }
  140.              case 3:
  141.              {
  142.                  set_daylight_hours = set_daylight_hours + delta;
  143.                  set_night_hours = 24 - set_daylight_hours;
  144.                  if (lightsOn) {
  145.                             next_count = (set_daylight_hours * 3600) / action_interval;
  146.                         } else {
  147.                             next_count = (set_night_hours * 3600) / action_interval;
  148.                         }
  149.                  //eeprom_write_byte(0,set_daylight_hours);
  150.                  break;
  151.              }
  152.              case 4:
  153.              {
  154.                  pump_interval += delta;
  155.                  break;
  156.              }
  157.            }
  158. }
  159. // display info to the LCD screen depending on screen/mode.
  160. // 0=main, 1=temp,
  161. void display_screen(int num) {
  162.     lcd.clear();
  163.     switch (num)               // depending on which button was pushed, we perform an action
  164.        {
  165.          case 0:
  166.            {
  167.                if (lightsOn)
  168.                    lcd.print("Day: ");
  169.                else
  170.                    lcd.print("Night: ");
  171.                lcd.print(current_count,10);
  172.                lcd.print("/");
  173.                lcd.print(next_count,10);
  174.                lcd.setCursor(0,1);
  175.                  lcd.print("      ");
  176.                  lcd.setCursor(0,1);
  177.                  lcd.print(current_temp);
  178.                  //Serial.print(temp);
  179.                  lcd.print("F ");
  180.                  lcd.print(current_humidity);
  181.                  lcd.print("%");
  182.            //lcd.print("RIGHT ");
  183.            break;
  184.            }
  185.          case 1:
  186.          {
  187.              lcd.print("Set Temperature:");
  188.              lcd.setCursor(0,1);
  189.              lcd.print("      ");
  190.              lcd.setCursor(0,1);
  191.              lcd.print(set_temp);
  192.              break;
  193.          }
  194.          case 2:
  195.          {
  196.            lcd.print("Set Humidity:");
  197.            lcd.setCursor(0,1);
  198.            lcd.print("      ");
  199.            lcd.setCursor(0,1);
  200.            lcd.print(set_humidity);
  201.            break;
  202.           }
  203.          case 3:
  204.          {
  205.             lcd.print("Light/Dark Hours");
  206.             lcd.setCursor(0,1);
  207.             lcd.print("      ");
  208.             lcd.setCursor(0,1);
  209.             lcd.print(set_daylight_hours);
  210.             lcd.print("/");
  211.             lcd.print(set_night_hours);
  212.                break;
  213.          }
  214.          case 4:
  215.          {
  216.             lcd.print("Pump On Seconds");
  217.             lcd.setCursor(0,1);
  218.             lcd.print("      ");
  219.             lcd.setCursor(0,1);
  220.             lcd.print(pump_interval);
  221.  
  222.                    break;
  223.          }
  224.        }
  225. }
  226.  
  227. //  the loop() method runs over and over again,
  228. // as long as the Arduino has power
  229.  
  230. void loop()
  231. {
  232.   timer.run();
  233.   //digitalWrite(ledPin, HIGH);   // set the LED on
  234.   delay(100);
  235.   current_temp = dht.readTemperature(true);
  236.   current_humidity = dht.readHumidity();
  237.   //Serial.println("Temp: ");
  238.  
  239.   // wait for a second
  240.   //digitalWrite(ledPin, LOW);    // set the LED off
  241.   delay(100);                  // wait for a second
  242.  
  243.   lcd_key = read_LCD_buttons();  // read the buttons
  244.  
  245.    switch (lcd_key)               // depending on which button was pushed, we perform an action
  246.    {
  247.      case btnRIGHT:
  248.        {
  249.            screen = (screen + 1) % MAX_SCREEN;
  250.        //lcd.print("RIGHT ");
  251.        break;
  252.        }
  253.      case btnLEFT:
  254.        {
  255.  
  256.        screen = (screen - 1) % MAX_SCREEN;
  257.        break;
  258.        }
  259.      case btnUP:
  260.        {
  261.            set_value(1,screen);
  262.  
  263.        break;
  264.        }
  265.      case btnDOWN:
  266.        {
  267.          set_value(-1,screen);
  268.        break;
  269.        }
  270.      case btnSELECT:
  271.        {
  272.            select = true;
  273.        lcd.print("SELECT");
  274.        break;
  275.        }
  276.        case btnNONE:
  277.        {
  278.            if (select) {
  279.                select = false;
  280.                lightsOn = ~lightsOn;
  281.            }
  282.        //lcd.print("NONE  ");
  283.        break;
  284.        }
  285.    }
  286.   // Serial.print(screen);
  287.    display_screen(screen);
  288.  
  289.  
  290. }
  291.  
  292.  
  293. int main(void) {
  294.  
  295.   init();
  296.   setup();
  297.  
  298.   while(true) {
  299.     loop();
  300.   }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement