-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lamp_v3.0.ino
280 lines (223 loc) · 5.68 KB
/
Lamp_v3.0.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
/* ESP8266 Lam Controller v2.1 */
#include <ESP8266WiFi.h> //Library to control ESP8266 WiFi function
#include <BlynkSimpleEsp8266.h> //Blink library for ESP8266
//libraries needed for IR reciever
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
/*
Inversed 15 10-bit predefined brightness level first 8 = 2^n, the rest = 128*n
Inveresing is required to compesate inversing done by BJT driver
not inverted value 0,2,4,8,16,32,64, 128, 256,384,512,640,768,896, 1023
*/
//Setting for PIR
//const int PIR = 12; //PIR sensor attached to GPIO 12
//int move1; //tell the controller to ignore/accept PIR data
// GPIO pins used to control main LED panel
const int ledWarm = 13;
const int ledCool = 14;
//Green LED pins for debugging
const int ledGreen = 4;
//Onboard LED pins
const int ledOn1 = 2;
// An IR detector/demodulator is connected to GPIO pin 14(D5 on a NodeMCU
// board).
uint16_t RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;
int data; //HEX value from IR reciever
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
//function for the IR reciever
void IR_button();
//function for the PIR sensor
//void PIR_sense();
//Initial brightness level
int warm = 0;
int cool = 0;
int warm_temp = 0;
int cool_temp = 0;
float warm_cal;
float cool_cal;
int warm_pwm;
int cool_pwm;
int slow;
int interval = 100;
int timer0 = 0;
int timer1 = 0;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Sync the current virtual pins values to the one in the server
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
}
BLYNK_WRITE(V1)
{//assign local variable for warm brighness = virtual pins value
warm = param.asInt();
}
BLYNK_WRITE(V2)
{//assign local variable for cool brighness = virtual pins value
cool = param.asInt();
}
/*
BLYNK_WRITE(V3) {//assign local variable PIR control
move1 = param.asInt();
}*/
BLYNK_WRITE(V4)
{//change brightness respond
slow = param.asInt();
}
BLYNK_WRITE(V5)
{//change brightness respond
interval = param.asInt();
}
BLYNK_READ(V6)
{
Blynk.virtualWrite(V6,warm_pwm);
}
BLYNK_READ(V7)
{
Blynk.virtualWrite(V7,cool_pwm);
}
void IR_button()
{
if (irrecv.decode(&results)) {//if IR signal is recieved
data = results.value; //store and change HEX data from IR reciever (uint_64 ---> int)
Serial.println(data,HEX);
irrecv.resume(); // Receive the next value
if (data==0xFD12ED || data == 0xFDD02F || data == 0xFD02FD || data == 0xFD30CF)//Warm+
{
// Increase warm brightness
if (warm >= 4200 || data == 0xFDD02F || data == 0xFD30CF)
{
warm=4200;
}
else
{
warm=warm+200;
}
// Update variable
Blynk.virtualWrite(V1, warm);
}
if (data == 0xFD926D || data == 0xFD22DD || data == 0xFDF00F || data == 0xFDC837)//Warm-
{
// Decrease warm brightness
if (warm <= 0 || data == 0xFD22DD || data == 0xFDC837)
{
warm=0;
}
else
{
warm=warm-200;
}
// Update variable
Blynk.virtualWrite(V1, warm);
}
if (data == 0xFD52AD || data == 0xFDB04F || data == 0xFD02FD || data == 0xFD30CF)//Cool+
{
// Increase cool bright
if (cool >= 4200 || data == 0xFDB04F || data == 0xFD30CF)
{
cool=4200;
}
else
{
cool=cool+200;
}
// Update variable
Blynk.virtualWrite(V2, cool);
}
if (data == 0xFDD22D || data == 0xFDC23D || data == 0xFDF00F || data == 0xFDC837)//Cool-
{
// Decrease cool bright
if (cool <= 0 || data == 0xFDC23D || data == 0xFDC837)
{
cool=0;
}
else
{
cool=cool-200;
}
// Update variable
Blynk.virtualWrite(V2, cool);
}
}
}
/*
void PIR_sense()
{
//change PWM value
analogWrite(ledWarm, 2);
// Update variable
Blynk.virtualWrite(V1, pwm[2]);
}
*/
void setup()
{
// Debug console
//Serial.begin(115200);
analogWriteFreq(2000);
// Start the receiver
irrecv.enableIRIn();
//Start BLynk service
Blynk.begin(auth, ssid, pass);
//set GPIO as output
pinMode(ledWarm, OUTPUT);
pinMode(ledCool, OUTPUT);
// pinMode(PIR, INPUT);
//Innitial LED value (off by default)
analogWrite(ledWarm, warm_pwm);
analogWrite(ledCool, cool_pwm);
}
void loop()
{
Blynk.run();
IR_button();
int timer = micros();
if (slow == 1)
{
if ((timer - timer0) >= interval)
{
if (warm_temp > warm)
{warm_temp--;}
else if (warm_temp < warm)
{warm_temp++;}
timer0 = timer;
}
else
{warm_temp = warm_temp;}
if ((timer - timer1) >= interval)
{
if (cool_temp > cool)
{cool_temp--;}
else if (cool_temp < cool)
{cool_temp++;}
timer1 = timer;
}
else
{cool_temp = cool_temp;}
}
else if (slow == 0)
{
warm_temp = warm;
cool_temp = cool;
}
warm_cal = warm_temp/300;
cool_cal = cool_temp/300;
warm_cal = pow(warm_cal,2.626125127);
warm_pwm = 1023-warm_cal;
analogWrite(ledWarm, warm_pwm);
cool_cal = pow(cool_cal,2.626125127);
cool_pwm = 1023-cool_cal;
analogWrite(ledCool, cool_pwm);
}