Components:
1 x Arduino UNO 1 x HC-05 Bluetooth Module 1 x Car chassis 4 x Wheels 4 x Gear Motor 5v 1 x L293D motor driver module 1 x Battery holder 2/4 x Accumulators/Batteries N/A Jumpers
The car is equipped with an Adafruit Motor Shield, which allows control of multiple motors simultaneously.
The code begins by including the necessary libraries. AFMotor is the library for controlling motors using the Adafruit Motor Shield, and SoftwareSerial is used for communication with the Bluetooth module.
Four motor objects are created using the AF_DCMotor class from the AFMotor library. These objects represent the individual motors connected to the motor shield. The parameters passed to the constructor specify the motor number and the frequency at which it operates.
In the setup() function, the serial communication with the Bluetooth module is initiated by calling bluetoothSerial.begin(9600). The baud rate is set to 9600, which should match the baud rate configured for the Bluetooth module.
The loop() function continuously checks if there is data available to be read from the Bluetooth module using bluetoothSerial.available(). If data is available, it is read using bluetoothSerial.read() and stored in the command variable.
After reading the command, the Stop() function is called to ensure that all motors are stopped before executing the new command.
Based on the received command, the code enters a switch statement to determine the action to be performed. If the command is 'F', the forward() function is called, which sets the motors to rotate forward. Similarly, for 'B', 'L', and 'R' commands, the back(), left(), and right() functions are called, respectively.
The forward() function sets the speed of all the motors to 255, which is the maximum velocity. Then, it calls the run() function for each motor with the FORWARD parameter, causing them to rotate in the clockwise direction.
Similarly, the back(), left(), and right() functions set the motors to rotate in the opposite directions, based on the desired movement.
This code allows an Arduino-controlled car to receive commands via a Bluetooth module and perform movements such as forward, backward, left, and right by controlling the motors connected to the Adafruit Motor Shield.