//Locking, Automatic Chicken Coop Door
//By Mike Roberts 2017 (based off of original code by Seth Johnson Land To House llc 2016)
#include <Arduino.h>
//#include <ElegantOTA.h>
//#include <WiFi.h>
//#include <WiFiClient.h>
//#include <WebServer.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <DallasTemperature.h>
#define CoolRelay 32
#define HeatRelay 33
#define ONE_WIRE_BUS 4
const boolean SerialDisplay = true; // print debug messages or not to serial
//This section is for initializing things:
//const int led = 13;
int tcrt;
const int PWM = 26; //enables PWM on the drv8801 motor controller.
const int DIR = 27; //send high or low to DIR on the motor controller.
const int red = 14; // red LED heating
const int blue = 12; // blue LED cooling
const int coopDoorOpenLed = 13;
const int coopDoorClosedLed = 23;
const int lightSensorPin = 25; //Initialize "lightSensor" This is the photoresistor.
const int BotSwtch = 18;
const int TopSwtch = 19;
int lightVal = 0;
int BotSwtchState = 0;
int TopSwtchState = 0; //These are the variables that hold the state of the reed switches
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Set WiFi Credentials
//const char *ssid = "Wi-Fi Fo Fum";
//const char *password = "****";
//WebServer server(80);
void setup() //This only runs once.
{
Serial.begin(115200); // initialize the serial communications:
//connect to WiFi
/* WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
*/
//Begin OTA
// ElegantOTA.begin(&server); // Start ElegantOTA
// server.begin();
// Serial.println("HTTP server started");
lcd.init(); // set up the LCD's number of columns and rows:
lcd.backlight();
sensors.begin();
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(CoolRelay, OUTPUT);
pinMode(HeatRelay, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(DIR, OUTPUT); // set the motor control pins as outputs. This means pins 9, 10 are outputs to the drv8801 motor controller.
pinMode(coopDoorOpenLed, OUTPUT);
pinMode(coopDoorClosedLed, OUTPUT);
pinMode(BotSwtch, INPUT_PULLUP);
pinMode(TopSwtch, INPUT_PULLUP);
BotSwtchState = digitalRead(BotSwtch);
TopSwtchState = digitalRead(TopSwtch);
// TestBtnState = digitalRead(TestBtn);
/*
if (BotSwtchState != HIGH) //if BotSwtchState is not high then go to the while statement
{
while (BotSwtchState != HIGH) // this function will run the motor down as long as switch 1 has not been triggered
{
analogWrite(PWM, 200); // turn on motor and set speed to 255
digitalWrite(DIR, LOW); // DIR LOW=down HIGH=up
BotSwtchState = digitalRead(BotSwtch); //read the state of the switch again to make sure that it has not been triggered
TopSwtchState = digitalRead(TopSwtch); //read the state of switch 2 (the top one) and place it in TopSwtchState
}
delay(1000);
analogWrite(PWM, 0); // once TopSwtchState has been triggered turn off the motor
digitalWrite(coopDoorClosedLed, HIGH); // turns on coopDoorClosedLed (green)
digitalWrite(coopDoorOpenLed, LOW);
} */
}
void motordown() //code for sending door down
{
BotSwtchState = digitalRead(BotSwtch);
while (BotSwtchState != HIGH) {
analogWrite(PWM, 200);
digitalWrite(DIR, LOW);
BotSwtchState = digitalRead(BotSwtch);
}
delay(1000); //wait 2 seconds before turning off the motor to let the locks engage at the bottom
analogWrite(PWM, 0); // now turn off motor
digitalWrite(coopDoorClosedLed, HIGH); // turns on coopDoorClosedLed (green)
digitalWrite(coopDoorOpenLed, LOW); // turns off coopDoorOpenLed (red)
}
void motorup() // this function will run the motor as long as switch 2 has not been triggered
{
BotSwtchState = digitalRead(BotSwtch);
TopSwtchState = digitalRead(TopSwtch);
while (TopSwtchState != HIGH) {
analogWrite(PWM, 200);
digitalWrite(DIR, HIGH);
TopSwtchState = digitalRead(TopSwtch);
}
analogWrite(PWM, 0);
digitalWrite(coopDoorClosedLed, LOW); // turns on coopDoorClosedLed (green)
digitalWrite(coopDoorOpenLed, HIGH); // turns off coopDoorOpenLed (red)
}
//this runs over and over
void loop() {
// Handle OTA
// server.handleClient();
// ElegantOTA.loop();
//Code for OTA
sensors.requestTemperatures();
float temperature = sensors.getTempFByIndex(0);
lightVal = analogRead(lightSensorPin); //read the light sensor and place it in lightval
lcd.home();
char buffer[50];
sprintf(buffer, "LSV: %4d", (analogRead(lightSensorPin))); // was A0 which is now 19 try 19 if lightSensorPin doesnt work
lcd.setCursor(0, 0);
lcd.print(buffer);
lcd.setCursor(10, 0);
lcd.print("T:"); // sprintf(buffer, "Temp: %f\337F", temperature);
lcd.print(sensors.getTempFByIndex(0));
lcd.print("\337F"); // lcd.print (buffer);
Serial.print(" Light sensor value: ");
Serial.println(analogRead(lightSensorPin));
Serial.print(" ");
Serial.print(sensors.getTempFByIndex(0));
Serial.println("\337F");
if (temperature < 40) //changed temp to 40
{
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
Serial.println(" Heating");
digitalWrite(CoolRelay, 0);
digitalWrite(HeatRelay, 1);
delay(30000);
} else if (temperature > 40 && temperature < 85) //changed temp to 40
{
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
Serial.println(" All Off");
digitalWrite(CoolRelay, 0);
digitalWrite(HeatRelay, 0);
} else if (temperature > 85) {
digitalWrite(CoolRelay, 1);
digitalWrite(HeatRelay, 0);
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
Serial.println(" Cooling ");
}
BotSwtchState = digitalRead(BotSwtch);
Serial.print(" Bottom Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(BotSwtch));
sprintf(buffer, "Bot Sw: %d", BotSwtchState);
lcd.setCursor(0, 1);
lcd.print(buffer);
TopSwtchState = digitalRead(TopSwtch);
Serial.print(" Top Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(TopSwtch));
sprintf(buffer, "Top Sw: %d", TopSwtchState);
lcd.setCursor(0, 2);
lcd.print(buffer);
if (TopSwtchState == HIGH && lightVal < 45) {
delay(30000);
motordown();
} else if (BotSwtchState == HIGH && lightVal > 600) {
delay(30000);
motorup();
}
}