-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
240 lines (201 loc) · 5.38 KB
/
main.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
#include <Servo.h>
#include <SoftwareSerial.h> // For Bluetooth communication
// Pin definitions
#define TRIG_PIN 8 // Trig pin for HC-SR04
#define ECHO_PIN 9 // Echo pin for HC-SR04
#define ENA 3 // Enable/speed motors Right (PWM pin)
#define ENB 11 // Enable/speed motors Left (PWM pin)
#define IN1 7 // L298N in1 motors Right
#define IN2 6 // L298N in2 motors Right
#define IN3 5 // L298N in3 motors Left
#define IN4 4 // L298N in4 motors Left
#define SERVO_PIN 12 // Servo control pin
#define SPEED 100 // Motor speed for autonomous mode
#define MIDPOINT 90 // Servo midpoint for forward-facing
// Variables
Servo servo;
int distance = 0;
int speedCar = 125; // Default RC speed
bool isRCMode = false; // Flag to check RC mode
SoftwareSerial SerialBT(0, 1); // RX, TX for HC-05/HC-06 Bluetooth module
void setup() {
// Serial monitor for debugging
Serial.begin(9600); // Debug via USB
SerialBT.begin(9600); // Bluetooth serial
// Motor pin setup
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Ultrasonic sensor setup
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Servo setup
servo.attach(SERVO_PIN);
servo.write(MIDPOINT); // Start with servo at midpoint
Serial.println("Bluetooth RC + Obstacle Avoidance Ready.");
}
void loop() {
if (SerialBT.available()) {
isRCMode = true; // Switch to RC mode if Bluetooth input is detected
handleRCMode();
} else {
isRCMode = false; // If no Bluetooth commands, enter autonomous mode
handleAutonomousMode();
}
}
// Autonomous obstacle avoidance mode
void handleAutonomousMode() {
distance = measureDistance();
if (distance <= 25) {
stopMotors();
delay(200); // Ensure proper stopping
// Check left and right distances
int leftDistance = checkLeft();
int rightDistance = checkRight();
// Reset servo to initial midpoint position
servo.write(MIDPOINT);
delay(500);
// Decide direction based on the distances
if (leftDistance > rightDistance) {
turnLeft();
} else {
turnRight();
}
} else {
moveForward();
}
}
// Measure distance using the HC-SR04 sensor
int measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
int distanceCm = duration * 0.034 / 2; // Convert to cm
return distanceCm;
}
// Servo function to check left distance
int checkLeft() {
servo.write(150); // Turn servo to the left
delay(500); // Allow servo to stabilize
int leftDist = measureDistance();
return leftDist;
}
// Servo function to check right distance
int checkRight() {
servo.write(30); // Turn servo to the right
delay(500); // Allow servo to stabilize
int rightDist = measureDistance();
return rightDist;
}
// Motor control functions for autonomous mode
void moveForward() {
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void turnLeft() {
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(500); // Adjust turn duration
stopMotors();
}
void turnRight() {
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(500); // Adjust turn duration
stopMotors();
}
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
// RC mode (remote control)
void handleRCMode() {
if (SerialBT.available()) {
char receivedChar = SerialBT.read(); // Read a single character from Bluetooth
switch (receivedChar) {
case 'F':
goAhead();
break;
case 'B':
goBack();
break;
case 'L':
goLeft();
break;
case 'R':
goRight();
break;
case 'S':
stopRobot();
break;
default:
if (receivedChar >= '0' && receivedChar <= '9') {
speedCar = map(receivedChar - '0', 0, 9, 0, 255); // Map speed value
}
break;
}
}
}
// RC mode motor controls
void goAhead() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, speedCar);
}
void goBack() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, speedCar);
}
void goLeft() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, speedCar);
}
void goRight() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, speedCar);
}
void stopRobot() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
}