-
Notifications
You must be signed in to change notification settings - Fork 0
/
velux-blind-control.ino
186 lines (159 loc) · 4.33 KB
/
velux-blind-control.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
//
// Arduino Sketch to allow control of Velux blinds
// by hacking a Velux remote control
//
// D2: connected to the Close button (down)
// D3: connected to the Stop button
// D4: connected to the Open button (up)
//
// Connect to the Arduino's serial port at 9600N1
//
// Send a command followed by a newline character:
// close
// stop
// open
//
#include <EveryTimerB.h>
enum {
stateStopped,
stateOpening,
stateClosing
} currentState = stateStopped;
#define FULLY_OPEN (100)
#define FULLY_CLOSED (0)
#define PIN_CLOSE (2)
#define PIN_STOP (3)
#define PIN_OPEN (4)
int currentPosition = FULLY_OPEN;
int targetPosition = FULLY_OPEN;
bool reportState = true;
void updatePostion()
{
if (currentState == stateOpening) {
currentPosition += 10;
if (currentPosition > FULLY_OPEN) {
currentPosition = FULLY_OPEN;
currentState = stateStopped;
}
reportState = true;
} else if (currentState == stateClosing) {
currentPosition -= 10;
if (currentPosition < FULLY_CLOSED) {
currentPosition = FULLY_CLOSED;
currentState = stateStopped;
}
reportState = true;
}
if (currentState != stateStopped && currentPosition == targetPosition) {
// We have reached the target position
currentState = stateStopped;
reportState = true;
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
TimerB2.initialize();
TimerB2.attachInterrupt(updatePostion);
TimerB2.setPeriod(1600000); // 16 seconds / 10 steps = 1600000 microseconds
Serial.begin(9600);
Serial.println("# velux-blind-control");
}
void pulsePin(int pin)
{
// Flash LED
digitalWrite(LED_BUILTIN, HIGH);
pinMode(pin, OUTPUT); // Disable high-impedence
digitalWrite(pin, LOW);
delay(200);
pinMode(pin, INPUT); // Enable high-impedance
digitalWrite(LED_BUILTIN, LOW);
}
void closeCommand()
{
targetPosition = FULLY_CLOSED;
currentState = stateClosing;
pulsePin(PIN_CLOSE);
}
void stopCommand()
{
targetPosition = currentPosition;
currentState = stateStopped;
pulsePin(PIN_STOP);
}
void openCommand()
{
targetPosition = FULLY_OPEN;
currentState = stateOpening;
pulsePin(PIN_OPEN);
}
boolean isInteger(String str)
{
unsigned int stringLength = str.length();
if (stringLength == 0) {
return false;
}
for(unsigned int i = 0; i < stringLength; ++i) {
char chr = str.charAt(i);
if (!isDigit(chr)) {
return false;
}
}
return true;
}
void loop()
{
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
if (command.equalsIgnoreCase("close")) {
closeCommand();
Serial.println("ok");
} else if (command.equalsIgnoreCase("open")) {
openCommand();
Serial.println("ok");
} else if (command.equalsIgnoreCase("stop")) {
stopCommand();
Serial.println("ok");
} else if (command.equalsIgnoreCase("toggle")) {
if (targetPosition > 50) {
closeCommand();
} else {
openCommand();
}
Serial.println("ok");
} else if (isInteger(command)) {
// Keep things simple and only open or close (nothing in-between)
int i = command.toInt();
if (i < 50) {
closeCommand();
} else if (i >= 50) {
openCommand();
}
Serial.println("ok");
} else {
Serial.println("error");
}
};
// FIXME: report current position less frequently?
if (reportState == true) {
Serial.print("target-position: ");
Serial.println(targetPosition, DEC);
Serial.print("current-position: ");
Serial.println(currentPosition, DEC);
switch(currentState) {
case stateClosing:
Serial.println("state: closing");
break;
case stateOpening:
Serial.println("state: opening");
break;
case stateStopped:
Serial.println("state: stopped");
break;
default:
Serial.println("state: error");
break;
}
reportState = false;
}
}