This circuit is designed to control an object-following robot using an Arduino UNO as the main microcontroller. The robot is equipped with an ultrasonic sensor (HC-SR04) for distance measurement and two IR sensors for obstacle detection. It uses two L293D motor drivers to control four DC motors, providing the necessary movement and direction control. Additionally, a servo motor (MG995) is included for extra maneuverability.
/*
* Object Following Robot
* This code controls an object-following robot using an Arduino UNO.
* The robot uses an ultrasonic sensor (HC-SR04) to measure distance to an object
* and IR sensors to detect obstacles. It controls four DC motors via two L293D
* motor drivers and a servo motor for additional movement.
*/
#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A2
#define MAX_DISTANCE 200
#define RIGHT_IR A3
#define LEFT_IR A0
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor Motor1(1, MOTOR12_1KHZ);
AF_DCMotor Motor2(2, MOTOR12_1KHZ);
AF_DCMotor Motor3(3, MOTOR34_1KHZ);
AF_DCMotor Motor4(4, MOTOR34_1KHZ);
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(10);
for (int pos = 90; pos <= 180; pos++) {
myservo.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos--) {
myservo.write(pos);
delay(15);
}
for (int pos = 0; pos <= 90; pos++) {
myservo.write(pos);
delay(15);
}
pinMode(RIGHT_IR, INPUT);
pinMode(LEFT_IR, INPUT);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
unsigned int rightValue = digitalRead(RIGHT_IR);
unsigned int leftValue = digitalRead(LEFT_IR);
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Right IR: ");
Serial.println(rightValue);
Serial.print("Left IR: ");
Serial.println(leftValue);
if (distance > 1 && distance < 15) {
moveForward();
} else if (rightValue == 0 && leftValue == 1) {
turnLeft();
} else if (rightValue == 1 && leftValue == 0) {
turnRight();
} else if (distance > 15) {
stopMotors();
}
}
void moveForward() {
Motor1.setSpeed(130);
Motor1.run(FORWARD);
Motor2.setSpeed(130);
Motor2.run(FORWARD);
Motor3.setSpeed(130);
Motor3.run(FORWARD);
Motor4.setSpeed(130);
Motor4.run(FORWARD);
}
void turnLeft() {
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(BACKWARD);
Motor4.setSpeed(150);
Motor4.run(BACKWARD);
delay(150);
}
void turnRight() {
Motor1.setSpeed(150);
Motor1.run(BACKWARD);
Motor2.setSpeed(150);
Motor2.run(BACKWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);
delay(150);
}
void stopMotors() {
Motor1.setSpeed(0);
Motor1.run(RELEASE);
Motor2.setSpeed(0);
Motor2.run(RELEASE);
Motor3.setSpeed(0);
Motor3.run(RELEASE);
Motor4.setSpeed(0);
Motor4.run(RELEASE);
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
(Note: The code for the servomotor MG995 is not provided with specific functionality and appears to be a template. Additional code would be required to control the servomotor.)
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
(Note: The code for the battery is not applicable as batteries do not have embedded code. This appears to be a placeholder or error in the input data.)