This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathON_OFF_Controller_English.ino
232 lines (186 loc) · 5.07 KB
/
ON_OFF_Controller_English.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
// Libraries #########################################################################################
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // version 2.8
#include <IRremote.h> //version 1.1.2
#include <arduino-timer.h>
// Pins ##############################################################################################
int receptor_pin = 6;
int relay_pin = 7 ;
int ldr_pin = A0;
// Remote DEC Codes ##################################################################
int b_MENU = 25245;
int b_NEXT = 765;
int b_PREV = 8925;
int b_OK = -15811;
int b_BACK = -28561;
int b_MORE = -22441;
int b_LESS = -8161;
// Parameters ########################################################################################
LiquidCrystal_I2C lcd(0x27, 16, 2);
IRrecv irrecv(receptor_pin);
decode_results results;
auto timer_diagnostic = timer_create_default();
auto timer_screen = timer_create_default();
// Variables ####s####################################################################################
int lux_limit = 100; //Maximum Value in LUX to turn on relay
int lux = 0; //Momentary value in LUX
int R_div = 1000; // Resistance of Voltage Diviser
bool diagonostic_result = true; // Diagonostic Result
String state = "";
bool state_overide = false;
String mode = "";
int menu = 0;
int remote = 0;
int error = 0;
// Functions ##########################################################################################
void screen (int cursor1, String line1, int cursor2, String line2){
lcd.clear();
lcd.setCursor(cursor1,0);
lcd.print(line1);
lcd.setCursor(cursor2,1);
lcd.print(line2);
}
void ldr_mesure(){
int ldr_value = analogRead (ldr_pin); //LDR terminal reading (0-1012)
float Vout = float(ldr_value) * (float(5)/float(1023)); // Voltage conversion
float R_ldr = (R_div * (5 - Vout))/Vout; //Value of LDR Resistance
lux = 500/(R_ldr/1000); //LUX conversion
}
bool diagonostic(void*) {
screen (2, "Diagonostic", 0, "");
delay(600);
ldr_mesure();
if (lux < 5 && lux > 0){
diagonostic_result = false;
error = 1;
state_overide = true;
}
else if (lux < 0){
diagonostic_result = false;
error = 2;
state_overide = true;
}
else{
diagonostic_result = true;
error = 0;
}
Serial.println(diagonostic_result);
return true;
}
bool screen_main_menu(void*) {
if (state_overide){
state = "OFF";
}
else{
state = "ON";
}
switch (menu) {
case 0:
if (diagonostic_result){
screen(0, "mode: " + mode, 0, String(lux) + " lux");
}
else if (!diagonostic_result){
switch(error) {
case 1:
screen(5, "error 1", 4, "LDR open");
break;
case 2:
screen(5, "error 2", 4, "LDR open");
break;
}
}
break;
case 1:
menu = 2;
break;
case 2:
screen(0, ">state: " + state , 1 , "lim: " + String(lux_limit) + " lux");
break;
case 3:
screen(1, "state: " + state , 0 , ">lim: " + String(lux_limit) + " lux");
break;
case 4:
screen(1, "lim: " + String(lux_limit) + " lux" , 0 , ">diagonostic");
break;
case 5 ... 9:
menu = 4;
break;
}
return true; //Keep the timer active
}
// Void setup & loop ################################################################################################
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
screen(2, "initializing", 0, "");
irrecv.enableIRIn();
pinMode(relay_pin, OUTPUT);
timer_diagnostic.every(3600000, diagonostic); // Run every hour
timer_screen.every(800, screen_main_menu);
}
void loop() {
timer_diagnostic.tick();
timer_screen.tick();
ldr_mesure();
if(state_overide){
digitalWrite (relay_pin, HIGH);
mode = "OFF";
}
if (irrecv.decode(&results)) {
irrecv.resume();
remote = results.value;
if (remote == b_MENU){
menu = 2;
}
else if (remote == b_BACK && menu > 0){
menu = 0;
}
else if (remote == b_NEXT && menu > 0){
menu++;
}
else if (remote == b_PREV && menu > 0){
menu--;
}
else if (remote == b_OK && menu > 0){
switch(menu){
case 2:
if (state_overide == false){
state_overide = true;
digitalWrite (relay_pin, HIGH);
mode = "OFF";
}
else{
state_overide = false;
}
break;
case 4:
timer_diagnostic.in(0,diagonostic);
menu=0;
break;
}
}
else if (remote == b_MORE && menu == 3){
lux_limit = lux_limit + 5;
}
else if (remote == b_LESS && menu == 3){
lux_limit = lux_limit - 5;
}
Serial.println(menu);
Serial.println(remote);
irrecv.resume();
}
else if (diagonostic_result && menu == 0 && !state_overide){
if (lux < lux_limit){
digitalWrite (relay_pin, LOW);
state = "ON";
mode = "Night";
}
else{
digitalWrite (relay_pin, HIGH);
state = "OFF";
mode = "Day";
}
}
delay(100);
}