Add door reed switch

This commit is contained in:
ivan 2018-05-12 18:06:17 +03:00
parent ec9e35e700
commit 0c228f2c28
3 changed files with 149 additions and 115 deletions

Binary file not shown.

View File

@ -1,26 +1,27 @@
/*
* Hencoop with automatic lamp and door timer, TM1637 display and two buttons
* Hencoop with automatic lamp and door timer, TM1637 display, two buttons and magnetically operated sealed switch
*/
#include <Arduino.h>
#include <Wire.h>
#include <TimeLib.h>
#include <Thread.h>
#include <DS1307RTC.h>
#include <SevenSegmentExtended.h> // Extended TM1637 library https://github.com/bremme/arduino-tm1637
long openDoorVar = 3780; // Interval for door opening
long closeDoorVar = 3600; // Interval for door closing
long maxOpenDoorVar = 8765; // Interval for maximum door opening time
// long openDoorVar = 3750; // Interval for door opening
long closeDoorVar = 3999; // Interval for door closing
const byte PIN_CLK = 2; // Define CLK pin (for 4-Digit Display)
const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display)
const byte redButton = 4; // RedButton: light on, open door
const byte blackButton = 5; // BlackButton: light off, close door
const byte pinLight = 6; // Light relay
const byte pinDC = 7; // DC motor relay
const byte pinLight = 6; // Light power relay
const byte pinDC = 7; // DC motor power relay
const byte pinRelay3 = 8; // Motor relay control
const byte pinRelay4 = 9; // Motor relay control
const byte led = 13;
const byte PIN_CLK = 10; // Define CLK pin (for 4-Digit Display)
const byte PIN_DIO = 11; // Define DIO pin (for 4-Digit Display)
const byte doorSwitch = 12; // Door's magnetically operated sealed switch
// const byte led = 13;
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
@ -50,9 +51,10 @@ void setup() {
pinMode(pinRelay4, OUTPUT);
pinMode(pinDC, OUTPUT);
pinMode(pinLight, OUTPUT);
pinMode(led, OUTPUT);
// pinMode(led, OUTPUT);
pinMode(redButton, INPUT);
pinMode(blackButton, INPUT);
pinMode(doorSwitch, INPUT);
digitalWrite(pinRelay3, HIGH);
digitalWrite(pinRelay4, HIGH);
digitalWrite(pinDC, HIGH);
@ -103,93 +105,6 @@ void loop() {
*/
}
void checkTime() {
// Winter
// 6:56 open door, 7:01 light on, 10:01 light off,
// 16:01 light on, 19:01 light off, 19:05 close door
if (month()==11 || month()==12 || month()==1 ) {
if (hour() == 6 && minute() == 56) {
openDoor();
delay(60000);
}
if (hour() == 7 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 10 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 16 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 19 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 19 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Spring
// 6:30 door open, 17:01 light on, 20:01 light off, 20:05 door close
if (month()==2 || month()==3 || month()==4 ) {
if (hour() == 6 && minute() == 30) {
openDoor();
delay(60000);
}
if (hour() == 17 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 20 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 20 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Summer
// 5:30 open door, 22:05 close door
if (month()==5 || month()==6 || month()==7 ) {
if (hour() == 5 && minute() == 30) {
openDoor();
delay(60000);
}
if (hour() == 22 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Autumn
// 6:01 open door, 17:01 light on, 20:01 light off, 21:05 close door
if (month()==8 || month()==9 || month()==10 ) {
if (hour() == 6 && minute() == 1) {
openDoor();
delay(60000);
}
if (hour() == 17 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 20 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 21 && minute() == 5) {
closeDoor();
delay(60000);
}
}
}
/*
// LED pulse blinking thread
void ledBlink() {
@ -251,27 +166,59 @@ void lightOff() {
}
void openDoor() {
serStr("Door opening started...");
digitalWrite(pinDC, LOW); // DC on
delay(1000);
digitalWrite(pinRelay4, LOW);
delay(openDoorVar);
digitalWrite(pinRelay4, HIGH);
delay(1000);
digitalWrite(pinDC, HIGH); // DC off
serStr("...door opening finished");
unsigned long openStart = millis();
if (digitalRead(doorSwitch) == LOW) {
serStr("Door opening started...");
digitalWrite(pinRelay3, HIGH);
digitalWrite(pinRelay4, HIGH);
delay(1000);
digitalWrite(pinDC, LOW); // DC on
delay(3000);
digitalWrite(pinRelay4, LOW);
while (digitalRead(doorSwitch) == LOW) {
if ((millis() - openStart) > maxOpenDoorVar) {
break;
}
delay(55);
}
digitalWrite(pinRelay4, HIGH);
delay(2000);
digitalWrite(pinDC, HIGH); // DC off
delay(1000);
digitalWrite(pinRelay3, HIGH);
digitalWrite(pinRelay4, HIGH);
serStr("...door opening finished");
} else {
serStr("Can't open door, magnet shows that door is open");
display.on();
display.print("can't - door is open");
display.off();
}
}
void closeDoor() {
serStr("Door closing started...");
digitalWrite(pinDC, LOW); // DC on
delay(1000);
digitalWrite(pinRelay3, LOW);
delay(closeDoorVar);
digitalWrite(pinRelay3, HIGH);
delay(1000);
digitalWrite(pinDC, HIGH); // DC off
serStr("...door closing finished");
if (digitalRead(doorSwitch) == HIGH) {
serStr("Door closing started...");
digitalWrite(pinRelay3, HIGH);
digitalWrite(pinRelay4, HIGH);
delay(1000);
digitalWrite(pinDC, LOW); // DC on
delay(3000);
digitalWrite(pinRelay3, LOW);
delay(closeDoorVar);
digitalWrite(pinRelay3, HIGH);
delay(2000);
digitalWrite(pinDC, HIGH); // DC off
delay(1000);
digitalWrite(pinRelay3, HIGH);
digitalWrite(pinRelay4, HIGH);
serStr("...door closing finished");
} else {
serStr("Can't close door, magnet shows that door is closed");
display.on();
display.print("can't - door is closed");
display.off();
}
}
// Send string to serial monitor with millis() counter and date/time
@ -295,3 +242,90 @@ void serStr(const char* serString) {
Serial.print(year());
Serial.println();
}
void checkTime() {
// Winter
// 6:56 open door, 7:01 light on, 10:01 light off,
// 16:01 light on, 19:01 light off, 19:05 close door
if (month()==11 || month()==12 || month()==1 ) {
if (hour() == 6 && minute() == 56) {
openDoor();
delay(60000);
}
if (hour() == 7 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 10 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 16 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 19 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 19 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Spring
// 6:30 door open, 17:01 light on, 20:01 light off, 20:05 door close
if (month()==2 || month()==3 || month()==4 ) {
if (hour() == 6 && minute() == 30) {
openDoor();
delay(40000);
}
if (hour() == 17 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 20 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 20 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Summer
// 5:30 open door, 22:05 close door
if (month()==5 || month()==6 || month()==7 ) {
if (hour() == 5 && minute() == 30) {
openDoor();
delay(60000);
}
if (hour() == 22 && minute() == 5) {
closeDoor();
delay(60000);
}
}
// Autumn
// 6:01 open door, 17:01 light on, 20:01 light off, 21:05 close door
if (month()==8 || month()==9 || month()==10 ) {
if (hour() == 6 && minute() == 1) {
openDoor();
delay(60000);
}
if (hour() == 17 && minute() == 1) {
lightOn();
delay(60000);
}
if (hour() == 20 && minute() == 1) {
lightOff();
delay(60000);
}
if (hour() == 21 && minute() == 5) {
closeDoor();
delay(60000);
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 KiB

After

Width:  |  Height:  |  Size: 258 KiB