user32546: i have a project about making an obstacle avoiding robot. But this one is a bit special. I want the robot to move forward and when an object is close to it, to turn right. https://i.stack.imgur.com/Vfnzi.png
#define echoPin 7 // Echo Pin

#define LEDPin 13 // Onboard LED

#define m1a 4

#define m1b 3

#define ena 9

#define m2a 2

#define m2b 5

#define enb 8

int maximumRange = 200; // Maximum range needed

int minimumRange = 0; // Minimum range needed

long duration, distance=0; // Duration used to calculate distance


void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)

pinMode(m1a, OUTPUT);

pinMode(m1b, OUTPUT);

pinMode(m2a, OUTPUT);

pinMode(m2b, OUTPUT);

pinMode(ena, OUTPUT);

pinMode(enb, OUTPUT);

}
  void moveForward()
 {

 digitalWrite(LEDPin, LOW); 

 digitalWrite(m1a, LOW);

 digitalWrite(m1b, HIGH);

 analogWrite(ena, 180);

 digitalWrite(m2a,HIGH);

 digitalWrite(m2b,LOW);

 analogWrite(enb,180);

 }
 void turnRight()

{


 digitalWrite(LEDPin, HIGH); 

 digitalWrite(m1a, LOW);

 digitalWrite(m1b, LOW);

 analogWrite(ena, 100);

 digitalWrite(m2a,HIGH);

 digitalWrite(m2b,LOW);

 analogWrite(enb,100);

 }

void loop() {

 digitalWrite(trigPin, LOW); 

 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.

 distance = duration/58.2;

if (distance <= maximumRange && distance >= minimumRange){
    turnRight();
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
 moveForward();
}

//Delay 50ms before next reading.
 delay(50);
}


user20158: 
I want the robot to move forward and when an object is close to it, to turn right.

How close do you want the robot to be to the obstacle? You should at least declare some form of threshold to determine what values are considered "close". (Maybe a closeThreshold variable)

while(distance>=maximumDistance) { moveForward(); if(distance<=maximumDistance) break; } turnRight();

As for the code you have posted, why not use a simple if else function with a closeThreshold variable instead of a while loop?
...
void loop() { 
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10);
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.

 distance = duration/58.2; 
 if(distance<=closeThreshold) { 
 turnRight(); 
 } else {
 moveForward();
 }
}


32546: <p>i have a project about making an obstacle avoiding robot. But this one is a bit special. I want the robot to move forward and when an object is close to it, to turn right. <a href="https://i.stack.imgur.com/Vfnzi.png" rel="nofollow noreferrer">https://i.stack.imgur.com/Vfnzi.png</a></p>

<pre><code>#define echoPin 7 // Echo Pin

#define LEDPin 13 // Onboard LED

#define m1a 4

#define m1b 3

#define ena 9

#define m2a 2

#define m2b 5

#define enb 8

int maximumRange = 200; // Maximum range needed

int minimumRange = 0; // Minimum range needed

long duration, distance=0; // Duration used to calculate distance


void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)

pinMode(m1a, OUTPUT);

pinMode(m1b, OUTPUT);

pinMode(m2a, OUTPUT);

pinMode(m2b, OUTPUT);

pinMode(ena, OUTPUT);

pinMode(enb, OUTPUT);

}
  void moveForward()
 {

 digitalWrite(LEDPin, LOW); 

 digitalWrite(m1a, LOW);

 digitalWrite(m1b, HIGH);

 analogWrite(ena, 180);

 digitalWrite(m2a,HIGH);

 digitalWrite(m2b,LOW);

 analogWrite(enb,180);

 }
 void turnRight()

{


 digitalWrite(LEDPin, HIGH); 

 digitalWrite(m1a, LOW);

 digitalWrite(m1b, LOW);

 analogWrite(ena, 100);

 digitalWrite(m2a,HIGH);

 digitalWrite(m2b,LOW);

 analogWrite(enb,100);

 }

void loop() {

 digitalWrite(trigPin, LOW); 

 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.

 distance = duration/58.2;

if (distance &lt;= maximumRange &amp;&amp; distance &gt;= minimumRange){
    turnRight();
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
 moveForward();
}

//Delay 50ms before next reading.
 delay(50);
}
</code></pre>
 20158: <blockquote>
  <p>I want the robot to move forward and when an object is close to it, to turn right.</p>
</blockquote>

<p>How close do you want the robot to be to the obstacle? You should at least declare some form of threshold to determine what values are considered "close". (Maybe a <em>closeThreshold</em> variable)</p>

<blockquote>
  <p>while(distance>=maximumDistance) { moveForward(); if(distance&lt;=maximumDistance) break; } turnRight();</p>
</blockquote>

<p>As for the code you have posted, why not use a simple if else function with a <em>closeThreshold</em> variable instead of a while loop?</p>

<pre><code>...
void loop() { 
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10);
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.

 distance = duration/58.2; 
 if(distance&lt;=closeThreshold) { 
 turnRight(); 
 } else {
 moveForward();
 }
}
</code></pre>
