-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESP32S.ino
351 lines (291 loc) · 9.81 KB
/
ESP32S.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
#include <WiFi.h>
#include <WiFiClient.h>
#include <Esp32.h>
#include <BlynkSimpleEsp32.h>
#include <Servo.h>
//Connect to Blynk IoT
#define BLYNK_TEMPLATE_ID " " // Edit
#define BLYNK_TEMPLATE_NAME "Web Control UEV"
#define BLYNK_DEVICE_NAME "Web Control UEV"
#define BLYNK_AUTH_TOKEN " " // Edit
#define BLYNK_PRINT Serial
// WiFi credentials
char ssid = "YourWiFiSSID";
char pass = "YourWiFiPassword";
// Coonect Motor Control Project
char auth_motor[] = "Auth-Token-MotorProject";
// Connect Sensor Stream Project
char auth_sensor[] = "Auth-Token-SensorProject";
// Connect Pan-tilt Servo Project
char auth_sensor[] = "Auth-Token-ServoProject";
// Pins connected to the motors
#define IN_1 2 // Connect to Motor Control Module's pin, Input 1
#define IN_2 4 // Connect to Motor Control Module's pin, Input 2
#define IN_3 5 // Connect to Motor Control Module's pin, Input 3
#define IN_4 18 // Connect to Motor Control Module's pin, Input 4
#define ENA 22 // Enable Motor A Pin
#define ENB 15 // Enable Motor B Pin
// Pins connected to the ultrasonic sensors
#define TRIG_PIN_1 13 // Change to your trigger pin for sensor 1
#define ECHO_PIN_1 12 // Change to your echo pin for sensor 1
#define TRIG_PIN_2 14 // Change to your trigger pin for sensor 2
#define ECHO_PIN_2 27 // Change to your echo pin for sensor 2
#define TRIG_PIN_3 33 // Change to your trigger pin for sensor 3
#define ECHO_PIN_3 32 // Change to your echo pin for sensor 3
#define TRIG_PIN_4 23 // Change to your trigger pin for sensor 4
#define ECHO_PIN_4 25 // Change to your echo pin for sensor 4
const int photoresistorPin = 34; // Analog pin connected to the photoresistor
const int ledPin = 26; // Digital pin connected to the LED
// Create servo objects
Servo servo1; //D19
Servo servo2; //D21
// Initialize the Blynk objects for Motor Control Project and Sensor Stream Project and Servo Project
BlynkTimer timer_motor;
BlynkTimer timer_sensor;
BlynkTimer timer_servo;
//Linear Motion Functions
// Function to control motor speed for function 1
void forward(int speed) {
// Set speed of alternate motors
analogWrite(IN_1, speed);
analogWrite(IN_3, speed);
analogWrite(IN_2, 0);
analogWrite(IN_4, 0);
}
// Function to control motor speed for function 2
void reverse(int speed) {
// Set speed of alternate motors
analogWrite(IN_1, 0);
analogWrite(IN_3, 0);
analogWrite(IN_2, speed);
analogWrite(IN_4, speed);
}
//Turn Functions
// Function to control motor speed for function 3
void right(int speed) {
// Set speed of motors 2 and 3
analogWrite(IN_1, 0);
analogWrite(IN_4, 0);
analogWrite(IN_2, speed);
analogWrite(IN_3, speed);
}
// Function to control motor speed for function 4
void left(int speed) {
// Set speed of motors 1 and 4
analogWrite(IN_2, 0);
analogWrite(IN_3, 0);
analogWrite(IN_1, speed);
analogWrite(IN_4, speed);
}
// Function to control single motor speed for function 5
void leftrad(int speed) {
// Set speed of motors 1 and 4
analogWrite(IN_2, 0);
analogWrite(IN_3, 0);
analogWrite(IN_1, 0);
analogWrite(IN_4, speed);
}
// Function to control single motor speed for function 6
void rightrad(int speed) {
// Set speed of motors 1 and 4
analogWrite(IN_2, 0);
analogWrite(IN_4, 0);
analogWrite(IN_1, 0);
analogWrite(IN_3, speed);
}
// Function to read distance from ultrasonic sensor 2
float readDistance2() {
digitalWrite(TRIG_PIN_2, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN_2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN_2, LOW);
unsigned long duration = pulseIn(ECHO_PIN_2, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}
// Function to read distance from ultrasonic sensor 3
float readDistance3() {
digitalWrite(TRIG_PIN_3, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN_3, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN_3, LOW);
unsigned long duration = pulseIn(ECHO_PIN_3, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}
// Function to read distance from ultrasonic sensor 4
float readDistance4() {
digitalWrite(TRIG_PIN_4, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN_4, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN_4, LOW);
unsigned long duration = pulseIn(ECHO_PIN_4, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}
void readServoAngles() {
// Read the current angles of the servos
int currentAngle1 = servo1.read();
int currentAngle2 = servo2.read();
// Update the value of virtual pins V12 and V13 with the servo angles
Blynk.virtualWrite(V12, currentAngle1);
Blynk.virtualWrite(V13, currentAngle2);
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the GPIO pins to Enable motors A and B
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
digitalWrite(ENA, HIGH); // Turn on the first LED
digitalWrite(ENB, HIGH); // Turn on the second LED
// Set motor pins as outputs
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
// Set ultrasonic sensor pins as inputs/outputs
pinMode(TRIG_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(TRIG_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT);
pinMode(TRIG_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT);
pinMode(TRIG_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT);
// Initialize Blynk for the Motor Control Project
Blynk.config(auth_motor);
// Initialize Blynk for Sensor Stream Project
Blynk.config(auth_sensor);
// Initialize Blynk for the Pan-Tilt Servo Project
Blynk.config(auth_servo);
Blynk.begin(ssid, pass);
// Attach servo motors to GPIO pins
servo1.attach(19); // Pin 19 for servo 1
servo2.attach(21); // Pin 21 for servo 2
pinMode(photoresistorPin, INPUT);
pinMode(ledPin, OUTPUT);
// Call functions when virtual pins change for Motor Control Project
timer_motor.setInterval(100L, []() {
int speed = Blynk.virtualRead(V5).asInt(); // Read speed from V5 slider
function1(speed); // Call function 1 with speed
function2(speed); // Call function 2 with speed
function3(speed); // Call function 3 with speed
function4(speed); // Call function 4 with speed
function5(speed); // Call function 5 with speed
function6(speed); // Call function 6 with speed
});
timer.setInterval(100L, servoControl);
timer.setInterval(100L, readServoAngles);
// Stream distances from ultrasonic sensors to Sensor Stream Project
timer_sensor.setInterval(1000L, []() {
float distance1 = readDistance1(); // Read distance from sensor 1
float distance2 = readDistance2(); // Read distance from sensor 2
float distance3 = readDistance3(); // Read distance from sensor 3
float distance4 = readDistance4(); // Read distance from sensor 4
Blynk.virtualWrite(V6, distance1); // Stream distance to V6
Blynk.virtualWrite(V7, distance2); // Stream distance to V7
Blynk.virtualWrite(V8, distance3); // Stream distance to V8
Blynk.virtualWrite(V9, distance4); // Stream distance to V9
});
}
void loop() {
// Initiate Blynk
Blynk.run();
// Run BlynkTimer tasks for Motor Control Project
timer_motor.run();
// Run BlynkTimer tasks for Sensor Stream Project
timer_sensor.run();
// Run BlynkTimer tasks for Servo Project
timer_servo.run();
// Read the analog value from the photoresistor
int lightIntensity = analogRead(photoresistorPin);
// If light intensity is less than one-third of maximum intensity
if (lightIntensity < 341) { // (1024 * 1/3 = 341)
// Turn on the LED
digitalWrite(ledPin, HIGH);
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);
}
}
// Callback for slider widget to control motor speed
BLYNK_WRITE(V5) {
int speed = param.asInt(); // Read speed from V5 slider
// No need to call functions here as the speed is updated in the timer interval function
}
// Callbacks for button widgets to control motor state
BLYNK_WRITE(V1) {
int buttonState = param.asInt();
if (buttonState == HIGH) {
forward(speed);
}
BLYNK_WRITE(V2) {
int buttonState = param.asInt();
if (buttonState == HIGH) {
reverse(speed);
}
BLYNK_WRITE(V3) {
int buttonState = param.asInt();
if (buttonState == 1) {
right(speed); //Two wheel Turn
}
else if (buttonState == 2) {
rightrad(speed); //One wheel Turn
}
}
BLYNK_WRITE(V4) {
int buttonState = param.asInt();
if (buttonState == 1) {
left(speed); //Two wheel Turn
}
else if (buttonState == 2) {
leftrad(speed); //One wheel Turn
}
}
// Blynk Write callbacks for Sensor Stream Project to receive data from ultrasonic sensors
BLYNK_WRITE(V6) {
float distance = param.asFloat(); // Get the distance value from the Blynk app
// Update the Blynk widget with the received distance value
Blynk.virtualWrite(V6, distance);
}
// Similar BLYNK_WRITE() callbacks for V7, V8, and V9
BLYNK_WRITE(V7) {
float distance = param.asFloat();
Blynk.virtualWrite(V7, distance);
}
BLYNK_WRITE(V8) {
float distance = param.asFloat();
Blynk.virtualWrite(V8, distance);
}
BLYNK_WRITE(V9) {
float distance = param.asFloat();
Blynk.virtualWrite(V9, distance);
}
// Blynk Slider Widget callback for servo 1
BLYNK_WRITE(V10) {
int pos = param.asInt();
servo1.write(pos);
}
// Blynk Slider Widget callback for servo 2
BLYNK_WRITE(V11) {
int pos = param.asInt();
servo2.write(pos);
}
// Blynk Write callback to read and display first servo angle on the first value display widget
BLYNK_WRITE(V12) {
// Read the current angle of the first servo
int currentAngle1 = servo1.read();
// Update the first value display widget with the current angle
Blynk.virtualWrite(V12, currentAngle1);
}
// Blynk Write callback to read and display second servo angle on the second value display widget
BLYNK_WRITE(V13) {
// Read the current angle of the second servo
int currentAngle2 = servo2.read();
// Update the second value display widget with the current angle
Blynk.virtualWrite(V13, currentAngle2);
}