-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.ino
175 lines (153 loc) · 6.08 KB
/
board.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
#include <SPI.h>
#include <WiFi101.h>
#include <MKRMotorCarrier.h>
#include "arduino_secrets.h"
#define INTERRUPT_PIN 6
#define MOTOR_ROATATION 25
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(115200); // initialize serial communication
// pinMode(9, OUTPUT); // set the LED pin mode
while(!Serial);
if(controller.begin()){
Serial.print("Mkr1000 motor shield connected");
}else{
Serial.print("Mkr1000 motor schield not connected");
}
Serial.println("Reboot");
controller.reboot();
delay(500);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network rape time btw:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWiFiStatus(); // you're connected now, so print out the status
M1.setDuty(0);
M2.setDuty(0);
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
int dutyR=0;
int dutyL=0;
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:+
client.print("<a href=\"/UP\"><button>U</button></a>");
client.print("<a href=\"/Forward\"><button>N</button></a><br>");
client.print("<a href=\"/Left\"><button>O</button></a>");
client.print("<a href=\"/Backward\"><button>S</button></a>");
client.print("<a href=\"/Right\"><button>E</button></a><br>");
client.print("<a href=\"/Down\"><button>D</button></a>");
client.print("<a href=\"/Stop\"><button>Stop</button></a>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check gets
if(currentLine.endsWith("GET /UP")){
for(int i = 0; i <180; i+=5)
{
servo1.setAngle(i);
Serial.print("Servo position:");
Serial.print(i);
}
}else if(currentLine.endsWith("GET /Down")){
for(int i = 180; i>0; i-=5){
servo1.setAngle(i);
Serial.print("Servo position:");
Serial.print(i);
}
}else if(currentLine.endsWith("GET /Forward")){
for(int i = 0; i <= MOTOR_ROATATION; i++){
dutyR += i;
dutyL += i;
M1.setDuty(dutyL);
M2.setDuty(dutyR);
}
}else if(currentLine.endsWith("GET /Backward")){
for(int i = MOTOR_ROATATION; i >= 0; i--){
dutyL -= i;
dutyR -= i;
M1.setDuty(dutyL);
M2.setDuty(dutyR);
}
}else if(currentLine.endsWith("GET /Right")){
for(int i = 0; i <= (MOTOR_ROATATION + 20); i++){
dutyR -= i;
M2.setDuty(dutyR);
}
}else if(currentLine.endsWith("GET /Left")){
for(int i = 0; i <= (MOTOR_ROATATION + 20); i++){
dutyL -= i;
M1.setDuty(dutyL);
}
}else if(currentLine.endsWith("GET /Stop"))
{
dutyL = 0;
dutyR = 0;
M1.setDuty(dutyL);
M2.setDuty(dutyR);
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
controller.ping();
delay(50);
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}