-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoop.ino
247 lines (227 loc) · 6.46 KB
/
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
/*
RaggedPi Project
Arduino Uno "Vanilla" - Chicken Coop
Written by david durost <[email protected]>
*/
/* Includes */
#include <SoftwareSerial.h> // Serial library
#include <Relay.h> // Relay library
#include <Servo.h> // Servo libraryu
#include <Wire.h> // one wire library
#include <Time.h> // time library
#include "DS3231/DS3231.h" // rtc library
#include <OneButton.h> // OneButton library
/* Constants */
// Misc
#define SDA 2 // SDA
#define SDL 3 // SDL
#define CS 10 // Chipselect
#define LED 13 // LED pin
// Pins
#define LDR_PIN A1 // Analog pin
#define HEAT_PIN RELAY2 // Digital pin
#define FAN_PIN RELAY3 // Digital pin
#define LIGHT_PIN RELAY4 // Digital pin
#define SERVO_PIN 12 // Digital pin
#define LIGHT_BTN_PIN 9 // Digital pin
#define HEAT_BTN_PIN 10 // Digital pin
#define FAN_BTN_PIN 11 // Digital pin
#define DOOR_BTN_PIN 13 // Digital pin
// Delimiters
#define OPEN 180 // Door open position
#define CLOSED 0 // Door closed position
#define FAN 0 // delimiter
#define HEATLAMP 1 // delimiter
#define LIGHT 2 // delimiter
#define DOOR 3 // delimiter
#define C 0 // delimiter
#define F 1 // delimiter
#define CURRENT 0 // delimiter
#define PREVIOUS 1 // delimiter
// Settings
#define DHT_F true // Use fahrenheit
#define LDR_READ_WAIT 60000 // ms
/* Variables */
Relay relays[3] = { // relays
Relay(FAN_PIN, true),
Relay(HEAT_PIN, true),
Relay(LIGHT_PIN, true)
};
Servo doorServo; // servo
OneButton buttons[4] = { // buttons
OneButton(FAN_BTN_PIN, true),
OneButton(HEAT_BTN_PIN, true),
OneButton(LIGHT_BTN_PIN, true),
OneButton(DOOR_BTN_PIN, true)
};
RTClib rtc; // read time clock
DateTime cTime; // current time
DS3231 thermometer; // thermometer
int LDRReading[2]; // LDR sensor
int LDRReadingLevel[2]; // LDR sensor level
int doorPos = 0; // door position
bool override[4] = { 0, 0, 0, 0 }; // overrides
float temp[2]; // temperatures
double tempSetting[2]; // temperature settings
unsigned long lastLDRReadTime = 0; // ms
unsigned long lastTempCheckTime = 0; // ms
/* Utility Methods */
/**
* Move door
* @param int moveTo
*/
void moveDoor(int moveTo) {
doorServo.write(moveTo);
doorPos = moveTo;
}
/*
0-3 dark
4-120 twilight
121+ light
*/
/**
* Is dusk
* @return bool
*/
bool isDusk() {
if ((cTime.unixtime() - lastLDRReadTime) <= LDR_READ_WAIT) readLDR();
return ((LDRReadingLevel[CURRENT] == 2) && (LDRReading[CURRENT] < LDRReading[PREVIOUS]));
}
/**
* Is dawn
* @return bool
*/
bool isDawn() {
if ((cTime.unixtime() - lastLDRReadTime) <= LDR_READ_WAIT) readLDR();
return ((LDRReadingLevel[CURRENT] == 2) && (LDRReading[CURRENT] > LDRReading[PREVIOUS]));
}
/**
* Fahrenheit to celsius
* @param float c
* @return float
*/
float fahrenheitToCelsius(float c) {
return c * 9 / 5 + 32;
}
/* Toggle Methods */
/**
* Toggle door
*/
void toggleDoor() {
if (doorPos == CLOSED) moveDoor(OPEN);
else moveDoor(CLOSED);
}
/**
* Toggle heat lamp
*/
void toggleHeatLamp() {
if (relays[HEATLAMP].isOff()) relays[HEATLAMP].on();
else relays[HEATLAMP].off();
}
/**
* Toggle fan
*/
void toggleFan() {
if (relays[FAN].isOff()) relays[FAN].on();
else relays[FAN].off();
}
/**
* Toggle light
*/
void toggleLight() {
if (relays[LIGHT].isOff()) relays[LIGHT].on();
else relays[LIGHT].off();
}
/* Read Methods */
/**
* Read temperature
*/
void readTemps() {
temp[C] = thermometer.getTemperature();
temp[F] = fahrenheitToCelsius(temp[C]);
if (isnan(temp[C])) temp[C] = 0.00;
else if (isnan(temp[F])) temp[F] = 32.00;
lastTempCheckTime = cTime.unixtime();
}
/**
* Read photocells
*/
void readLDR() {
LDRReading[PREVIOUS] = LDRReading[CURRENT];
LDRReading[CURRENT] = analogRead(LDR_PIN);
lastLDRReadTime = cTime.unixtime();
LDRReadingLevel[PREVIOUS] = LDRReadingLevel[CURRENT];
if (LDRReading[CURRENT] >= 0 && LDRReading[CURRENT] <= 3)
LDRReadingLevel[CURRENT] = 1;
else if (LDRReading[CURRENT] >= 4 && LDRReading[CURRENT] <= 120)
LDRReadingLevel[CURRENT] = 2;
else if (LDRReading[CURRENT] >= 125 )
LDRReadingLevel[CURRENT] = 3;
}
/* Print Methods */
/**
* Print temps
*/
void printTemps(int CorF=3) {
if (C == CorF || 3 == CorF) {
Serial.print("Temp: ");
Serial.print(temp[C], 1);
Serial.println("C\t");
}
if (F == CorF || 3 == CorF) {
Serial.print("Temp: ");
Serial.print(temp[F], 1);
Serial.println("F\t");
}
}
/**
* Print photocells
*/
void printLDR() {
Serial.print("LDR Sensor Reading: ");
Serial.println(LDRReading[CURRENT], 1);
Serial.print("LDR Sensor Reading Level: ");
if (1 == LDRReadingLevel[CURRENT]) Serial.println("Dark");
else if (2 == LDRReadingLevel[CURRENT]) Serial.println("Twilight");
else if (3 == LDRReadingLevel[CURRENT]) Serial.println("Light");
else Serial.println("error obtaining reading.");
}
/**
* Setup
*/
void setup() {
Serial.begin(9600);
while (!Serial);
Wire.begin();
pinMode(LED, OUTPUT);
pinMode(CS, OUTPUT);
pinMode(LDR_PIN, INPUT);
pinMode(SERVO_PIN, OUTPUT);
Serial.println("RaggedPi Project Codename Vanilla Initializing...");
for (int x=0; x<sizeof(relays); x++) relays[x].begin();
delay(600);
buttons[DOOR].attachClick(toggleDoor);
buttons[LIGHT].attachClick(toggleLight);
buttons[HEATLAMP].attachClick(toggleHeatLamp);
buttons[FAN].attachClick(toggleFan);
delay(600);
}
/**
* Loop
*/
void loop() {
cTime = now();
for (int x=0; x<sizeof(buttons); x++) buttons[x].tick();
// Get temperature
readTemps();
// Heatlamp
if ((temp[F] < tempSetting[HEATLAMP])) relays[HEATLAMP].on();
else relays[HEATLAMP].off();
// Exaust Fan
if ((temp[F] >= tempSetting[FAN])) relays[FAN].on();
else relays[FAN].off();
// Door
if (isDawn()) moveDoor(OPEN);
if (isDusk()) moveDoor(CLOSED);
delay(3000);
}