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

Code

This document defines the pins and functions to control servos, ultrasonic sensors, DC motors and relays on an Arduino. It includes functions to read distances from the ultrasonic sensor, move the motors forward, backward, and turn, and control the position of servos based on the sensor readings.

Uploaded by

Abaidullah Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Code

This document defines the pins and functions to control servos, ultrasonic sensors, DC motors and relays on an Arduino. It includes functions to read distances from the ultrasonic sensor, move the motors forward, backward, and turn, and control the position of servos based on the sensor readings.

Uploaded by

Abaidullah Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <Servo.

h> //standard library for the servo


#include <NewPing.h> //for the Ultrasonic sensor function library.

//L298N motor control pins


#define IN1 4 // Motor 1 direction pin 1
#define IN2 5 // Motor 1 direction pin 2
#define IN3 6 // Motor 2 direction pin 1
#define IN4 7 // Motor 2 direction pin 2

//sensor pins
#define TRIG_PIN A1
#define ECHO_PIN A2
#define MAX_DISTANCE 200
#define MAX_SPEED 120 // sets speed of DC motors
#define MAX_SPEED_OFFSET 15
Servo myservo;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

boolean goesForward = false;


float distance_sonar = 0;
int distance = 100;
//int speedSet = 0;
#define trigPin 12
#define echoPin 11
Servo servo;
//int sound = 250;
const int relayPin = 2;
Servo sprayerservo;

void setup(){
Serial.begin(115200);
myservo.attach(9);
myservo.write(115);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(10);
sprayerservo.attach(3);
}

void loop(){
// Move the servo from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 1) {
// Set the servo position
sprayerservo.write(angle);
// Wait for the servo to reach the desired position
delay(15);
}

// Move the servo back from 90 to 0 degrees


for (int angle = 90; angle >= 0; angle -= 1) {
// Set the servo position
sprayerservo.write(angle);
// Wait for the servo to reach the desired position
delay(15);
}
int distanceR = 0;
int distanceL = 0;
//delay(50);
float distance_sonar = readPing();
Serial.print(distance_sonar);
if (distance <= 70) {
moveStop();
// delay(100);
moveBackward();
// delay(300);
moveStop();
// delay(200);
distanceR = lookRight();
// delay(200);
distanceL = lookLeft();
// delay(200);

if (distanceR >= distanceL) {


turnRight();
moveStop();
} else {
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = readPing();
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 5) {
Serial.println("the distance is less than 10");
servo.write(270);
delay(1500);
}
else {
servo.write(0);
}
if (distance > 15 || distance <= 0){
Serial.println("The distance is more than 15");
servo.write(180);
}
else {
Serial.print(distance);
Serial.println(" cm");
}

digitalWrite(relayPin, LOW); // Activate the relay (LOW-triggered)


delay(5000); // Wait for 5 seconds
digitalWrite(relayPin, HIGH); // Deactivate the relay
delay(5000);
}

int lookRight() {
myservo.write(50);
// delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}

int lookLeft() {
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
// delay(100);
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250;
}
return cm;
}

void moveStop() {
//analogWrite(ENA, 0);
//analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(500);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
// analogWrite(ENA, MAX_SPEED);
// analogWrite(ENB, MAX_SPEED);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN4, HIGH);
digitalWrite(IN3, LOW);

}
delay(500);
}
void moveBackward() {
goesForward = false;
//analogWrite(ENA, MAX_SPEED);
//analogWrite(ENB, MAX_SPEED);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN4, LOW);
digitalWrite(IN3, HIGH);
delay(500);
}
void turnRight() {
//analogWrite(ENA, MAX_SPEED);
//analogWrite(ENB, MAX_SPEED - MAX_SPEED_OFFSET);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN3, HIGH);
delay(500);
moveStop();
}
void turnLeft() {
//analogWrite(ENA, MAX_SPEED - MAX_SPEED_OFFSET);
//analogWrite(ENB, MAX_SPEED);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);
digitalWrite(IN3, LOW);
delay(500);
moveStop();
}

You might also like