-1

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);
}
  • m1a,m1b,m2a,m2b are the motor pins. ena, enb are the pwm pins from the driver(l298n). I am currently using an arduino uno, and hc-sr04 ultrasonic sensor and a l298n motor shield – Gabi Dobre Apr 6 '17 at 0:16
  • 2
    Please edit your question (click "edit") and include the information from your comment in the body of the question. Also, please edit your question and mark the code as code. When including code in a stackexchange question or answer, first paste the code into the question or answer editing box; then in that editing box, highlight the code and press ctrl-k. Or highlight the code and click the {} icon in the toolbar at the top of the editing box. To highlight text click-drag across it or use the arrow keys while holding the shift key. – James Waldby - jwpat7 Apr 6 '17 at 0:39
1

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();
 }
}
  • oh well..the robot now is stuck right now. – Gabi Dobre Apr 6 '17 at 19:36
  • Stuck as in not moving at all or stammering at a particular spot? – Photon Apr 7 '17 at 2:18
  • Not moving at all – Gabi Dobre Apr 7 '17 at 4:41
  • I created a variable called maximumDistance, and if the current distance is smaller than that, the obstacle is considered close enough. It's the same with the closeThreshold variable – Gabi Dobre Apr 7 '17 at 4:44
  • nope. it's not working at all – Gabi Dobre Apr 7 '17 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.