0% found this document useful (0 votes)
21 views

Code Sharing Forum

The document describes an Arduino code for a robot that uses infrared sensors, an ultrasonic sensor, and a servo motor. The code defines pin connections for the sensors and motors. It includes functions to read sensor values, control the servo and motors, and avoid obstacles by turning or backing up based on sensor readings. The code rotates the servo from 0 to 180 degrees, reads the sensors at each position, and controls the motors accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Code Sharing Forum

The document describes an Arduino code for a robot that uses infrared sensors, an ultrasonic sensor, and a servo motor. The code defines pin connections for the sensors and motors. It includes functions to read sensor values, control the servo and motors, and avoid obstacles by turning or backing up based on sensor readings. The code rotates the servo from 0 to 180 degrees, reads the sensors at each position, and controls the motors accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CODE 8.

1
#include <Servo.h>

// Motor control pins


const int enAPin = 5;
const int in1Pin = 7;
const int in2Pin = 8;
const int enBPin = 6;
const int in3Pin = 11;
const int in4Pin = 12;

// IR sensor pins
const int leftIR = A0; // Analog pin for left IR sensor
const int rightIR = A1; // Analog pin for right IR sensor

// Ultrasonic sensor pins


const int triggerPin = 9;
const int echoPin = 10;

// Servo motor pin


const int servoPin = 4;

// Define motor speeds


int leftMotorSpeed = 255;
int rightMotorSpeed = 255;
int servoPosition = 0; // Initial servo position

int leftIRValue = 0;
int rightIRValue = 0;
float distance = 0.0;

Servo myservo;

void setup() {
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);

pinMode(leftIR, INPUT);
pinMode(rightIR, INPUT);

pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(servoPin, OUTPUT);

myservo.attach(servoPin);
myservo.write(servoPosition);

Serial.begin(9600);
Serial.println("Initializing...");
}

void loop() {
// Rotate forward
for (servoPosition = 0; servoPosition <= 180; servoPosition += 15) {
rotateServo(servoPosition);
readSensorsAndPrint();
controlMotors();
delay(500);
}

// Rotate backward
for (servoPosition = 180; servoPosition >= 0; servoPosition -= 15) {
rotateServo(servoPosition);
readSensorsAndPrint();
controlMotors();
delay(500);
}
}

void rotateServo(int position) {


myservo.write(position);
delay(500); // Adjust delay as needed for the servo to reach the
position
}

void readSensorsAndPrint() {
leftIRValue = analogRead(leftIR);
rightIRValue = analogRead(rightIR);

Serial.print("Servo Position: ");


Serial.println(servoPosition);
delay(100);
Serial.print("Left IR Sensor: ");
Serial.print(leftIRValue);
delay(100);
Serial.print(" - Right IR Sensor: ");
Serial.println(rightIRValue);
delay(100);

distance = getUltrasonicDistance();
Serial.print("Distance at ");
Serial.print(servoPosition);
Serial.print(" degrees: ");
Serial.print(distance);
Serial.println(" cm");
}

float getUltrasonicDistance() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

float duration = pulseIn(echoPin, HIGH);


float distance = duration * 0.034 / 2;

return distance;
}
void controlMotors() {
if (leftIRValue > 500 && rightIRValue > 500) {
moveForward();
} else if (leftIRValue < 500 && rightIRValue > 500) {
turnLeft();
} else if (leftIRValue > 500 && rightIRValue < 500) {
turnRight();
} else if (distance < 50) {
moveBackward();
} else {
stopMotors();
}
}

void moveForward() {
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void turnLeft() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, leftMotorSpeed / 2);
analogWrite(enBPin, rightMotorSpeed);
}

void turnRight() {
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, HIGH);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed / 2);
}

void moveBackward() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, HIGH);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void stopMotors() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, 0);
analogWrite(enBPin, 0);
}

#include <Servo.h>

// Motor control pins


const int enAPin = 5;
const int in1Pin = 7;
const int in2Pin = 8;
const int enBPin = 6;
const int in3Pin = 11;
const int in4Pin = 12;

// IR sensor pins
const int leftIR = A0; // Analog pin for left IR sensor
const int rightIR = A1; // Analog pin for right IR sensor

// Ultrasonic sensor pins


const int triggerPin = 9;
const int echoPin = 10;
// Servo motor pin
const int servoPin = 4;

// Define motor speeds


int leftMotorSpeed = 255;
int rightMotorSpeed = 255;
int servoPosition = 0; // Initial servo position

int leftIRValue = 0;
int rightIRValue = 0;
float distance = 0.0;

Servo myservo;

void setup() {
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);

pinMode(leftIR, INPUT);
pinMode(rightIR, INPUT);

pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(servoPin, OUTPUT);

myservo.attach(servoPin);
myservo.write(servoPosition);

Serial.begin(9600);
Serial.println("Initializing...");
}

void loop() {
for (servoPosition = 0; servoPosition <= 180; servoPosition += 15) {
rotateServo(servoPosition);
delay(500); // Wait for the servo to reach the position
readSensorsAndPrint();
controlMotors();
delay(500);
}
}

