Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <DHT.h>;
- LiquidCrystal_I2C lcd(0x27, 20, 4); // Set I2C Address and display size
- // pin assignments
- const int photocellPin = A0; // analog input pin for photocell
- int button1 = 2;
- int button2 = 3;
- int button3 = 4;
- #define DHTPIN 13 // what pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
- // Menu Variables and States
- unsigned long dhtDelay = 2000; //delay so sensor is read every 2 seconds
- unsigned long lastDhtReadingTime = 0; // the last time the DHT22 was read
- bool menuOn = true; // state of the display menu
- int menu[5]; // number of menu items
- bool doorOpen = true; // is the coop door open or closed
- int optionSelect = 0; //which menu option is selected
- int lastOptionSelect = 0;
- int lastItemSelect = 0;
- int itemSelect = 0;
- int coopTemp = 4;
- int closeDoor = 20;
- int hotTemp = 30;
- int coldTemp = 2;
- int waterTemp = 2;
- int chk;
- float hum; //Stores humidity value
- float temp; //Stores temperature value
- int photocellReading; // analog reading of the photocell
- int photocellReadingLevel; // photocell reading levels (night, light)
- unsigned long photocellReadingDelay = 6000; // 600000 = 10 minute
- unsigned long lastPhotocellReadingTime = 0; // the last time the photocell was read
- int buttonState1 = 0; // current state of each button
- int buttonState2 = 0;
- int buttonState3 = 0;
- int lastButtonState1 = 0; // previous state of each button
- int lastButtonState2 = 0;
- int lastButtonState3 = 0;
- unsigned long displayTimer = 6000; // timer to automatically turn on the display
- unsigned long lastDisplayTimer = 0;
- void setup() {
- // put your setup code here, to run once:
- dht.begin();
- pinMode(photocellPin, INPUT);
- pinMode(button1, INPUT);
- pinMode(button2, INPUT);
- pinMode(button3, INPUT);
- lcd.begin();
- lcd.clear();
- lcd.home();
- lcd.print(" Coop Command");
- lcd.setCursor(0, 1);
- lcd.print(" Control Center");
- lcd.setCursor(0, 2);
- lcd.print(" V1.0");
- lcd.setCursor(0, 3);
- lcd.print(" LCD TEST/MENU TEST");
- delay(2000);
- lcd.clear();
- }
- void ventFan() {
- if ((unsigned long)(millis() - lastDhtReadingTime) >= dhtDelay) {
- lastDhtReadingTime = millis();
- hum = dht.readHumidity();
- temp = dht.readTemperature();
- coopTemp = temp;
- }
- }
- void coopDoor() {
- photocellReading = analogRead(photocellPin);
- if ((unsigned long)(millis() - lastPhotocellReadingTime) >= photocellReadingDelay) {
- lastPhotocellReadingTime = millis();
- if (photocellReading >= 0 && photocellReading <= 20) {
- photocellReadingLevel = 1;
- }
- else if (photocellReading >= 125) {
- photocellReadingLevel = 0;
- }
- }
- }
- void readButtons() {
- buttonState1 = (digitalRead(button1));
- buttonState2 = (digitalRead(button2));
- buttonState3 = (digitalRead(button3));
- if (buttonState1 != lastButtonState1) {
- if (buttonState1 == LOW) { //if the enter/back button has been pressed
- lastDisplayTimer = millis();
- if (itemSelect == 0) { //if we are not in the settings change
- itemSelect = 1;
- }
- else if (itemSelect == 1) {
- itemSelect = 0;
- }
- }
- lastButtonState1 = buttonState1;
- }
- if (itemSelect == 0) { // if we are not adjusting settings
- if (buttonState2 != lastButtonState2) { //if the right button has been pressed
- if (buttonState2 == LOW) { //if the right button has been pressed
- lastDisplayTimer = millis();
- if (optionSelect <= 3) { //if we are not past the last menu screen
- optionSelect ++;
- }
- else if (optionSelect == 4) { // if we are at the last menu screen
- optionSelect = 0;
- }
- }
- lastButtonState2 = buttonState2;
- }
- if (buttonState3 != lastButtonState3) { //if the left button has been pressed
- if (buttonState3 == LOW) { //if the left button has been pressed
- lastDisplayTimer = millis();
- if (optionSelect > 0) { //if we are not at the first menu screen
- optionSelect --;
- }
- else if (optionSelect == 0) {
- optionSelect = 3;
- }
- }
- lastButtonState3 = buttonState3;
- }
- }
- else if (itemSelect == 1) { // if we are adjusting settings
- buttonState2 = (digitalRead(button2));
- buttonState3 = (digitalRead(button3));
- if (buttonState2 != lastButtonState2) { //if the right button has been pressed
- if (buttonState2 == LOW) { //if the right button has been pressed
- lastDisplayTimer = millis();
- if (optionSelect == 1) { //if we are adjusting the Vent Fan Temp
- hotTemp = (hotTemp + 5);
- }
- if (itemSelect == 2) { // if we are adjusting the Water Heater Temp
- coldTemp = (coldTemp + 1);
- }
- if (itemSelect == 3) { // if we are adjusting the Door Light Sensor
- closeDoor = (closeDoor + 5);
- }
- }
- lastButtonState2 = buttonState2;
- }
- if (buttonState3 != lastButtonState3) { //if the left button has been pressed
- if (buttonState3 == LOW) { //if the left button has been pressed
- lastDisplayTimer = millis();
- if (optionSelect == 1) { //if we are adjusting the Vent Fan Temp
- hotTemp = (hotTemp - 5);
- }
- if (itemSelect == 2) { // if we are adjusting the Water Heater Temp
- coldTemp = (coldTemp - 1);
- }
- if (itemSelect == 3) { // if we are adjusting the Door Light Sensor
- closeDoor = (closeDoor - 5);
- }
- }
- lastButtonState3 = buttonState3;
- }
- }
- }
- void displayMenu() {
- if (menuOn) { // if the display is supposed to be on
- lcd.display(); // turn the display on
- lcd.backlight();
- if (optionSelect != lastOptionSelect) {
- lcd.clear();
- lastOptionSelect = optionSelect;
- }
- else if (itemSelect != lastItemSelect) {
- lcd.clear();
- lastItemSelect = itemSelect;
- }
- switch (optionSelect) {
- case 0:
- lcd.home();
- lcd.print(" Coop Command");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print(" Control Center");
- lcd.setCursor(0, 3);
- lcd.print(" Firmware V1.0");
- break;
- case 1:
- if (itemSelect == 0) {
- lcd.home();
- lcd.print(" Coop Temp: ");
- lcd.print(coopTemp);
- lcd.print(" C");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print("Ventilation Fan: ");
- lcd.print("OFF");
- }
- if (itemSelect == 1) {
- lcd.home();
- lcd.print(" Coop Temp: ");
- lcd.print(coopTemp);
- lcd.print(" C");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print("Fan On Temp: ");
- lcd.print(hotTemp);
- }
- break;
- case 2:
- if (itemSelect == 0) {
- lcd.home();
- lcd.print(" Water Temp: ");
- lcd.print(waterTemp);
- lcd.print(" C");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print(" Water Heat: ");
- lcd.print("ON");
- }
- if (itemSelect == 1) {
- lcd.home();
- lcd.print(" Water Temp: ");
- lcd.print(waterTemp);
- lcd.print(" C");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print(" Heat On Temp: ");
- lcd.print(coldTemp);
- }
- break;
- case 3:
- if (itemSelect == 0) {
- lcd.home();
- lcd.print(" Coop Door: ");
- lcd.print("OPEN");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print(" Light Value: ");
- lcd.print(photocellReading);
- }
- if (itemSelect == 1) {
- lcd.home();
- lcd.print(" Coop Door: ");
- lcd.print("OPEN");
- lcd.setCursor(0, 1);
- lcd.print("< >");
- lcd.setCursor(0, 2);
- lcd.print(" Light Adjust: ");
- lcd.print(closeDoor);
- }
- break;
- }
- if ((unsigned long)(millis() - lastDisplayTimer) >= displayTimer) {
- menuOn = false;
- }
- }
- else if (!menuOn) { // if the display is supposed to be off
- lcd.noDisplay(); // turn the display off
- lcd.noBacklight();
- }
- if ((buttonState1 == 0) && (!menuOn)) {
- menuOn = true;
- lastDisplayTimer = millis();
- optionSelect = 0; //back to front screen
- itemSelect = 0;
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- readButtons();
- ventFan();
- coopDoor;
- displayMenu();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement