Programming Codes Obstacle Avoiding Robocar
Programming Codes Obstacle Avoiding Robocar
void setup()
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
servoLook.attach(10); //Assign the servo pin
pinMode(trig,OUTPUT); //Assign ultrasonic sensor pin modes
pinMode(echo,INPUT);
}
void loop()
{
servoLook.write(90); //Set the servo to look straight ahead
delay(750);
int distance = getDistance(); //Check that there are no objects ahead
if(distance >= stopDist) //If there are no objects within the stopping distance,
move forward
{
moveForward();
}
while(distance >= stopDist) //Keep checking the object distance until it is within
the minimum stopping distance
{
distance = getDistance();
delay(250);
}
stopMove(); //Stop the motors
int turnDir = checkDirection(); //Check the left and right object distances and get the
turning instruction
Serial.print(turnDir);
switch (turnDir) //Turn left, turn around or turn right depending on the
instruction
{
case 0: //Turn left
turnLeft (400);
break;
case 1: //Turn around
turnLeft (700);
break;
case 2: //Turn right
turnRight (400);
break;
}
}
void decelerate() //Function to decelerate the motors from full speed to zero
{
for (int i=motorSpeed; i!=0; i--) //Loop from full speed to 0
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}
void turnLeft(int duration) //Set motors to turn left for the specified duration
then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(BACKWARD);
leftBack.run(BACKWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
void turnRight(int duration) //Set motors to turn right for the specified
duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
int checkDirection() //Check the left and right directions and decide
which way to turn
{
int distances [2] = {0,0}; //Left and right distances
int turnDir = 1; //Direction to turn, 0 left, 1 reverse, 2 right
servoLook.write(180); //Turn servo to look left
delay(500);
distances [0] = getDistance(); //Get the left object distance
servoLook.write(0); //Turn servo to look right
delay(1000);
distances [1] = getDistance(); //Get the right object distance
if (distances[0]>=200 && distances[1]>=200) //If both directions are clear, turn left
turnDir = 0;
else if (distances[0]<=stopDist && distances[1]<=stopDist) //If both directions are blocked,
turn around
turnDir = 1;
else if (distances[0]>=distances[1]) //If left has more space, turn left
turnDir = 0;
else if (distances[0]<distances[1]) //If right has more space, turn right
turnDir = 2;
return turnDir;
}
PARALLEL PARKING
#include <NewPing.h>
#define TRIGGER_PIN1 13
#define ECHO_PIN1 12
#define MAX_DISTANCE1 200
NewPing sonar1 ( TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1);
int d1;
void Read () ;
#define TRIGGER_PIN2 11
#define ECHO_PIN2 10
#define MAX_DISTANCE2 200
NewPing sonar2 ( TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);
int d2;
#define TRIGGER_PIN3 9
#define ECHO_PIN3 8
#define MAX_DISTANCE3 200
NewPing sonar3 ( TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE3);
int d3;
#define TRIGGER_PIN4 7
#define ECHO_PIN4 6
#define MAX_DISTANCE4 200
NewPing sonar4 ( TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE4);
int d4;
int in1LF = 2;
int in2LR = 3;
int in3RR = 4;
int in4RF = 5;
void setup() {
pinMode(in1LF, OUTPUT);
pinMode(in2LR, OUTPUT);
pinMode(in3RR, OUTPUT);
pinMode(in4RF, OUTPUT);
Serial.begin (9600);
}
void loop()
{
Read();
if ((d1 < 10 && d3 < 15 && d4 < 15) || (d2 < 5 && d3 < 15 && d4 < 15)) {
digitalWrite (in1LF, LOW);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, LOW);
}
else if (d1 > 10) {
digitalWrite (in1LF, HIGH);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, HIGH);
if (d3 < 55 && d3 > 27) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
while (1) {
digitalWrite(in1LF, HIGH);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
Read ();
if (d1 > 20 && d3 > 40)
{ break;
}
}
}
else if (d4 < 55 && d4 > 27) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
while (1) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, HIGH);
digitalWrite(in2LR, HIGH);
Read ();
if (d1 > 20 && d4 > 40)
{ break;
}
}
}
}
else if (d1 < 10 && d2 > 70 && d3 < 15 && d4 < 15)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
while (1)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, HIGH);
Read ();
if (d2 > 10)
{ break;
}
}
}
}
void Read()
{
int uS1 = sonar1.ping();
Serial.print("pingF: ");
Serial.print(uS1 / US_ROUNDTRIP_CM);
Serial.println("cm ");
d1 = uS1 / US_ROUNDTRIP_CM;
String readString;
int x=90, y=90, z=90, p=90;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;
void setup()
{
Serial.begin(9600);
myservoa.attach(3);
myservob.attach(5);
myservoc.attach(6);
myservod.attach(9);
myservoa.write(x);
myservob.write(y);
myservoc.write(z);
myservod.write(p);
}
void loop()
{
if (Serial.available())
{
char m = Serial.read();
if (m == '/')
{
if (readString.length() >1)
{
Serial.println(readString);
readString="";
}
}
else
{
readString += m;
}
}
}