-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevapCooler.ino
286 lines (263 loc) · 7.13 KB
/
evapCooler.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
#include <Wire.h>
#include <Stepper.h>
#include "uRTCLib.h"
#include <LiquidCrystal.h>
#include <DHT.h>
#include <DHT_U.h>
#include "Arduino.h"
#define TBE 0x20
#define RDA 0x80
#define THSENSPIN 7
#define DHTTYPE DHT11
#define WRITE_HIGH_PD(pin_num) *port_d |= (0x01 << pin_num);
#define WRITE_LOW_PD(pin_num) *port_d &= ~(0x01 << pin_num);
#define WRITE_HIGH_PA(pin_num) *port_a |= (0x01 << pin_num);
#define WRITE_LOW_PA(pin_num) *port_a &= ~(0x01 << pin_num);
#define WRITE_HIGH_PK(pin_num) *port_k |= (0x01 << pin_num);
#define WRITE_LOW_PK(pin_num) *port_k &= ~(0x01 << pin_num);
bool timer = false;
bool isIdle = false;
bool enabled = false;
bool test = false;
bool consoleMessage = false;
const float stepNum = 50;
unsigned long int delayStart = 0;
unsigned long int tempHigh = 100;
unsigned long int waterLow = 100;
volatile unsigned long int isr_delay = 0;
volatile unsigned long int startStop_count = 0;
volatile unsigned char *port_d = (unsigned char *)0x2B;
volatile unsigned char *ddr_d = (unsigned char *)0x2A;
volatile unsigned char *pin_d = (unsigned char *)0x29;
volatile unsigned char *my_ADMUX = (unsigned char *)0x7C;
volatile unsigned char *my_ADCSRB = (unsigned char *)0x7B;
volatile unsigned char *my_ADCSRA = (unsigned char *)0x7A;
volatile unsigned int *my_ADC_DATA = (unsigned int *)0x78;
volatile unsigned char *port_k = (unsigned char *)0x108;
volatile unsigned char *ddr_k = (unsigned char *)0x107;
volatile unsigned char *pin_k = (unsigned char *)0x106;
volatile unsigned char *myUCSR0A = (unsigned char *)0x00C0;
volatile unsigned char *myUCSR0B = (unsigned char *)0x00C1;
volatile unsigned char *myUCSR0C = (unsigned char *)0x00C2;
volatile unsigned int *myUBRR0 = (unsigned int *)0x00C4;
volatile unsigned char *myUDR0 = (unsigned char *)0x00C6;
volatile unsigned char *port_b = (unsigned char *)0x25;
volatile unsigned char *ddr_b = (unsigned char *)0x24;
volatile unsigned char *pin_b = (unsigned char *)0x23;
volatile unsigned char *port_a = (unsigned char *)0x22;
volatile unsigned char *ddr_a = (unsigned char *)0x21;
volatile unsigned char *pin_a = (unsigned char *)0x20;
uRTCLib rtc(0x68);
Stepper VENTMOTOR(stepNum, 2, 4, 3, 5);
LiquidCrystal display(8, 9, 10, 11, 12, 13);
DHT THSENS(THSENSPIN, DHTTYPE);
void setup() {
*ddr_a |= 0b00101010;
U0init(9600);
THSENS.begin();
display.begin(16, 2);
display.clear();
VENTMOTOR.setSpeed(500);
adc_init();
URTCLIB_WIRE.begin();
rtc.set(0, 40, 11, 3, 7, 12, 22);
}
void loop() {
bool startStop = *pin_d & 0b00000100;
attachInterrupt(digitalPinToInterrupt(19), myISR, FALLING);
if(startStop & !enabled){
enabled = true;
delayStart = millis() - 60000;
Serial.println("State has changed...");
Serial.println(startStop_count);
} else if (startStop){
consoleMessage = false;
enabled = false;
Serial.println("State has changed...");
Serial.println(startStop_count);
}
bool button_1 = *pin_d & 0b00001000;
bool button_2 = *pin_d & 0b00010000;
if (!consoleMessage){
if (button_1) {
VENTMOTOR.step(50);
Serial.println("Vent raising");
printTime();
} else if (button_2) {
VENTMOTOR.step(-50);
Serial.println("Vent lowering");
printTime();
}
}
if (!consoleMessage && ((millis() - delayStart) >= 60000) && !enabled) {
display.clear();
delayStart = millis();
} else if (!consoleMessage && ((millis() - delayStart) >= 60000) && enabled) {
display.clear();
delayStart = millis();
float humidity = THSENS.readHumidity();
float temp_celsius = THSENS.readTemperature();
float temp_farenheit = THSENS.readTemperature(true);
display.print("Temp : ");
display.print(temp_farenheit);
if (temp_farenheit > tempHigh) {
isIdle = false;
} else {
isIdle = true;
}
display.print("\337F");
display.setCursor(0, 1);
display.print("Humid %: ");
display.print(humidity);
}
unsigned int adc_reading = adc_read(0);
if (enabled && !isIdle && adc_reading < waterLow){
consoleMessage = true;
}
if ((adc_reading <= waterLow) && enabled){
isIdle = false;
consoleMessage = true;
display.clear();
display.setCursor(0, 0);
display.print("Please refill");
display.setCursor(0, 1);
display.print("water");
}
if (enabled && !consoleMessage && !isIdle) {
if (!timer){
Serial.print("Fan enabled at time...");
printTime();
timer = true;
}
test = true;
*port_a |= 0b00001010;
} else if (enabled && isIdle) {
*port_a &= 0b11110101;
if (timer){
Serial.print("Fan enabled, error at time...");
printTime();
timer = false;
}
test = true;
} else {
*port_a &= 0b11110101;
timer = false;
if (test){
Serial.print("Cooler turned off");
printTime();
test = false;
}
}
if (!enabled){
display.clear();
}
statusLEDs();
delay(1000);
}
void statusLEDs() {
if (consoleMessage) {
WRITE_HIGH_PK(6);
} else {
WRITE_LOW_PK(6);
}
if (isIdle & !consoleMessage & enabled) {
WRITE_HIGH_PK(7);
} else {
WRITE_LOW_PK(7);
}
if (!enabled) {
WRITE_HIGH_PK(4);
} else {
WRITE_LOW_PK(4);
}
if (!isIdle && enabled && !consoleMessage){
WRITE_HIGH_PK(5);
} else {
WRITE_LOW_PK(5);
}
}
// External Functions
void myISR() {
if ((millis() - isr_delay) >= 1000) {
isr_delay = millis();
startStop_count++;
}
}
void printTime(){
rtc.refresh();
Serial.print(rtc.year());
Serial.print('/');
Serial.print(rtc.month());
Serial.print('/');
Serial.print(rtc.day());
Serial.print(' ');
Serial.print(rtc.hour());
Serial.print(':');
Serial.print(rtc.minute());
Serial.print(':');
Serial.print(rtc.second());
}
void adc_init() {
*my_ADCSRA |= 0b10000000;
*my_ADCSRA &= 0b11011111;
*my_ADCSRA &= 0b11110111;
*my_ADCSRA &= 0b11111000;
*my_ADCSRB &= 0b11110111;
*my_ADCSRB &= 0b11111000;
*my_ADMUX &= 0b01111111;
*my_ADMUX |= 0b01000000;
*my_ADMUX &= 0b11011111;
*my_ADMUX &= 0b11100000;
}
unsigned int adc_read(unsigned char adc_channel_num) {
*my_ADMUX &= 0b11100000;
*my_ADCSRB &= 0b11110111;
if (adc_channel_num > 7) {
adc_channel_num -= 8;
*my_ADCSRB |= 0b00001000;
}
*my_ADMUX += adc_channel_num;
*my_ADCSRA |= 0x40;
while ((*my_ADCSRA & 0x40) != 0);
return *my_ADC_DATA;
}
void print_int(unsigned int out_num) {
unsigned char print_flag = 0;
if (out_num >= 1000) {
U0putchar(out_num / 1000 + '0');
print_flag = 1;
out_num = out_num % 1000;
}
if (out_num >= 100 || print_flag) {
U0putchar(out_num / 100 + '0');
print_flag = 1;
out_num = out_num % 100;
}
if (out_num >= 10 || print_flag) {
U0putchar(out_num / 10 + '0');
print_flag = 1;
out_num = out_num % 10;
}
U0putchar(out_num + '0');
U0putchar('\n');
}
void U0init(int U0baud) {
unsigned long FCPU = 16000000;
unsigned int tbaud;
tbaud = (FCPU / 16 / U0baud - 1);
*myUCSR0A = 0x20;
*myUCSR0B = 0x18;
*myUCSR0C = 0x06;
*myUBRR0 = tbaud;
Serial.begin(9600);
}
unsigned char U0kbhit() {
return *myUCSR0A & RDA;
}
unsigned char U0getchar() {
return *myUDR0;
}
void U0putchar(unsigned char U0pdata) {
while ((*myUCSR0A & TBE) == 0);
*myUDR0 = U0pdata;
}