void rotateServo(int position) {


myservo.write(position);
delay(500); // Adjust delay as needed for the servo to reach the
position
}

void readSensorsAndPrint() {
leftIRValue = analogRead(leftIR);
rightIRValue = analogRead(rightIR);

Serial.print("Left IR Sensor: ");


Serial.print(leftIRValue);
Serial.print(" - Right IR Sensor: ");
Serial.println(rightIRValue);
delay(100); // Delay added for better readability

distance = getUltrasonicDistance();
Serial.print("Distance at ");
Serial.print(servoPosition);
Serial.print(" degrees: ");
Serial.print(distance);
Serial.println(" cm");
delay(100); // Delay added for better readability
}

float getUltrasonicDistance() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
float duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;

return distance;
}

void controlMotors() {
if (distance > 20) {
moveForward();
} else {
avoidObstacle();
}
}

void moveForward() {
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void moveBackward() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, HIGH);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void stopMotors() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, 0);
analogWrite(enBPin, 0);
}

void turnRight() {
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
digitalWrite(in3Pin, LOW);
digitalWrite(in4Pin, HIGH);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void turnLeft() {
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
digitalWrite(in3Pin, HIGH);
digitalWrite(in4Pin, LOW);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void avoidObstacle() {
stopMotors();
delay(500);

if (leftIRValue < 500 && rightIRValue > 500) {


// Obstacle detected on the left, turn right
turnRight();
} else if (leftIRValue > 500 && rightIRValue < 500) {
// Obstacle detected on the right, turn left
turnLeft();
} else {
// If both sensors detect an obstacle or no obstacle, check
ultrasonic sensor
float obstacleDistance = getUltrasonicDistance();

if (obstacleDistance < 20) {


// Obstacle detected by ultrasonic sensor, make a decision based
on the sensor values
if (leftIRValue < rightIRValue) {
// Obstacle is closer to the left, turn right
turnRight();
} else {
// Obstacle is closer to the right or equidistant, turn left
turnLeft();
}
} else {
// If no obstacle detected by ultrasonic sensor, move backward
moveBackward();
}
}
delay(1000); // Adjust this delay as needed for turning duration
stopMotors();
}

#include <Servo.h>

// IR sensor pins
const int frontLeftIR = A0;
const int frontRightIR = A1;

// Ultrasonic sensor pins


const int triggerPin = 9;
const int echoPin = 10;

// Motor control pins


const int enAPin = 5;
// const int LeftMtrIn1 = 7; // Removed this line
// const int LeftMtrIn2 = 8; // Removed this line
const int enBPin = 6;
const int RightMtrIn1 = 11;
const int RightMtrIn2 = 12;

// Servo pin
const int servoPin = 4;
// Motor speeds
int leftMotorSpeed = 200;
int rightMotorSpeed = 200;

// Define servo and sensors


Servo ultrasonicServo;
float distanceValue = 0;

void setup() {
pinMode(frontLeftIR, INPUT);
pinMode(frontRightIR, INPUT);

pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(enAPin, OUTPUT);
// pinMode(LeftMtrIn1, OUTPUT); // Removed this line
// pinMode(LeftMtrIn2, OUTPUT); // Removed this line
pinMode(enBPin, OUTPUT);
pinMode(RightMtrIn1, OUTPUT);
pinMode(RightMtrIn2, OUTPUT);

ultrasonicServo.attach(servoPin);

Serial.begin(9600);
Serial.println("Initializing...");
}

void loop() {
// Read sensor values
int frontLeftIRValue = analogRead(frontLeftIR);
int frontRightIRValue = analogRead(frontRightIR);

// Move according to obstacle detection


if (frontLeftIRValue > 500 && frontRightIRValue > 500) {
moveForward();
} else {
// Use ultrasonic sensor for obstacle detection in front
scanUltrasonic();
if (distanceValue > 20) {
moveForward();
} else {
moveBackward();
delay(1000); // Adjust as needed
turnRight();
delay(1000); // Adjust as needed
}
}
}

void moveForward() {
digitalWrite(RightMtrIn1, LOW);
digitalWrite(RightMtrIn2, HIGH);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void moveBackward() {
digitalWrite(RightMtrIn1, HIGH);
digitalWrite(RightMtrIn2, LOW);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}

void stopMotors() {
digitalWrite(RightMtrIn1, LOW);
digitalWrite(RightMtrIn2, LOW);
analogWrite(enAPin, 0);
analogWrite(enBPin, 0);
}

void turnRight() {
digitalWrite(RightMtrIn1, LOW);
digitalWrite(RightMtrIn2, LOW);
analogWrite(enAPin, leftMotorSpeed);
analogWrite(enBPin, rightMotorSpeed);
}
void scanUltrasonic() {
for (int angle = 0; angle <= 180; angle += 15) {
ultrasonicServo.write(angle);
delay(500); // Adjust as needed
distanceValue = getUltrasonicDistance();
if (distanceValue < 20) {
break;
}
}
}

float getUltrasonicDistance() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

float duration = pulseIn(echoPin, HIGH);


float distance = duration * 0.034 / 2;
return distance;
}

You might also like