my code is given below
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2, 3); // RX, TX pins
#define motor1A 4
#define motor1B 5
#define motor2A 6
#define motor2B 7
int motorSpeed = 255; // Default motor speed, range: 0 to 255
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
}
void moveForward() {
analogWrite(motor1A, motorSpeed);
digitalWrite(motor1B, LOW);
analogWrite(motor2A, motorSpeed);
digitalWrite(motor2B, LOW);
}
void moveBackward() {
digitalWrite(motor1A, LOW);
analogWrite(motor1B, motorSpeed);
digitalWrite(motor2A, LOW);
analogWrite(motor2B, motorSpeed);
}
void turnLeft() {
digitalWrite(motor1A, LOW);
analogWrite(motor1B, motorSpeed / 2);
analogWrite(motor2A, motorSpeed);
digitalWrite(motor2B, LOW);
}
void turnRight() {
analogWrite(motor1A, motorSpeed);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
analogWrite(motor2B, motorSpeed / 2);
}
void stopMoving() {
analogWrite(motor1A, 0);
digitalWrite(motor1B, 0);
analogWrite(motor2A, 0);
digitalWrite(motor2B, 0);
}
void loop() {
if (bluetooth.available() > 0) {
char command = bluetooth.read();
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':
stopMoving();
break;
case '0':
motorSpeed = 0; // Stop the car
break;
case '1':
motorSpeed = 100; // Set speed to 100
break;
case '2':
motorSpeed = 150; // Set speed to 150
break;
case '3':
motorSpeed = 200; // Set speed to 200
break;
case '4':
motorSpeed = 255; // Set speed to maximum (255)
break;
}
}
}