Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.cpp
- *
- * Created on: Feb 18, 2013
- * Author: madned
- */
- #include <Arduino.h>
- #include <avr/eeprom.h>
- #include <util/delay.h>
- // include the library code:
- #include <LiquidCrystal.h>
- #include "SimpleTimer.h"
- #include "DHT.h"
- // Device Control Pins
- int lightPin = 3;
- int fanPin = 0;
- int pumpPin = 1;
- int screen = 0; // LCD screen ID
- float current_temp = 0;
- float current_humidity = 0;
- float set_temp = 75;
- float set_humidity = 50;
- long current_count = 0; // number of intervals we counted
- long next_count = 0; // number we want to get to
- long set_daylight_hours = 12;
- long set_night_hours = 12;
- boolean lightsOn = false;
- boolean select = false;
- long action_interval = 600; // respond every 600 seconds (10 minutes.)
- int pump_interval = 3; // number of seconds to run mister
- SimpleTimer timer;
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- DHT dht(2,DHT11);
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- #define MAX_SCREEN 5 // number of screens
- void timerCallback() {
- //Serial.println("callback!");
- screen=0;
- int pumpInterval = pump_interval * 1000;
- // run the misting pump for a set period if humidity is low.
- if (current_humidity < set_humidity) {
- digitalWrite(pumpPin, LOW);
- delay(pumpInterval);
- digitalWrite(pumpPin, HIGH);
- }
- if (current_temp > set_temp) {
- digitalWrite (fanPin, LOW); // too hot, fan on
- } else {
- digitalWrite (fanPin, HIGH);
- }
- current_count = current_count + 1;
- if (current_count > next_count) {
- current_count = 0;
- if (lightsOn) {
- lightsOn = false;
- digitalWrite(lightPin,HIGH); // lights off
- next_count = (set_night_hours * 3600) / action_interval;
- } else {
- lightsOn = true;
- digitalWrite(lightPin,LOW); // lights on
- next_count = (set_daylight_hours * 3600) / action_interval;
- }
- }
- }
- // read the buttons
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0);
- // read the value from the sensor
- // my buttons when read are centered at these values: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- return btnNONE; // when all others fail, return this...
- }
- // The setup() method runs once, when the sketch starts
- void setup() {
- long checkInterval = action_interval * 1000;
- //set_daylight_hours = eeprom_read_byte(0);
- //set_night_hours = 24-set_daylight_hours;
- //next_count = set_night_hours * 60 *60 / action_interval;
- //next_count = set_daylight_hours * 60 *60 / action_interval;
- //Serial.begin(9600);
- // initialize the control pins as outputs:
- pinMode (lightPin, OUTPUT);
- pinMode (fanPin, OUTPUT);
- pinMode (pumpPin, OUTPUT);
- digitalWrite(lightPin, HIGH);
- digitalWrite(fanPin, HIGH);
- digitalWrite(pumpPin, HIGH);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print welcome message to the LCD.
- //lcd.print("Greenhouse Data:");
- timer.setInterval(checkInterval,timerCallback);
- }
- void set_value(int delta, int num) {
- switch (num) // depending on which button was pushed, we perform an action
- {
- case 1:
- {
- set_temp += delta;
- break;
- }
- case 2:
- {
- set_humidity += delta;
- break;
- }
- case 3:
- {
- set_daylight_hours = set_daylight_hours + delta;
- set_night_hours = 24 - set_daylight_hours;
- if (lightsOn) {
- next_count = (set_daylight_hours * 3600) / action_interval;
- } else {
- next_count = (set_night_hours * 3600) / action_interval;
- }
- //eeprom_write_byte(0,set_daylight_hours);
- break;
- }
- case 4:
- {
- pump_interval += delta;
- break;
- }
- }
- }
- // display info to the LCD screen depending on screen/mode.
- // 0=main, 1=temp,
- void display_screen(int num) {
- lcd.clear();
- switch (num) // depending on which button was pushed, we perform an action
- {
- case 0:
- {
- if (lightsOn)
- lcd.print("Day: ");
- else
- lcd.print("Night: ");
- lcd.print(current_count,10);
- lcd.print("/");
- lcd.print(next_count,10);
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(current_temp);
- //Serial.print(temp);
- lcd.print("F ");
- lcd.print(current_humidity);
- lcd.print("%");
- //lcd.print("RIGHT ");
- break;
- }
- case 1:
- {
- lcd.print("Set Temperature:");
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(set_temp);
- break;
- }
- case 2:
- {
- lcd.print("Set Humidity:");
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(set_humidity);
- break;
- }
- case 3:
- {
- lcd.print("Light/Dark Hours");
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(set_daylight_hours);
- lcd.print("/");
- lcd.print(set_night_hours);
- break;
- }
- case 4:
- {
- lcd.print("Pump On Seconds");
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(pump_interval);
- break;
- }
- }
- }
- // the loop() method runs over and over again,
- // as long as the Arduino has power
- void loop()
- {
- timer.run();
- //digitalWrite(ledPin, HIGH); // set the LED on
- delay(100);
- current_temp = dht.readTemperature(true);
- current_humidity = dht.readHumidity();
- //Serial.println("Temp: ");
- // wait for a second
- //digitalWrite(ledPin, LOW); // set the LED off
- delay(100); // wait for a second
- lcd_key = read_LCD_buttons(); // read the buttons
- switch (lcd_key) // depending on which button was pushed, we perform an action
- {
- case btnRIGHT:
- {
- screen = (screen + 1) % MAX_SCREEN;
- //lcd.print("RIGHT ");
- break;
- }
- case btnLEFT:
- {
- screen = (screen - 1) % MAX_SCREEN;
- break;
- }
- case btnUP:
- {
- set_value(1,screen);
- break;
- }
- case btnDOWN:
- {
- set_value(-1,screen);
- break;
- }
- case btnSELECT:
- {
- select = true;
- lcd.print("SELECT");
- break;
- }
- case btnNONE:
- {
- if (select) {
- select = false;
- lightsOn = ~lightsOn;
- }
- //lcd.print("NONE ");
- break;
- }
- }
- // Serial.print(screen);
- display_screen(screen);
- }
- int main(void) {
- init();
- setup();
- while(true) {
- loop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement