-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallswitch.ino
70 lines (65 loc) · 2 KB
/
wallswitch.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
#include "EEPROM.h"
ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_FREQUENTLY_AWAKE);
ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE, ZUNO_ASSOCIATION_GROUP_SET_VALUE, ZUNO_ASSOCIATION_GROUP_SET_VALUE, ZUNO_ASSOCIATION_GROUP_SET_VALUE);
ZUNO_SETUP_CFGPARAMETER_HANDLER(config_parameter_changed);
ZUNO_DISABLE(SERVICE_LEDS);
ZUNO_ENABLE(NO_LOOP_MINIMALDELAY);
ZUNO_SETUP_BATTERY_LEVELS(2700, 3600);
byte state;
byte value;
byte reason;
byte pins[4] = {17, 18, 19, 20};
byte sent[4] = {0, 0, 0, 0};
void setup() {
reason = zunoGetWakeReason();
if (reason != ZUNO_WAKEUP_REASON_INT1 && reason != ZUNO_WAKEUP_REASON_RADIO) {
for (int i = 0; i <= 3; i++) {
pinMode(pins[i], INPUT_PULLUP);
NZ_BYTE(i) = 0;
}
zunoSetWUOptions(ZUNO_WUPFLAGS_INT1_KEYSCAN);
}
}
void loop() {
if (reason == ZUNO_WAKEUP_REASON_INT1) {
while (true) {
delay(20);
for (int i = 0; i <= 3; i++) {
value = digitalRead(pins[i]);
if (sent[i] == 0 && value == LOW) {
if (NZ_BYTE(i) == 255) {
state = 0;
} else {
state = 255;
}
zunoSendToGroupSetValueCommand((i + 1), state);
sent[i] = 1;
NZ_BYTE(i) = state;
}
else if (sent[i] == 1 && value == HIGH) {
sent[i] = 0;
}
}
if (sent[0] == 0 && sent[1] == 0 && sent[2] == 0 && sent[3] == 0) {
break;
}
else if (sent[0] == 1 && sent[1] == 1 && sent[2] == 1 && sent[3] == 1) {
delay(5000);
if (digitalRead(pins[0]) == LOW && digitalRead(pins[1]) == LOW && digitalRead(pins[2]) == LOW && digitalRead(pins[3]) == LOW) {
if (zunoInNetwork()) {
for (int i = 1; i <= 4; i++) {
zunoSendToGroupSetValueCommand(i, 255);
}
}
zunoStartLearn(30, 0);
delay(60000);
zunoReboot();
}
}
}
}
zunoSendDeviceToSleep();
}
void config_parameter_changed(byte param, word value) {
NZ_BYTE((param - 64)) = value;
}