-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_logic.ino
116 lines (97 loc) · 2.72 KB
/
command_logic.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
#include <WiFi.h>
const char* ssid = "your-ssid";
const char* password = "your-password";
int esp32_port = 80;
int steeringPin1 = 19;
int steeringPin2 = 21;
int backWheelsPin1 = 22;
int backWheelsPin2 = 23;
int enablePin = 2;
WiFiServer server(esp32_port);
void setup() {
Serial.begin(115200);
delay(5000);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
delay(2000);
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// Initialize motor control pins
pinMode(steeringPin1, OUTPUT);
pinMode(steeringPin2, OUTPUT);
pinMode(backWheelsPin1, OUTPUT);
pinMode(backWheelsPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char command = client.read();
executeCommand(command);
}
}
client.stop();
}
}
void executeCommand(char command) {
Serial.print("Received command: ");
Serial.println(command);
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':
stopMoving();
break;
default:
Serial.println("Invalid command");
break;
}
}
void moveForward() {
digitalWrite(steeringPin1, LOW);
digitalWrite(steeringPin2, LOW);
digitalWrite(backWheelsPin1, HIGH);
digitalWrite(backWheelsPin2, LOW);
digitalWrite(enablePin, HIGH); // Set enable pin to HIGH for full speed
}
void moveBackward() {
digitalWrite(steeringPin1, LOW);
digitalWrite(steeringPin2, LOW);
digitalWrite(backWheelsPin1, LOW);
digitalWrite(backWheelsPin2, HIGH);
digitalWrite(enablePin, HIGH); // Set enable pin to HIGH for full speed
}
void turnLeft() {
digitalWrite(steeringPin1, HIGH);
digitalWrite(steeringPin2, LOW);
digitalWrite(enablePin, HIGH); // Set enable pin to HIGH for full speed
}
void turnRight() {
digitalWrite(steeringPin1, LOW);
digitalWrite(steeringPin2, HIGH);
digitalWrite(enablePin, HIGH); // Set enable pin to HIGH for full speed
}
void stopMoving() {
digitalWrite(steeringPin1, LOW);
digitalWrite(steeringPin2, LOW);
digitalWrite(backWheelsPin1, LOW);
digitalWrite(backWheelsPin2, LOW);
digitalWrite(enablePin, LOW); // Set enable pin to LOW to stop the motors
}