Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my Arduino code. I have a robot that walks and is connected to

ID: 3777254 • Letter: I

Question

I need help with my Arduino code. I have a robot that walks and is connected to two motors. One motor controls the drive(backwards and forwards) the other one controls the direction. I need help with making the motor turn gradually when its making its turns left and right. If someone can help edit my code so that the robot turns smoothly. Here is a video of the robot if anyone is interested in seeing it https://vid.me/1Ift

You don't have to do this but i was told by a friend to implement this method when turning . I need this to be implemented for each time the bot turns.

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

#include <Servo.h>

int trigPin1 = 11;
int echoPin1 = 10;
int trigPin2 = 7;
int echoPin2 = 6;
int trigPin3 = 42;
int echoPin3 = 43;

int distance1, distance2, distance3;
long duration1, duration2, duration3;

Servo servoDrive; // Define left servo
Servo servoDirection; // Define right servo

boolean isFrontWallThere = false;
boolean isARightWallThere = false;
boolean isALeftWallThere = false;

void setup() {
Serial.begin(9600);

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);

pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
  
servoDrive.attach(18); // Set left servo to digital pin 18
servoDirection.attach(14); // Set right servo to digital pin 14
}

void loop() {

digitalWrite (trigPin1, LOW);
delayMicroseconds(2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1;
if ((distance1 > 0 && distance1 < 15)) {
isFrontWallThere = true;
}
else {
isFrontWallThere = false;
}

digitalWrite (trigPin2, LOW);
delayMicroseconds(2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;

if ((distance2 > 0 && distance2 < 15)) {
isALeftWallThere = true;
}
else {
isALeftWallThere = false;
}

digitalWrite (trigPin3, LOW);
delayMicroseconds(2);
digitalWrite (trigPin3, HIGH);
delayMicroseconds (5);
duration3 = pulseIn (echoPin3, HIGH);
distance3 = (duration3 / 2) / 29.1;

if ((distance3 > 0 && distance3 < 15)) {
isARightWallThere = true;
}
else {
isARightWallThere = false;
}
Serial.print("Front Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
Serial.print("Left Sensor: ");
Serial.print(distance2);
Serial.print("cm ");
Serial.print("Right Sensor: ");
Serial.print(distance3);
Serial.print("cm");
Serial.println(" ");

if (isFrontWallThere == false && isARightWallThere == false && isALeftWallThere == false)//NO OBSUTRCTIONS CAN MOVE FORWARD
{
  
servoDrive.write(180);//MOVES FORWARDS
servoDirection.write(130);//CENTERS THE DIRECTION OF THE ROBOT
Serial.println(" ");
Serial.println("All Clear! going forwards");
delay(300);
}
//moving to back
else if (isFrontWallThere == true && isARightWallThere == false && isALeftWallThere == false)
{

servoDrive.write(0);//MOVES BACKWARDS
servoDirection.write(130);//CENTERS THE DIRECTION OF THE ROBOT
delay(7000);

if(distance2 > distance3){
//move left since distance from left wall to the robot is high

servoDrive.write(180);//MOVES FORWARD
servoDirection.write(180);//MOVES LEFT
delay(2000);
}

else{ //move right

servoDrive.write(180);//MOVES FORWARD
servoDirection.write(45);//MOVES RIGHT
Serial.println(" ");
Serial.println("Object detected in front of me.....moving back");
delay(2000);
}


}
//moving to right
else if (isFrontWallThere == false && isARightWallThere == false && isALeftWallThere == true)
{
servoDrive.write(180);//MOVES FORWARD
servoDirection.write(45);//MOVES RIGHT
Serial.println(" ");
Serial.println("Object detected to the LEFT.....moving to the right");
delay(2000);

}

//moving to left
else if (isFrontWallThere == false && isARightWallThere == true && isALeftWallThere == false) {
servoDrive.write(180);//MOVES FORWARD
servoDirection.write(180);//MOVES LEFT
Serial.println(" ");
Serial.println("Object detected to the RIGHT.....moving to the left");
delay(2000);
}

}

Explanation / Answer

#include <Servo.h>
int trigPin1 = 11;
int echoPin1 = 10;
int trigPin2 = 7;
int echoPin2 = 6;
int trigPin3 = 42;
int echoPin3 = 43;
int distance1, distance2, distance3;
long duration1, duration2, duration3;
Servo servoDrive; // Define left servo
Servo servoDirection; // Define right servo
boolean isFrontWallThere = false;
boolean isARightWallThere = false;
boolean isALeftWallThere = false;
void setup() {
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);

servoDrive.attach(18); // Set left servo to digital pin 18
servoDirection.attach(14); // Set right servo to digital pin 14
}
void loop() {
digitalWrite (trigPin1, LOW);
delayMicroseconds(2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1;
if ((distance1 > 0 && distance1 < 15)) {
isFrontWallThere = true;
}
else {
isFrontWallThere = false;
}
digitalWrite (trigPin2, LOW);
delayMicroseconds(2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;
if ((distance2 > 0 && distance2 < 15)) {
isALeftWallThere = true;
}
else {
isALeftWallThere = false;
}
digitalWrite (trigPin3, LOW);
delayMicroseconds(2);
digitalWrite (trigPin3, HIGH);
delayMicroseconds (5);
duration3 = pulseIn (echoPin3, HIGH);
distance3 = (duration3 / 2) / 29.1;
if ((distance3 > 0 && distance3 < 15)) {
isARightWallThere = true;
}
else {
isARightWallThere = false;
}
Serial.print("Front Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
Serial.print("Left Sensor: ");
Serial.print(distance2);
Serial.print("cm ");
Serial.print("Right Sensor: ");
Serial.print(distance3);
Serial.print("cm");
Serial.println(" ");

if (isFrontWallThere == false && isARightWallThere == false && isALeftWallThere == false)//NO OBSUTRCTIONS CAN MOVE FORWARD
{

servoDrive.write(180);//MOVES FORWARDS
servoDirection.write(130);//CENTERS THE DIRECTION OF THE ROBOT
Serial.println(" ");
Serial.println("All Clear! going forwards");
delay(300);
}
//moving to back
else if (isFrontWallThere == true && isARightWallThere == false && isALeftWallThere == false)
{

servoDrive.write(0);//MOVES BACKWARDS
servoDirection.write(130);//CENTERS THE DIRECTION OF THE ROBOT
delay(7000);
if (distance2 > distance3) {
//move left since distance from left wall to the robot is high
servoDrive.write(180);//MOVES FORWARD
servoDirection.write(180);//MOVES LEFT
delay(2000);
}
else { //move right
servoDrive.write(180);//MOVES FORWARD
servoDirection.write(45);//MOVES RIGHT
Serial.println(" ");
Serial.println("Object detected in front of me.....moving back");
delay(2000);
}

}
//moving to right
else if (isFrontWallThere == false && isARightWallThere == false && isALeftWallThere == true)
{
servoDrive.write(0);//MOVES FORWARD

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servoDirection.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}


//servoDirection.write(45);//MOVES RIGHT
Serial.println(" ");
Serial.println("Object detected to the LEFT.....moving to the right");
delay(2000);
}
//moving to left
else if (isFrontWallThere == false && isARightWallThere == true && isALeftWallThere == false) {
servoDrive.write(0);//MOVES FORWARD
//servoDirection.write(180);//MOVES LEFT

for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
// in steps of 1 degree
servoDirection.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

Serial.println(" ");
Serial.println("Object detected to the RIGHT.....moving to the left");
delay(2000);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote