-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalas.ino
264 lines (240 loc) · 6.76 KB
/
Palas.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
#include <Ethernet2.h>
#include <Servo.h>
Servo myservo[4];
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 42, 82, 190);
EthernetServer server(80);
String estado[2] = {"OFF", "ON"};
int dir[2] = {1, 1};
int movC[2] = {0, 0};
boolean movPala[2] = {false, false};
boolean sensor[2] = {false, false};
int s_pins[2] = {4, 1};
// Values to M0, M1, V0, V1
int values[] = {0, 0, 3, 3, 0};
void setup() {
myservo[0].attach(2, 750, 2250); // attaches the servo on pin 9 to the servo object
myservo[0].write(1505);
myservo[1].attach(3, 750, 2250); // attaches the servo on pin 9 to the servo object
myservo[1].write(1500);
myservo[2].attach(5); // attaches the servo on pin 9 to the servo object
myservo[2].write(130);
myservo[3].attach(6); // attaches the servo on pin 9 to the servo object
myservo[3].write(122);
Ethernet.begin(mac, ip);
server.begin();
}
void cargarPala(int id) {
if (movC[id] < 1000)
motor(id, dir[id], 1);
else if (movC[id] == 1000){
Serial.println("CARGA");
motor(id, 0, 0);
if (id == 1)
myservo[id + 2].write(157 - 8);
else
myservo[id + 2].write(157);
}
else if (movC[id] < 6000){
if (values[4] == 1){
if (movC[id] % 200 == 0){
if (id == 1)
myservo[id + 2].write(152 - 8);
else
myservo[id + 2].write(152);
}
else{
if (movC[id] % 100 == 0){
if (id == 1)
myservo[id + 2].write(157 - 8);
else
myservo[id + 2].write(157);
}
}
}
else if (movC[id] == 6000){
Serial.println("ASD2");
if (id == 1)
myservo[id + 2].write(157 - 8);
else
myservo[id + 2].write(157);
}
}
else if (movC[id] < 14000)
motor(id, -dir[id], 1);
else {
Serial.println("ASD");
movC[id] = 0;
movPala[id] = false;
dir[id] *= -1;
Serial.println(id);
Serial.println(movC[id]);
Serial.println(dir[id]);
return;
}
movC[id]++;
}
void descargarPala(int id) {
if (movC[id] < 1800)
motor(id, dir[id], 1);
else if (movC[id] == 2000){
Serial.println("DESCARGA");
motor(id, 0, 0);
if (id == 1)
myservo[id + 2].write(109 - 8);
else
myservo[id + 2].write(109);
}
else if (movC[id] < 12000){
if (movC[id] % 200 == 0){
if (id == 1)
myservo[id + 2].write(114 - 8);
else
myservo[id + 2].write(114);
}
else{
if (movC[id] % 100 == 0){
if (id == 1)
myservo[id + 2].write(109 - 8);
else
myservo[id + 2].write(109);
}
}
}
else if (movC[id] == 12000){
Serial.println("ASD2");
if (id == 1)
myservo[id + 2].write(130 - 8);
else
myservo[id + 2].write(130);
}
else if (movC[id] < 18000)
motor(id, -dir[id], 1);
else {
movC[id] = 0;
movPala[id] = false;
dir[id] *= -1;
Serial.println(id);
Serial.println(movC[id]);
Serial.println(dir[id]);
return;
}
movC[id]++;
}
void motor(int id, int dir, int vel) {
vel += 2;
if (id == 1)
myservo[id].write(1505 + 20*vel*dir);
else
myservo[id].write(1505 + 20*vel*dir);
}
void pala(int id) {
if (values[id] == 0) {
motor(id, 0, 0);
return;
}
if (analogRead(s_pins[id]) > 512) {
motor(id, 0, 0);
movPala[id] = true;
//sensor[id] = !sensor[id];
}
// Serial.println(dir[id]);
if (movPala[id])
if (dir[id] == 1){
//Serial.println("Perez");
cargarPala(id);
}
else
descargarPala(id);
else
motor(id, dir[id], values[id + 2]);
}
void readResponse() {
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
String cadena="";
while (client.connected()) {
if (client.available()) {
char c = client.read();
cadena.concat(c);
if (c == '\n' && currentLineIsBlank) {
int posi=cadena.indexOf("/?");
if (posi > 0) {
int par = cadena.substring(posi + 2, posi + 3).toInt();
String value = cadena.substring(posi + 4, posi + 5);
if (par > 3 && par < 6)
movPala[par - 4] = true;
else
values[par] = value.toInt();
}
createResponse(&client);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(100);
client.stop();
}
}
void createResponse(EthernetClient *client) {
String M0 = estado[values[0]];
String M1 = estado[values[1]];
//\nRefresh: 2\n
client->print("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close\n\n<!DOCTYPE HTML>\n<html><title>Palas</title>\n<head></head><body><h1 align='center'>PALAS </h1><div style='text-align:center;'>");
client->print("<b>Pala Izquierda</b><br /><button onClick=location.href='./?0=");
if (values[0])
client->print("0' >OFF</button><br /><br />");
else
client->print("1' >ON</button><br /><br />");
client->print("</b><br /><button onClick=location.href='./?4=");
client->print("1' >Levantar</button><br /><br />");
client->print("<b>Velocidad: ");
client->print(values[2]);
client->print("</b><br />");
client->print("<div class='slidecontainer'><input type='range' min='1' max='3' value='");
client->print(values[2]);
client->print("' class='slider' id='myRange' onChange='v(this.value, 2)'></div>");
client->print("<div style='text-align:center;'>");
client->print("<br /><br />");
client->print("<b>Pala Derecha</b><br />");
client->print("</b><br /><button onClick=location.href='./?1=");
if (values[1])
client->print("0' >OFF</button><br /><br />");
else
client->print("1' >ON</button><br /><br />");
client->print("</b><br /><button onClick=location.href='./?5=");
client->print("1' >Levantar</button><br /><br />");
client->print("<b>Velocidad: ");
client->print(values[3]);
client->print("</b><br />");
client->print("<div class='slidecontainer'><input type='range' min='1' max='3' value='");
client->print(values[3]);
client->print("' class='slider' id='myRange' onChange='v(this.value, 3)'></div>");
client->print("<b>Vibrar en la carga</b><br />");
client->print("</b><br /><button onClick=location.href='./?6=");
if (values[4])
client->print("0' >OFF</button><br /><br />");
else
client->print("1' >ON</button><br /><br />");
client->print("</b></body>");
// int sensorReading = analogRead(1);
// client->print("analog input 1 is ");
// client->print(sensorReading);
// client->print("<br />");
// sensorReading = analogRead(4);
// client->print("analog input 4 is ");
// client->print(sensorReading);
client->print("<script> function v(n, i){ location.href='./?'+i+'=' + n;} </script>");
client->print("</html>");
}
void loop() {
readResponse();
pala(0);
pala(1);
}