-
Notifications
You must be signed in to change notification settings - Fork 0
/
RollerShutterNodeCoverMarekv6_nano_calibration_hydro.ino
452 lines (406 loc) · 11.6 KB
/
RollerShutterNodeCoverMarekv6_nano_calibration_hydro.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
447
448
449
450
451
452
// Enable debug prints to serial monitor
#define MY_DEBUG
#define MY_GATEWAY_SERIAL
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
//#define MY_REPEATER_FEATURE
// uncomment if we want to manually assign an ID
#define MY_NODE_ID 1
#include <Bounce2.h>
#include <MySensors.h>
#include <SPI.h>
#define BUTTON_UP_PIN 4 // Arduino Digital I/O pin number for up button
#define BUTTON_DOWN_PIN 5 // Arduino Digital I/O pin number for down button
//#define BUTTON_STOP_PIN 5 // Arduino Digital I/O pin number for stop button
//#define RELAY_DIR_PIN 6 // Arduino Digital I/O pin number for direction relay
//#define RELAY_POWER_PIN 7 // Arduino Digital I/O pin number for power relay
#define RELAY_UP_PIN A5
#define RELAY_DOWN_PIN A4
#define RELAY_ON 1
#define RELAY_OFF 0
//#define RELAY_DOWN 1
//#define RELAY_UP 0
#define DIRECTION_DOWN 0
#define DIRECTION_UP 1
#define SKETCH_NAME "Cover by Marek"
#define SKETCH_VER "1.0"
#define CHILD_ID_COVER 0 // sensor Id of the sensor child
#define STATE_UP 100 // 100 is open - up
#define STATE_DOWN 0 // 0 is closed - down
//#define CHILD_ID_CALIBRATE 1 // sensor Id of the sensor child to calibrate
#define CHILD_ID_SET 1 // sensor Id of the sensor child to init the roll time
#define PRESENT_MESSAGE "Cover sensor for hass"
const int LEVELS = 100; //the number of levels
float rollTime = 60.0; //the overall rolling time of the shutter
const bool IS_ACK = false; //is to acknowlage
static bool initial_state_sent = false;//for hass we need at list one state send at begining
// debouncing parameters
int value = 0;
int oldValueUp = 0;
int oldValueDown = 0;
int oldValueStop = 0;
//static unsigned long last_interrupt_time_up = 0;
//static unsigned long last_interrupt_time_down = 0;
//static unsigned long debounce_time = 200;
Bounce debouncerUp = Bounce();
Bounce debouncerDown = Bounce();
Bounce debouncerStop = Bounce();
// shutter position parameters
float timeOneLevel = rollTime / LEVELS;
int requestedShutterLevel = 0;
int currentShutterLevel = 0;
unsigned long lastLevelTime = 0;
bool isMoving = false;
int directionUpDown;
bool calibrateDown;
bool calibrateUp;
unsigned long calibrationStartTime;
float calibrationTime = 15.0;
bool calibratedDown;
bool calibratedUp;
enum CoverState {
STOP,
UP, // Window covering. Up.
DOWN, // Window covering. Down.
};
static int coverState = STOP;
MyMessage msgUp(CHILD_ID_COVER, V_UP);
MyMessage msgDown(CHILD_ID_COVER, V_DOWN);
MyMessage msgStop(CHILD_ID_COVER, V_STOP);
MyMessage msgPercentage(CHILD_ID_COVER, V_PERCENTAGE);
//MyMessage msgCode(CHILD_ID_SET, V_IR_SEND);
void sendState() {
// Send current state and status to gateway.
// send(msgUp.set(coverState == UP));
// send(msgDown.set(coverState == DOWN));
// send(msgStop.set(coverState == STOP));
send(msgPercentage.set(currentShutterLevel));
}
void shuttersUp(void) {
#ifdef MY_DEBUG
Serial.println("Shutters going up");
#endif
if (digitalRead(RELAY_DOWN_PIN) == RELAY_ON) {
digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
wait(50);
}
digitalWrite(RELAY_UP_PIN, RELAY_ON);
directionUpDown = DIRECTION_UP;
isMoving = true;
coverState = UP;
sendState();
}
void shuttersDown(void) {
#ifdef MY_DEBUG
Serial.println("Shutters going down");
#endif
if (digitalRead(RELAY_UP_PIN) == RELAY_ON) {
digitalWrite(RELAY_UP_PIN, RELAY_OFF);
wait(50);
}
digitalWrite(RELAY_DOWN_PIN, RELAY_ON);
directionUpDown = DIRECTION_DOWN;
isMoving = true;
coverState = DOWN;
sendState();
}
void shuttersHalt(void) {
#ifdef MY_DEBUG
Serial.println("Shutters halted");
#endif
digitalWrite(RELAY_UP_PIN, RELAY_OFF);
digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
isMoving = false;
requestedShutterLevel = currentShutterLevel;
#ifdef MY_DEBUG
Serial.println("saving state to: ");
Serial.println(String(currentShutterLevel));
#endif
saveState(CHILD_ID_COVER, currentShutterLevel);
coverState = STOP;
sendState();
}
void changeShuttersLevel(int level) {
int dir = (level > currentShutterLevel) ? DIRECTION_UP : DIRECTION_DOWN;
if (isMoving && dir != directionUpDown) {
shuttersHalt();
}
requestedShutterLevel = level;
}
void initShutters() {
#ifdef MY_DEBUG
Serial.println("Init Cover");
#endif
shuttersUp();
wait((rollTime + timeOneLevel * LEVELS) * 1000);
currentShutterLevel = STATE_UP;
requestedShutterLevel = currentShutterLevel;
}
void receive(const MyMessage &message) {
#ifdef MY_DEBUG
Serial.println("recieved incomming message");
Serial.println("Recieved message for sensor: ");
Serial.println(String(message.sensor));
Serial.println("Recieved message with type: ");
Serial.println(String(message.type));
#endif
if (message.sensor == CHILD_ID_COVER) {
switch (message.type) {
case V_UP:
//Serial.println(", New status: V_UP");
changeShuttersLevel(STATE_UP);
//state = UP;
//sendState();
break;
case V_DOWN:
//Serial.println(", New status: V_DOWN");
changeShuttersLevel(STATE_DOWN);
//state = DOWN;
//sendState();
break;
case V_STOP:
//Serial.println(", New status: V_STOP");
shuttersHalt();
//state = IDLE;
//sendState();
break;
case V_PERCENTAGE:
//Serial.println(", New status: V_PERCENTAGE");
// if (!initial_state_sent) {
// #ifdef MY_DEBUG
// Serial.println("Receiving initial value from controller");
// #endif
// initial_state_sent = true;
// }
int per = message.getInt();
if (per > STATE_UP) {
per = STATE_UP;
}
changeShuttersLevel(per);
//InitShutters(message.getInt());//send value < 0 or > 100 to calibrate
//sendState();
break;
}
}
else if (message.sensor == CHILD_ID_SET) {
if (message.type == V_VAR1) {
#ifdef MY_DEBUG
Serial.println(", New status: V_VAR1, with payload: ");
#endif
String strRollTime = message.getString();
rollTime = strRollTime.toFloat();
#ifdef MY_DEBUG
Serial.println("rolltime value: ");
Serial.println(String(rollTime));
#endif
saveState(CHILD_ID_SET, rollTime);
}
}
#ifdef MY_DEBUG
Serial.println("exiting incoming message");
#endif
return;
}
void before() {
// Setup the button
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
// Activate internal pull-up
// digitalWrite(BUTTON_UP_PIN, HIGH);
// attachInterrupt(digitalPinToInterrupt(BUTTON_UP_PIN), upButtonPress, FALLING);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
// Activate internal pull-up
// digitalWrite(BUTTON_DOWN_PIN, HIGH);
// attachInterrupt(digitalPinToInterrupt(BUTTON_DOWN_PIN), downButtonPress, FALLING);
// pinMode(BUTTON_STOP_PIN, INPUT_PULLUP);
// Activate internal pull-up
// digitalWrite(BUTTON_STOP_PIN, HIGH);
// After setting up the button, setup debouncer
debouncerUp.attach(BUTTON_UP_PIN);
debouncerUp.interval(5);
// After setting up the button, setup debouncer
debouncerDown.attach(BUTTON_DOWN_PIN);
debouncerDown.interval(5);
// After setting up the button, setup debouncer
// debouncerStop.attach(BUTTON_STOP_PIN);
// debouncerStop.interval(5);
// Make sure relays are off when starting up
digitalWrite(RELAY_UP_PIN, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_UP_PIN, OUTPUT);
// Make sure relays are off when starting up
digitalWrite(RELAY_DOWN_PIN, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_DOWN_PIN, OUTPUT);
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME, SKETCH_VER);
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_COVER, S_COVER, PRESENT_MESSAGE, IS_ACK);
// present(CHILD_ID_SET, S_CUSTOM);
}
void setup(void) {
//set up roll time if the saved value is not 255
#ifdef MY_DEBUG
Serial.println("getting rolltime from eeprom: ");
#endif
float tmpRollTime = loadState(CHILD_ID_SET);
if (tmpRollTime != 0xff) {
rollTime = tmpRollTime;
}
#ifdef MY_DEBUG
Serial.println(String(rollTime));
#endif
int state = loadState(CHILD_ID_COVER);
#ifdef MY_DEBUG
Serial.println("getting state from eeprom: ");
Serial.println(String(state));
#endif
// if (state == 0xff) {
// initShutters();
// } else {
currentShutterLevel = state;
requestedShutterLevel = state;
// }
}
void loop(void) {
if (!initial_state_sent) {
#ifdef MY_DEBUG
Serial.println("Sending initial value");
#endif
sendState();
// send(msgCode.set('20.0'));
// #ifdef MY_DEBUG
// Serial.println("Requesting initial value from controller");
// #endif
// request(CHILD_ID_COVER, V_PERCENTAGE);
// wait(2000, C_SET, V_PERCENTAGE);
initial_state_sent = true;
}
debouncerUp.update();
value = debouncerUp.read();
if (value == 0 && value != oldValueUp) {
if(isMoving){
shuttersHalt();
}
else{
calibrateUp = false;
calibratedUp = false;
changeShuttersLevel(STATE_UP);
}
//state = UP;
//sendState();
}
oldValueUp = value;
debouncerDown.update();
value = debouncerDown.read();
if (value == 0 && value != oldValueDown) {
if(isMoving){
shuttersHalt();
}
else{
calibrateDown = false;
calibratedDown = false;
changeShuttersLevel(STATE_DOWN);
}
//state = DOWN;
//sendState();
}
oldValueDown = value;
/* debouncerStop.update();
value = debouncerStop.read();
if (value == 0 && value != oldValueStop) {
shuttersHalt();
//state = IDLE;
//sendState();
}
oldValueStop = value;
*/
if(currentShutterLevel != 100)
{
calibrateUp = false;
calibratedUp = false;
}
if(currentShutterLevel != 0)
{
calibrateDown = false;
calibratedDown = false;
}
if (isMoving)
{
unsigned long _now = millis();
if (_now - lastLevelTime >= timeOneLevel * 1000) {
if (directionUpDown == DIRECTION_UP) {
currentShutterLevel += 1;
} else {
currentShutterLevel -= 1;
}
currentShutterLevel = constrain(currentShutterLevel, 0, 100);
#ifdef MY_DEBUG
Serial.println(String(requestedShutterLevel));
Serial.println(String(currentShutterLevel));
#endif
lastLevelTime = millis();
send(msgPercentage.set(currentShutterLevel));
}
if (currentShutterLevel == requestedShutterLevel)
{
if(currentShutterLevel == 0 && !calibratedDown)
{
if(calibrateDown == false)
{
calibrateDown = true;
calibratedDown = false;
calibrationStartTime = _now;
}
else
{
if(calibratedDown == false)
{
if (_now - calibrationStartTime >= calibrationTime * 1000)
{
calibratedDown = true;
}
}
}
}
else if (currentShutterLevel == 100 && !calibratedUp)
{
if(calibrateUp == false)
{
calibrateUp = true;
calibratedUp = false;
calibrationStartTime = _now;
}
else
{
if(calibratedUp == false)
{
if (_now - calibrationStartTime >= calibrationTime * 1000)
{
calibratedUp = true;
}
}
}
}
else
{
shuttersHalt();
}
}
}
else
{
if (requestedShutterLevel != currentShutterLevel)
{
if (requestedShutterLevel > currentShutterLevel) {
shuttersUp();
}
else {
shuttersDown();
}
lastLevelTime = millis();
}
}
}