This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Slider1.0.ino
388 lines (288 loc) · 11.9 KB
/
Slider1.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
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const int en = 16;
const int cfg1 = 14;
const int stp = 12;
const int dir = 13;
const int chdir0 = 4;
const int chdir1 = 5;
int ms;
String auto_turn;
String start;
int cc;
int i;
int rt;
int rt_s;
//html code for setup page
const char* setuphtml = "<html><head><title>WLAN SLIDER</title><style>div.content {border: #B6B6B4 solid 1px;}"
"ol#toc {height: 8em;margin: 0;padding: 0;}ol#toc a {font-weight:bold;font-size:30px;text-decoration:none;"
"color: #000;float: left;line-height: 400%;width: 8em;text-align:center;padding-right: 10px;}"
"ol#toc li.current {background-color: #B6B6B4;}ol#toc li.current a {color: #fff;}ol#toc li {"
"background: #E5E4E2;float: left;padding-left: 10px;}body{background:#FFFFFF;}"
".bt{width:90%;padding:20%;text-align:center;color:BLACK;border-radius:50%;font-weight:bold;font-size:35px;}"
".r{background:#E5E4E2;} .g{background:#B6B6B4;} .b{background:#E5E4E2;}"
"</style></head><body>"
"<ol id='toc'><li><a href='init'>INITIAL</a></li><li class='current'><a href='setup'>SETUP</a></li>"
"<li><a href='presets'>PRESETS</a></li></ol><div class='content'><table width='100%'><colgroup width='50%' span='2'</colgroup>"
"<tr><td><form action='dir' method='get'><input class='bt b' type='submit' name='direct' value='FORWARD'></td>"
"<td><input class='bt b' type='submit' name='direct' value='BACKWARD'></form></td></tr>"
"<tr><td><form action='turn' method='get'><input class='bt b' type='submit' name='turn' value='AUTOTURN'></td>"
"<td><input class='bt b' type='submit' name='turn' value='AUTOSTOP'></form></td></tr>"
"<tr><td><form action='rt' method='get'><input class='bt b' type='number' name='rt' value='1' min='1'></td>"
"<td><input class='bt b' type='submit' value='RUNTIME'></form></td></tr>"
"<tr><td><form action='start' method='get'><input class='bt g' type='submit' name='start' value='START'></td>"
"<td><input class='bt r' type='submit' name='start' value='STOP'></form></td></tr>"
"</table></div></body></html>";
//html code for init page
const char* inithtml = "<html><head><title>WLAN SLIDER</title><style>div.content {border: #B6B6B4 solid 1px;}"
"ol#toc {height: 8em;margin: 0;padding: 0;}ol#toc a {font-weight:bold;font-size:30px;text-decoration:none;"
"color: #000;float: left;line-height: 400%;width: 8em;text-align:center;padding-right: 10px;}"
"ol#toc li.current {background-color: #B6B6B4;}ol#toc li.current a {color: #fff;}ol#toc li {"
"background: #E5E4E2;float: left;padding-left: 10px;}body{background:#FFFFFF;}"
".bt{width:90%;padding:20%;text-align:center;color:BLACK;border-radius:50%;font-weight:bold;font-size:35px;}"
".r{background:#E5E4E2;} .g{background:#B6B6B4;} .b{background:#E5E4E2;}"
"</style></head><body>"
"<ol id='toc'><li class='current'><a href='init'>INITIAL</a></li><li><a href='setup'>SETUP</a></li>"
"<li><a href='presets'>PRESETS</a></li></ol><div class='content'>"
"<table width='100%'><colgroup width='50%' span='2'</colgroup>"
"<tr><td><form action='en' method='get'><input class='bt g' type='submit' name='enable' value='ENABLE'></td>"
"<td><input class='bt r' type='submit' name='enable' value='DISABLE'></form></td></tr>"
"<tr><td><form action='goto' method='get'><input class='bt b' type='submit' name='goto' value='CHANGE SIDE'></form></td>"
"<td><form action='calib' method='get'><input class='bt b' type='submit' name='calib' value='CALIBRATE'></form></td></tr>"
"</table></div></body></html>";
//html code for preset page
const char* presethtml = "<html><head><title>WLAN SLIDER</title><style>div.content {border: #B6B6B4 solid 1px;}"
"ol#toc {height: 8em;margin: 0;padding: 0;}ol#toc a {font-weight:bold;font-size:30px;text-decoration:none;"
"color: #000;float: left;line-height: 400%;width: 8em;text-align:center;padding-right: 10px;}"
"ol#toc li.current {background-color: #B6B6B4;}ol#toc li.current a {color: #fff;}ol#toc li {"
"background: #E5E4E2;float: left;padding-left: 10px;}body{background:#FFFFFF;}"
".bt{width:90%;padding:20%;text-align:center;color:BLACK;border-radius:50%;font-weight:bold;font-size:35px;}"
".r{background:#E5E4E2;} .g{background:#B6B6B4;} .b{background:#E5E4E2;}"
"</style></head><body>"
"<ol id='toc'><li><a href='init'>INITIAL</a></li><li><a href='setup'>SETUP</a></li>"
"<li class='current'><a href='presets'>PRESETS</a></li></ol><div class='content'><table width='100%'><colgroup width='50%' span='2'</colgroup>"
"<tr><td><form action='pre' method='get'><input class='bt b' type='submit' name='preset' value='100%'></td><td><input class='bt b' type='submit' name='preset' value='50%'></td></tr>"
"<tr><td><input class='bt b' type='submit' name='preset' value='33%'></td><td><input class='bt b' type='submit' name='preset' value='25%'></td></tr>"
"<tr><td><input class='bt b' type='submit' name='preset' value='10%'></td><td><input class='bt b' type='submit' name='preset' value='5%'></form></td></tr>"
"</table></div></body></html>";
//Sliders IP Address
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
ESP8266WebServer server(80);
void setup() {
//define pins
pinMode(dir, OUTPUT);
pinMode(stp, OUTPUT);
pinMode(en, OUTPUT);
pinMode(cfg1, OUTPUT);
pinMode(chdir0, INPUT_PULLUP);
pinMode(chdir1, INPUT_PULLUP);
//Set CFG1 to HIGH for SILENT Mode 1/4 Step
digitalWrite(cfg1, HIGH);
//Set EN to HIGH to Disable the Slider at startup
digitalWrite(en, HIGH);
Serial.begin(115200);
Serial.setDebugOutput(true);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
//Change this to the SSID and PW you want
WiFi.softAP("TotalControl", "TotalControl");
server.on("/", handle_init);
server.on("/init",handle_init);
server.on("/setup", handle_setup);
server.on("/presets", handle_preset);
server.on("/en", do_en);
server.on("/dir", do_dir);
server.on("/turn", do_turn);
server.on("/rt", do_rt);
server.on("/start", do_start);
server.on("/calib",do_calib);
server.on("/goto",do_goto);
server.on("/pre",do_pre);
server.begin();
Serial.println("Succesfully stared");
Serial.print("DIR is: ");
Serial.println(dir);
Serial.print("STP is: ");
Serial.println(stp);
Serial.print("EN is: ");
Serial.println(en);
Serial.print("CFG1 is: ");
Serial.println(cfg1);
Serial.print("chdir0 is: ");
Serial.println(chdir0);
Serial.print("chdir1 is: ");
Serial.println(chdir1);
}
void handle_setup(){
server.send(200, "text/html", setuphtml);
delay(100);
}
void handle_init(){
server.send(200, "text/html", inithtml);
delay(100);
}
void handle_preset(){
server.send(200, "text/html", presethtml);
delay(100);
}
void do_pre(){
String preset=server.arg("preset");
if (preset == "100%25") do_preset(1);
if (preset == "50%25") do_preset(2);
if (preset == "33%25") do_preset(3);
if (preset == "25%25") do_preset(4);
if (preset == "10%25") do_preset(10);
if (preset == "5%25") do_preset(20);
handle_preset();
}
void do_en(){
//get enable from server and set enable pin to LOW (nessecary step to start any other thing)
String enable=server.arg("enable");
if (enable == "ENABLE"){
Serial.println("Enabled");
digitalWrite(en, LOW);
}
else if (enable == "DISABLE"){
Serial.println("Disabled");
digitalWrite(en, HIGH);
}
handle_init();
}
void do_calib(){
//start Calibration run (2 ways to be sure it started at the beginning)
while (i < 2){
do_calibrate();
i++;
Serial.print("Calibration done Part");
Serial.println(i);
Serial.print("Calibration Counter: ");
Serial.println(cc);
}
i=0;
}
void do_dir(){
//get direction from server, change dir pin
String direct=server.arg("direct");
if (direct == "FORWARD"){
Serial.println("Set direction to Forward");
digitalWrite(dir, HIGH);
}
if (direct == "BACKWARD"){
Serial.println("Set direction to backward");
digitalWrite(dir, LOW);
}
handle_setup();
}
String do_turn(){
//get turn from server return it to loop
server.send(200, "text/html", setuphtml);
auto_turn=server.arg("turn");
Serial.print("AutoTurn has been set to: ");
Serial.println(auto_turn);
return auto_turn;
}
String do_start(){
//get start from server return it to loop
server.send(200, "text/html", setuphtml);
start=server.arg("start");
Serial.print("Start has been set to: ");
Serial.println(start);
return start;
}
int do_rt(){
//get runtime from server, return it to loop
server.send(200, "text/html", setuphtml);
rt=server.arg("rt").toInt();
Serial.print("Runtime has been set to: ");
Serial.println(rt);
return rt;
}
void do_step(int ms){
//do one Step
digitalWrite(stp, HIGH);
delay(ms);
digitalWrite(stp, LOW);
}
void do_autoturn(){
//check buttons for AutoTurn
if (digitalRead(chdir0) == 0) digitalWrite(dir, HIGH);
if (digitalRead(chdir1) == 0) digitalWrite(dir, LOW);
}
void do_autostop(){
//check buttons for AutoStop
if (digitalRead(chdir0) == 0){
digitalWrite(dir, HIGH);
start = "Stop";
}
if (digitalRead(chdir1) == 0){
digitalWrite(dir, LOW);
start = "Stop";
}
}
int do_calibrate(){
cc=0;
server.send(200, "text/html", inithtml);
//Increase Calibration Counter until DIR Changes (button pushed)
while (digitalRead(dir) == 0){
if (digitalRead(chdir0) == 0){
digitalWrite(dir, HIGH);
return cc;
}
do_step(ms=1);
cc++;
}
cc=0;
while (digitalRead(dir) == 1){
if (digitalRead(chdir1) == 0){
digitalWrite(dir, LOW);
return cc;
}
do_step(ms=1);
cc++;
}
cc=0;
}
void do_goto() {
// change side in full speed
server.send(200, "text/html", inithtml);
if (digitalRead(dir) == 0) do_goto1(1);
else if (digitalRead(dir) == 1) do_goto2(1);
}
void do_goto1(int ms) {
//if direction is backward then go backward until direction is forward
while (digitalRead(dir) == 0){
do_step(ms);
if (digitalRead(chdir0) == 0){
digitalWrite(dir, HIGH);
}
}
}
void do_goto2(int ms) {
//if direction is forward then go forward until direction is backward
while (digitalRead(dir) == 1){
do_step(ms);
if (digitalRead(chdir1) == 0){
digitalWrite(dir, LOW);
}
}
}
void do_preset(int ms){
//same as do_goto but with ms given by do_pre
if (digitalRead(dir) == 0) do_goto1(ms);
else if (digitalRead(dir) == 1) do_goto2(ms);
}
void loop() {
server.handleClient();
//Failover if no turn option was choosen
if (auto_turn == "") do_autoturn();
if (auto_turn == "AUTOTURN") do_autoturn();
if (auto_turn == "AUTOSTOP") do_autostop();
//calculate delay(ms) from runtime and calibration couter
if (rt != 0){
rt_s = rt * 60 * 1000;
ms = rt_s / cc;
}
if (start == "START") do_step(ms);
}