-
Notifications
You must be signed in to change notification settings - Fork 0
/
chicken_coop.ino
446 lines (381 loc) · 10.2 KB
/
chicken_coop.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <dht.h>
#include <TimeLib.h>
#include <LiquidCrystal.h>
// Init the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// For TimeSerial
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
// For button sensors when reading door open/close
int openDoorPin = 13;
int closeDoorPin = 10;
int toggleDoorPin = 30; //30
bool doorOpen = true;
// Open door values
int openDoorStateVal;
int openDoorStateVal_2;
int openDoorState;
// Close door values
int closeDoorStateVal;
int closeDoorStateVal_2;
int closeDoorState;
// Toggle button door values
int toggleDoorStateVal;
int toggleDoorStateVal_2;
int toggleDoorState;
bool toggleSwitch = false;
// Debounce values
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay(100);
// Motor inputs
int enA = 9;
int in1 = 8;
int in2 = 7;
unsigned long time_now = 0;
int period = 10000;
// Reads to turn on heat lamp
int relayPin = 22;
// Temperature & fan variables
dht DHT;
int maxTemp = 70;
float temp;
float humidity;
int fanPin = 24;
#define DHT11_PIN 26
// Month nested array with sunrise/sunset times
// No leading zeroes allowed
// Military time <12AM = 0>
int sunriseData[12][2] = {
{810, 1845}, //Jan
{734, 1935}, //Feb
{644, 2015}, //Mar
{558, 2040}, //Apr
{530, 2110}, //May
{530, 2115}, //Jun
{530, 2100}, //Jul
{556, 2045}, //Aug
{627, 2020}, //Sep
{656, 1910}, //Oct
{730, 1755}, //Nov
{85, 1815} //Dec
};
int month_;
int hour_;
int minute_;
int sunrise;
int sunset;
void setup() {
Serial.begin(9600);
// Set all motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Set Relay output
pinMode(relayPin, OUTPUT);
pinMode(fanPin, OUTPUT);
// Set motor speed
setMotorSpeed(255);
// Init the time library to await EPOCH INPUT
setUpTimeLibrary();
// Init the lcd
lcd.begin(16, 2);
// Init the close/open door button sensors
pinMode(openDoorPin, INPUT);
pinMode(closeDoorPin, INPUT);
pinMode(toggleDoorPin, INPUT);
}
// Write to the LCD
void writeToLCD(int column, int row, String sensor, String value){
lcd.setCursor(column, row);
lcd.print(sensor);
lcd.print(value);
}
// Set up time library
void setUpTimeLibrary (){
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Input the current time as T??????????");
}
// Realtime clockcounter
void timeClock(){
if (Serial.available()) {
processSyncMessage();
}
if (timeStatus()!= timeNotSet) {
getTime();
}
}
void getTime() {
// digital clock display of the time
// Serial.print(hour());
// Serial.print(":");
// Serial.print(minute());
// Serial.print(":");
// Serial.print(second());
// Serial.print(" ");
// Serial.print(month());
// Serial.print(" ");
// Serial.print(day());
// Serial.print(" ");
// Serial.print(year());
// Serial.println();
month_ = month() - 1;
hour_ = hour();
minute_ = minute();
// Save sunset/sunrise times for the month
sunrise = checkSunriseTime();
sunset = checkSunsetTime();
handleDoor(); // Check to open/close door
}
// When the time reaches sunrise or sunset - open/close the door
void handleDoor() {
// Concat the current hour and minute
char hour_string[50];
sprintf(hour_string, "%d", hour_);
char minute_string[50];
sprintf(minute_string, "%d", minute_);
strcat(hour_string, minute_string);
// Convert hour_string back to int
int current_time = atoi(hour_string);
// Open and close door at sunset/sunrise
// #TODO take out the toggle switch condition
if(current_time == sunrise && !doorOpen && !toggleSwitch) {
openDoor();
}
else if(current_time == sunset && doorOpen && !toggleSwitch) {
closeDoor();
}
}
// Handle the door toggle switch
void handleDoorToggle() {
if(toggleDoorState != toggleSwitch) { // If the button is pressed and the door is not currently opening
if(toggleDoorState == 1) { // Button is pressed
//Serial.println("Outside button pressed");
toggleSwitch = true;
}
}
// Open door by outside toggle switch <1 complete press>
if(toggleSwitch && toggleDoorState == 0) {
if(doorOpen) {
closeDoor();
}
else {
openDoor();
}
// Serial.println("Toggle engage");
}
// If user started toggle switch & is holding the switch, stop the motor
else if(toggleSwitch && toggleDoorState == 1) {
//Serial.println("Outside button is held - pausing motor");
stopMotor();
}
}
// Recieves the user inputted current time and sets it within internal clock
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
long int converted_time = convertTimestamp(pctime);
if(converted_time >= DEFAULT_TIME) { // Check the integer is a valid time (greater than Jan 1 2013)
setTime(converted_time); // Sync Arduino clock to the time received on the serial port
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
}
}
time_t requestSync() {
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
// @return today's sunrise time
int checkSunriseTime() {
return sunriseData[month_][0];
}
// @return today's sunset time
int checkSunsetTime() {
return sunriseData[month_][1];
}
// Stops the motor
void stopMotor(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
// @param speed , 0-255
void setMotorSpeed(int speed) {
analogWrite(enA, speed);
}
// Close the door
void closeDoor() {
// Turn on the motor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
Serial.println("Closing Door");
if(closeDoorState == 1) {
stopMotor();
toggleSwitch = false;
doorOpen = false;
Serial.println("DOOR HAS CLOSED - STOPPING MOTOR");
}
}
// Open the door
void openDoor() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.println("Opening Door");
if(openDoorState == 1) {
stopMotor();
toggleSwitch = false;
doorOpen = true;
Serial.println("DOOR HAS OPENED - STOPPING MOTOR");
}
}
// Close the door
void testCloseDoor() {
// Turn on the motor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
Serial.println("Closing Door");
if(closeDoorState == 1) {
stopMotor();
doorOpen = false;
Serial.println("DOOR HAS CLOSED - STOPPING MOTOR");
delay(3000);
}
}
// Open the door
void testOpenDoor() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.println("Opening Door");
if(openDoorState == 1) {
stopMotor();
doorOpen = true;
Serial.println("DOOR HAS OPENED - STOPPING MOTOR");
delay(3000);
}
}
void testDoor() {
if(!doorOpen) {
testOpenDoor();
}
else {
testCloseDoor();
}
}
void debounceOpenDoorState(){
openDoorStateVal = digitalRead(openDoorPin);
if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) {
openDoorStateVal_2 = digitalRead(openDoorPin);
if(openDoorStateVal == openDoorStateVal_2) {
if(openDoorStateVal != openDoorState) {
openDoorState = openDoorStateVal;
}
//Serial.println(openDoorState);
}
}
}
void debounceCloseDoorState(){
closeDoorStateVal = digitalRead(closeDoorPin);
if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) {
closeDoorStateVal_2 = digitalRead(closeDoorPin);
if(closeDoorStateVal == closeDoorStateVal_2) {
if(closeDoorStateVal != closeDoorState) {
closeDoorState = closeDoorStateVal;
}
//Serial.println(closeDoorState);
}
}
}
void debounceToggleDoorState(){
toggleDoorStateVal = digitalRead(toggleDoorPin);
if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) {
toggleDoorStateVal_2 = digitalRead(toggleDoorPin);
if(toggleDoorStateVal == toggleDoorStateVal_2) {
if(toggleDoorStateVal != toggleDoorState) {
toggleDoorState = toggleDoorStateVal;
}
//Serial.print("Outside Door Button Status: ");
//Serial.println(toggleDoorState);
}
}
}
void readTempHumid() {
int chk = DHT.read11(DHT11_PIN);
temp = (DHT.temperature * 9/5) + 32;
// Serial.print("Temperature = ");
// Serial.println(temp);
humidity = DHT.humidity;
// Serial.print("Humidity = ");
// Serial.print(DHT.humidity);
// Serial.println("%");
//delay(1000);
}
// Toggle light using temperature
void toggleLight() {
if(temp <= maxTemp) {
//Serial.println("Light on");
digitalWrite(relayPin, LOW);
}
else {
//Serial.println("Light off");
digitalWrite(relayPin, HIGH);
}
}
// Toggle the fan based on temp/humidity
void toggleFan(){
if(humidity >= 40 && temp < 80) {
//Serial.println("Fan on");
digitalWrite(fanPin, LOW);
}
else if(temp >= 80 && humidity <= 40) {
//Serial.println("Fan on");
digitalWrite(fanPin, LOW);
}
else if(temp >= 80 || humidity >= 40){
//Serial.println("Fan on");
digitalWrite(fanPin, LOW);
}
else if(temp < 80 && humidity < 40){ // Turn fan off
//Serial.println("Fan off");
digitalWrite(fanPin, HIGH);
}
}
// Convert GMT to our timezone and add T and leading zero
long int convertTimestamp(long int GMT_time){
long int converted_time = GMT_time - 14400;
int digitCount = floor(log10(abs(converted_time))) + 1;
if(digitCount < 10) {
converted_time *= pow(10, digitCount);
}
Serial.print("Newly converted local time: ");
Serial.println(converted_time);
return converted_time;
}
// USed to convert temp & humidity values to string for LCD
String dhtFloatToStr(float value) {
char tempStr[16];
dtostrf(value, 1, 1, tempStr);
return (String) tempStr;
}
void loop() {
debounceCloseDoorState();
debounceOpenDoorState();
debounceToggleDoorState();
readTempHumid();
toggleLight();
toggleFan();
timeClock();
writeToLCD(0,0,"Temp: ", dhtFloatToStr(temp));
writeToLCD(0,1,"Humidity: ", dhtFloatToStr(humidity));
handleDoorToggle();
}