This document provides a detailed overview of a circuit designed to control a robotic vehicle with object detection and line-following capabilities. The circuit includes an Arduino UNO microcontroller, various sensors, a motor driver, and other components to achieve the desired functionality. The vehicle can operate in both manual and automatic modes, with manual control via Bluetooth and automatic control using IR sensors and an ultrasonic sensor.
Arduino UNO
HC-06
9V Battery
Servo
L298N DC Motor Driver
HC-SR04 Ultrasonic Sensor
DC Motor
Rocker Switch
IR Sensor
#include <Servo.h>
// Pin definitions
const int irSensor1Pin = A0;
const int irSensor2Pin = A1;
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 4;
const int motorENAPin = 10;
const int motorIN1Pin = 9;
const int motorIN2Pin = 8;
const int motorIN3Pin = 7;
const int motorIN4Pin = 6;
const int motorENBPin = 5;
const int btRxPin = 0;
const int btTxPin = 1;
// Servo object
Servo grabberServo;
// Variables
bool manualMode = false;
bool objectDetected = false;
long duration;
int distance;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize pins
pinMode(irSensor1Pin, INPUT);
pinMode(irSensor2Pin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorENAPin, OUTPUT);
pinMode(motorIN1Pin, OUTPUT);
pinMode(motorIN2Pin, OUTPUT);
pinMode(motorIN3Pin, OUTPUT);
pinMode(motorIN4Pin, OUTPUT);
pinMode(motorENBPin, OUTPUT);
// Attach servo
grabberServo.attach(servoPin);
// Set initial servo position
grabberServo.write(0);
}
void loop() {
if (manualMode) {
// Manual mode: control via Bluetooth
if (Serial.available() > 0) {
char command = Serial.read();
handleBluetoothCommand(command);
}
} else {
// Automatic mode: follow line and detect object
int irSensor1Value = digitalRead(irSensor1Pin);
int irSensor2Value = digitalRead(irSensor2Pin);
if (irSensor1Value == HIGH && irSensor2Value == HIGH) {
// Move forward
moveForward();
} else if (irSensor1Value == LOW && irSensor2Value == HIGH) {
// Turn left
turnLeft();
} else if (irSensor1Value == HIGH && irSensor2Value == LOW) {
// Turn right
turnRight();
} else {
// Stop
stopMotors();
}
// Check for object detection
distance = getUltrasonicDistance();
if (distance < 10) {
objectDetected = true;
grabObject();
}
}
}
void handleBluetoothCommand(char command) {
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':