MATERIALS REQUIRED
1.ARDUINO UNO
2.ROBOT CHASIS
3.HCSR-04 ULTRASONIC SENSOR
4.180 DEG SERVO
5.ARDUINO DC POWER SUPPLY
6.ADAFRUIT MOTOR SHIELD
7.300 RPM DC MOTOR-2
8.CASTOR WHEEL
9.WHEELS FOR DC MOTOR-2
10.DOUBLE SIDE TAPE
CIRCUIT
MOTOR 1- ADAFRUIT MOTOR SHIELD M3
MOTOR 2- ADAFRUIT MOTOR SHIELD M4
HCSR-04 GND-ARDUINO GND
HCSR-04 ECHO- ARDUINO A5
HCSR-04 TRIG-ARDUINO A4
HCSR-04 VCC-ARDUINO +5V
SERVO-ADAFRUIT MOTOR SHIELD SERVO_2
CODE
FIRST INSTALL THESE LIBRARY
adafruit motor shield library link-https://github.com/adafruit/Adafruit-Motor-Shield-library
new pings library link-https://bitbucket.org/teckel12/arduino-new-ping/downloads/
#include <AFMotor.h>//library of motor shield
#include <NewPing.h>//the library of ultrasonic sensor
#include <Servo.h>//library of servo
#define TRIG_PIN A4 //defining the trig pin which is connected to analog input4
#define ECHO_PIN A5 //defining the eco pin which is connected to analog input5
#define MAX_DISTANCE 200 //sets the maximum distance that is to be sensed
#define MAX_SPEED 190 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); //initiates the ultronic sensor pins
AF_DCMotor motor1(3);
AF_DCMotor motor2(4);
Servo myservo; //name of servo
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
myservo.attach(9); //pin that are servo is connected
myservo.write(115); //servo position
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceR = 0;//right distance
int distanceL = 0;//left distance
delay(40);
if(distance<=15)
{
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();
}
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() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void moveForward() {
if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(1000);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(1000);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
Comments
Post a Comment