#define left_motor 10 // Left motor speed control;
#define right_motor 11 // Controls speed of the right engine;
#define e1 8 // Controls the direction of rotation of the left engine;
#define e2 9 // Controls the direction of rotation of the left engine;
#define d1 12 // Controls the direction of rotation of the right engine;
#define d2 13 // Controls the direction of rotation of the right engine;
int trigger_front = A4; // Controls the impulse sent from the front sensor
int echo_front = A5; // Controls the pulse received from the front sensor
int trigger_left = A2; // Controls the impulse sent from the front sensor
int echo_left = A3; // Controls the pulse received from the front sensor
int trigger_right = A0; // Controls the impulse sent from the front sensor
int echo_right = A1; // Controls the pulse received from the front sensor
// Configuration of input types of variables declared;
void setup()
{
pinMode(trigger_front, OUTPUT); // Arduino signal output trigger forward
pinMode(echo_front, INPUT); // Arduino signal input echo front
pinMode(trigger_left, OUTPUT); // Arduino signal output trigger forward
pinMode(echo_left, INPUT); // Arduino signal input echo front
pinMode(trigger_right, OUTPUT); // Arduino signal output trigger forward
pinMode(echo_right, INPUT); // Arduino signal input echo front
pinMode(left_motor, OUTPUT); // signal output from the Arduino's left engine speed
pinMode(right_motor, OUTPUT); // signal output of the right engine speed Arduino
pinMode(e1, OUTPUT); // Arduino signal output control the direction of rotation of the left engine
pinMode(e2, OUTPUT); // Arduino signal output control the direction of rotation of the left engine
pinMode(d1, OUTPUT); // Arduino signal output control the direction of rotation of the right engine
pinMode(d2, OUTPUT); // Arduino signal output control the direction of rotation of the right engine
}
// code without endless repetition of the project;
void loop()
{
// declaration of variables used for project control;
long duration_front, duration_left, duration_right, rightt, leftt, front;
// It is declared the corresponding inputs and outputs//signal of the ultrasonic sensor and stored by the sensor that converts variable//the speed of sound is 340m / s or microseconds per centimeter, as // the signal goes and returns this time is the sensor half being = Time / 29/2; // thus follows also the other two sensors
digitalWrite(trigger_front, LOW);
delayMicroseconds(2);
digitalWrite(trigger_front, HIGH);
delayMicroseconds(5);
digitalWrite(trigger_front, LOW);
duration_front = pulseIn(echo_front, HIGH);
front = duration_front/29/2;
digitalWrite(trigger_left, LOW);
delayMicroseconds(2);
digitalWrite(trigger_left, HIGH);
delayMicroseconds(5);
digitalWrite(trigger_left, LOW);
duration_left = pulseIn(echo_left, HIGH);
rightt = duration_left/29/2;
digitalWrite(trigger_right, LOW);
delayMicroseconds(2);
digitalWrite(trigger_right, HIGH);
delayMicroseconds(5);
digitalWrite(trigger_right, LOW);
duration_right = pulseIn(echo_right, HIGH);
leftt = duration_right/29/2;
analogWrite(left_motor, 0); //block to initialize the inputs with pulse 0 // or off;
analogWrite(right_motor, 0); //
analogWrite(e1, 0); //
analogWrite(e2, 0); //
analogWrite(d1, 0); //
analogWrite(d2, 0); //
if(front>8)
{
//the use of the four if's down if within that are to control // handling robot,
// in order to keep it following the right wall straight;
if(rightt >8 && rightt< 13) // If the distance to the right wall // is between 9 and 12 cm, the robot remains straight;
{
analogWrite(left_motor, 120);
analogWrite(right_motor, 120);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 0);
analogWrite(d2, 255);
}
if(rightt >=13) //If the distance to the right wall is greater than or equal to 13 cm, the robot increases its left engine speed to approach the right wall;
{
analogWrite(left_motor, 140);
analogWrite(right_motor, 100);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 0);
analogWrite(d2, 255);
}
if(leftt <=8) // if in case the distance from the wall to the left is less than or equal to 8 cm, the robot increases its right engine speed to distance the left wall;
{
analogWrite(left_motor, 140);
analogWrite(right_motor, 100);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 0);
analogWrite(d2, 255);
}
if(rightt <=8) // if in case the distance from the wall to the right is less than or equal to 8 cm, the robot increases its right engine speed to distance themselves from the right wall;
{
analogWrite(left_motor, 100);
analogWrite(right_motor, 140);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 0);
analogWrite(d2, 255);
}
}
if(leftt <=20 && rightt>20 && front <=8) right(); //if in case the distance // front is less than or equal to 8 cm, the distance to the right is greater than 20 cm and the distance // the left is less than or equal to 20 cm it calls the function dir ();
if(leftt >20 && rightt>20 && front <=8) right(); //If the distance to the front // is less than or equal to 8 cm, the distance from the right is larger than 20 cm and the distance to left // is greater than 20 cm tell it calls the function ();
if(rightt <=20 && leftt>20 && front <=8) left(); //If the distance to the front // is less than or equal to 8 cm, the distance from the right is less than or equal to 20 cm and the distance from the left // is greater than 20 cm it calls the function Left ();
if(rightt <=20 && leftt<=20 && front <=8) voltar(); //if in case the distance // front is less than or equal to 8 cm, the distance to the right is less than or equal to 20 cm and the distance // the left is less than or equal to 20 cm it calls the function back ();
}
void left() //function to make the robot turn 90 left if he did not have exit the front and right;;
{
analogWrite(left_motor, 0);
analogWrite(right_motor, 0);
delay(100);
analogWrite(left_motor, 120);
analogWrite(right_motor, 120);
analogWrite(e1, 0);
analogWrite(e2, 255);
analogWrite(d1, 0);
analogWrite(d2, 255);
delay(700);
}
void right() // function to make the robot turn 90 right up if you do not exit the front or the left;
{
analogWrite(left_motor, 0);
analogWrite(right_motor, 0);
delay(100);
analogWrite(left_motor, 120);
analogWrite(right_motor, 120);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 255);
analogWrite(d2, 0);
delay(800);
}
void voltar() // function to make the robot turn 180 if he did not have exit the front, right and left;
{
analogWrite(left_motor, 120);
analogWrite(right_motor, 120);
analogWrite(e1, 255);
analogWrite(e2, 0);
analogWrite(d1, 255);
analogWrite(d2, 0);
delay(1400);
}
No comments:
Post a Comment