-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_car.ino
42 lines (34 loc) · 1.51 KB
/
bluetooth_car.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
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo; // Create a servo object
SoftwareSerial bluetooth(2, 3); // RX, TX pins for HC-05
const int motorPin1 = 10; // Motor pin 1 connected to L298N OUT1
const int motorPin2 = 11; // Motor pin 2 connected to L298N OUT2
void setup() {
pinMode(motorPin1, OUTPUT); // Set motor pins as outputs
pinMode(motorPin2, OUTPUT);
myservo.attach(9); // Attach the servo to pin 9
bluetooth.begin(9600); // Initialize Bluetooth communication
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
if (bluetooth.available()) {
char command = bluetooth.read(); // Read the command from Bluetooth
Serial.println(command); // Print the received command to the serial monitor
if (command == 'B') { // Move backward
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
} else if (command == 'F') { // Move forward
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
} else if (command == 'S') { // Stop
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
} else if (command >= '1' && command <= '9') {
int scale = command - '0'; // Convert char to integer scale value (1-9)
// Map the scale value to the corresponding servo angle (0-180)
int angle = map(scale, 1, 9, 0, 180);
myservo.write(angle); // Move the servo to the calculated angle
}
}
}