You are on page 1of 9

#include <NewPing.

h> //library for the SR04 ultrasonic sensor


#include <Servo.h>

//servo library, SR04 is mounted on this servo

Servo myservo;
const int TRIGGER_PIN = 28; //modified
const int ECHO_PIN = 29; //modified
const int MAX_DISTANCE = 200; //modified
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define runEvery(t) for (static typeof(t) _lasttime;(typeof(t))((typeof(t))milli
s() - _lasttime) > (t);_lasttime += (t))
//define PWM pins.
const int ENA = 2;
const int ENB = 3;
const int ENC = 4;
const int END = 5;

//define direction pins.


const int A_1 = 31;
const int A_2 = 33;
const int B_1 = 35;
const int B_2 = 37;
const int C_1 = 39;
const int C_2 = 41;
const int D_1 = 43;
const int D_2 = 45;

int uS;

//value of SR04 ultrasonic sensor

int distance;

//distance in cm of ultrasonic sensor

int pos = 90;

//start position of servo = 90

int servoDirection = 0; // sweeping left or right


int robotDirection = 0; //0 = forward, 1 = backward, 2 = left, 3 = right
int lastRobotDirection; //last direction of the robot
int distanceCenter;
int distanceLeft;
int distanceRight;
int servoDelay = 20; //with this parameter you can change the sweep speed of the
servo
const int speedLeft = 205; //Left direction speed
const int speedRight = 255 ; // Right direction speed
long previousMillis = 0;
const int interval = 650; //interval to switch between the robotDirection, this
value will determine
//how long the robot will turn left/right when it detects an obstacle

void setup()
{
Serial.begin(115200); //to use the serial monitor
myservo.attach(30); //servo on pin 30
myservo.write(pos); //center servo
delay(1500); // delay so we have some time to put the robot on the floor
}

void loop() {

sweepServo(); //function to sweep the servo

getDistance(); //function to get the distance from the ultrasonic sensor

if (pos >= 15 && pos <= 45)


{
distanceRight = distance; //servo is to the right so measured distance = dista
nceRight
}
if (pos >= 135 && pos <= 165)
{
distanceLeft = distance; //servo is to the left so measured distance = distance
Left
}
if (pos > 70 && pos < 110)
{
distanceCenter = distance; //servo is centred so measured distance = distanceCen
ter
}
if (distanceCenter >= 25)

// coast is clear, full power forward new adding

{
robotDirection = 0;

//move forward

}
else

//obstacle detected, turn left or right?

{
if (distanceLeft > distanceRight)
{
robotDirection = 2; //turn left
}
if (distanceLeft < distanceRight)
{
robotDirection = 3; //turn right
}

if (distanceLeft <= 10 && distanceCenter <= 10 || distanceRight <= 5 && distanc


eCenter <= 10) // new adding
{
robotDirection = 1; // we are stuck, lets try and backup
}

}
unsigned long currentMillis = millis(); //set a timer
if(robotDirection == 0 && robotDirection == lastRobotDirection)
{
forward();
lastRobotDirection = robotDirection;
}
if(robotDirection == 0 && robotDirection != lastRobotDirection && currentMillis
- previousMillis > interval )
{
forward();
lastRobotDirection = robotDirection;
previousMillis = currentMillis;
}
if(robotDirection == 1 && robotDirection == lastRobotDirection)
{
backward();
lastRobotDirection = robotDirection;
}
if(robotDirection == 1 && robotDirection != lastRobotDirection && currentMillis
- previousMillis > interval )
{
backward();
lastRobotDirection = robotDirection;
previousMillis = currentMillis;
}
if(robotDirection == 2 && robotDirection == lastRobotDirection)
{

left();
lastRobotDirection = robotDirection;
}
if(robotDirection == 2 && robotDirection != lastRobotDirection && currentMillis
- previousMillis > interval )
{
left();
lastRobotDirection = robotDirection;
previousMillis = currentMillis;
}
if(robotDirection == 3 && robotDirection == lastRobotDirection)
{
right();
lastRobotDirection = robotDirection;
}
if(robotDirection == 3 && robotDirection != lastRobotDirection && currentMillis
- previousMillis > interval )
{
right();
lastRobotDirection = robotDirection;
previousMillis = currentMillis;
}
}
void backward()
{
analogWrite(ENA, 150);
analogWrite(ENB, 150);
analogWrite(ENC, 150);
analogWrite(END, 150);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);

digitalWrite(C_2, LOW);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}

void stop()
{
analogWrite(ENA, 0);
analogWrite(ENB , 0);
analogWrite(ENC, 0);
analogWrite(END, 0);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}

void forward()
{
analogWrite(ENA, 150);
analogWrite(ENB, 150);
analogWrite(ENC, 150);
analogWrite(END, 150);
digitalWrite(A_2, HIGH);

digitalWrite(A_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(B_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(D_2, HIGH);
digitalWrite(D_1, LOW);
}

void left()
{
analogWrite(ENA, 255);
analogWrite(ENB, 255);
analogWrite(ENC, 255);
analogWrite(END, 255);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_2, LOW);
digitalWrite(D_1, HIGH);
}

void right()
{

analogWrite(ENA, 255);
analogWrite(ENB, 255);
analogWrite(ENC, 255);
analogWrite(END, 255);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_2, HIGH);
digitalWrite(D_1, LOW);
}

void getDistance()
{
runEvery(40)

//loop for ultrasonic measurement

{
uS = sonar.ping();
distance = uS / US_ROUNDTRIP_CM;
if (uS == NO_ECHO) // if the sensor did not get a ping
{
distance = MAX_DISTANCE;
x vaulue of the sensor

//so the distance must be bigger then the ma

}
Serial.print("Ping: "); //to check distance on the serial monitor
Serial.print(distance);
Serial.println("cm");

}
}

void sweepServo()
{
runEvery(servoDelay) //this loop determines the servo position
{
if(pos < 165 && servoDirection == 0)

// 165 = servo to the left

{
pos = pos + 5;

// +1 was to slow

}
if(pos > 15 && servoDirection == 1)

// 15 = servo to the right

{
pos = pos - 5;
}
}
if (pos == 165 ) //new adding 165
{
servoDirection = 1; //changes the direction
}
if (pos == 15 )// new adding 15
{
servoDirection = 0; //changes the direction
}
myservo.write(pos); //move that servo!
}

You might also like