forked from Thinesh-19/NightCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiD.ino
159 lines (139 loc) · 3.79 KB
/
WiD.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
//include libraries
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2); //RX pin = 3, TX pin = 2
//definition of variables
#define DEBUG true //show messages between ESP8266 and Arduino in serial port
int state = 5; //define initial state of the robot (5 = stand-by)
//define motor pins
const int motor1Pin1 = 5;
const int motor1Pin2 = 6;
const int motor2Pin1 = 9;
const int motor2Pin2 = 10;
//led blink
int sensepin = 0;
int ledpin = 12;
//define motor speed
int motorSpeed = 150; //motor speed (PWM)
//*****
//SETUP
//*****
void setup()
{
//set pin modes
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
//start communication
Serial.begin(115200);
esp8266.begin(115200);
sendData("AT+RST\r\n", 2000, DEBUG); //reset module
sendData("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
sendData("AT+CWJAP=\"ghost\",\"sam12346\"\r\n", 2000, DEBUG); //connect wi-fi network (replace XXXXX by your Wi-Fi router SSID and YYYYY by its password
delay(5000); //wait for connection
sendData("AT+CIFSR\r\n", 1000, DEBUG); //show IP address
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //allow multiple connections
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start web server on port 80
//led blink
// put your setup code here, to run once:
analogReference(DEFAULT); //not necessary
// Serial.begin(9600);
pinMode(ledpin, OUTPUT);
}
//*********
//MAIN LOOP
//*********
void loop()
{
if (esp8266.available()) //verify incoming data
{
if (esp8266.find("+IPD,")) //if there is a message
{
String msg;
esp8266.find("?"); //look for the message
msg = esp8266.readStringUntil(' '); //read whole message
String command = msg.substring(0, 3); //first 3 characters = command
Serial.println(command);
//move forward
if(command == "cm1") {
state = 1;
}
//move backward
if(command == "cm2") {
state = 2;
}
//turn right
if(command == "cm3") {
state = 3;
}
//turn left
if(command == "cm4") {
state = 4;
}
//do nothing
if(command == "cm5") {
state = 5;
}
}
}
//STATE 1: move forward
if (state == 1) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 2: move backward
if (state == 2) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed); }
//STATE 3: move right
if (state == 3) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed);
}
//STATE 4: move left
if (state == 4) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 5: do nothing
if (state == 5) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
int value = analogRead(sensepin);
value = constrain(value, 20, 250);
int ledlevel = map(value, 50, 250, 255, 0);
analogWrite(ledpin, ledlevel);
}
//*******************
//Auxiliary functions
//*******************
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266.available())
{
char c = esp8266.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}