ultrasonic robot - obstacle course program tip

So I have a robot (from makeblock) with one ultrasonic sensor on it.

But I have some problems with the the programming.

Here is the program:

#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

MeDCMotor MotorL(M1);  
MeDCMotor MotorR(M2);
MeUltrasonicSensor UltrasonicSensor(PORT_3);
int moveSpeed = 100;
int turnSpeed = 130;
int distance=0;



void setup() {
 // put your setup code here, to run once:
   }



void loop() 
 // put your main code here, to run repeatedly:

{
 
distance = UltrasonicSensor.distanceCm();
 
//Serial.println(distance);
 

 
 if(distance>2&&distance<23)
 {
   Turnright();
     delay(500);
     Brake();
     delay(1000);
        
     if(distance>23)
      
      {
       Forward();
      }
     
     Turnleft();
     delay(850);
     Brake();
     delay(1000);
     if(distance>23)
       {
       Forward();
       }
   
  
 }
 else
 {
      Forward();
     
 }
 delay(150);
}

void Brake()
{
 MotorL.stop();
 MotorR.stop();
}

void Forward()
{
 MotorL.run(moveSpeed);
 MotorR.run(moveSpeed);
}

void Turnleft()
{
 MotorL.run(-turnSpeed);
 MotorR.run(turnSpeed);
}

void Turnright()
{
 MotorL.run(turnSpeed);
 MotorR.run(-turnSpeed);
}

Here is a picture if you want to see: Imgur: The magic of the Internet

So what you want with the program is:

  1. if the sensor sence an object between 2-23 cm it first turns right for 0.5 sec, takes a short brake

  2. then again looks if their is an object in the intervall, if their is no object (distance>23) it starts moving forward, but if their is still an object their it turns left for a longer time 0.85 sec and now again looks if it is an object their or not and then start the program over.

But when I run the robot and it sense an object it turns right then it turns left, AND THEN it looks if their is no object in front and starts to drive forward again. So it misses that if sats after Turnright.

You have any ideas?

(maybe solved it with an return function, should try it tomorrow)

  if(distance>2&&distance<23)
  {
    Turnright();
      delay(500);
      Brake();
      delay(1000);
         
      if(distance>23)

If the value in distance is greater than 2 and less than 23, see if it is greater than 23. What are the odds of THAT happening?

PaulS:

  if(distance>2&&distance<23)

{
    Turnright();
      delay(500);
      Brake();
      delay(1000);
       
      if(distance>23)



If the value in distance is greater than 2 and less than 23, see if it is greater than 23. What are the odds of THAT happening?

do you mean that the if sats inside the if sats is dependet of that value? so that the " if(distance>23) "
cant happening because it lay in the "if(distance>2&&distance<23)" ?

do you mean that the if sats inside the if sats is dependet of that value? so that the " if(distance>23) "
cant happening because it lay in the "if(distance>2&&distance<23)" ?

I think I do.

PaulS:
I think I do.

hmm, do you have any tips on how to do it then?

I think you understand what I want to do with the code?

I think you understand what I want to do with the code?

I'm not sure that I do. It appears that you want to do something if the distance is small, and something else (there's a clue) if the distance is not small.

if(distance>2&&distance<23)
{
   // Do whatever should happen when there is an obstacle close
}
else
{
   // Distance is greater than or equal to 23, so lots of room out front
}

PaulS:
I'm not sure that I do. It appears that you want to do something if the distance is small, and something else (there's a clue) if the distance is not small.

if(distance>2&&distance<23)

{
  // Do whatever should happen when there is an obstacle close
}
else
{
  // Distance is greater than or equal to 23, so lots of room out front
}

Well, so lets say we have an easy labyrint that first goes straight, then a right turn, straight, then left turn and so on or the other way round.

So it goes straight as long as there is no object in 2-23 cm , then when the wall comes , it shoulds turn right for 0,5 sec and then stop for a short time, then it looks again if the object is still in range ( 2-23 cm, witch it will not be as it was a right turn), if it is not the robot start goin straight again.

Then comes the left turn, and it notice that an object is within 2-23 cm, it starts to turn right for just 0.5 sec and stops for a short while , it looks if there is an object within the interval, but this time the wall is still there as it is a left turn, so now it makes a longer left turn for 0.85 sec, (then it maybe turns a couple of time) but as the left turn is longer it will in the end have no object in front of it start to go straight again and then it have managed to take both right and left turn.

Do you understand? :slight_smile:

So it goes straight as long as there is no object in 2-23 cm

See, that is where you don't seem to understand how loop() works. The loop() function could (and should) iterate many thousands of times while there is nothing close in front of the bot. What it should do each time it iterates is check the distance. If it is still good, do nothing.

Only if it is not should you do something. That something, whatever it is you decide to do, should block loop() from ending until the operation is complete.

At that point, loop() will be called again, and the distance in front of the bot checked, and used to decide what to do - stop, go forward, turn right, turn left, whatever.

There should NOT be a while loop in loop(). The loop() function already runs in a while loop.

@xefyros: What you are going to need to implement is called a state machine; google around, this has been discussed here on the forum several times.

PaulS:
See, that is where you don't seem to understand how loop() works. The loop() function could (and should) iterate many thousands of times while there is nothing close in front of the bot. What it should do each time it iterates is check the distance. If it is still good, do nothing.

Only if it is not should you do something. That something, whatever it is you decide to do, should block loop() from ending until the operation is complete.

At that point, loop() will be called again, and the distance in front of the bot checked, and used to decide what to do - stop, go forward, turn right, turn left, whatever.

There should NOT be a while loop in loop(). The loop() function already runs in a while loop.

So i put "
if (distance>23)
Forward();
"

In the void setup

and then the "

if(distance>2&&distance<23)
{
Turnright();
delay(500);
Brake();
delay(1000);
Turnleft();
delay(850);
Brake();
delay(1000);
}
"
In void loop?

So i put "
if (distance>23)
 Forward(); 
"

In the void setup

Where is "distance" assigned a value in setup?

AWOL:

So i put "

if (distance>23)
Forward();
"

In the void setup



Where is "distance" assigned a value in setup?

I have this in the beginning of the program but you maybe mean that I should have

distance = UltrasonicSensor.distanceCm(); in the setup also? :slight_smile:

int distance=0

Just post the code.

AWOL:
Just post the code.

My first post was with the code but here it is again:

#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
**MeDCMotor MotorL(M1); **
MeDCMotor MotorR(M2);
MeUltrasonicSensor UltrasonicSensor(PORT_3);
int moveSpeed = 100;
int turnSpeed = 130;
int distance=0;
void setup() {
// put your setup code here, to run once:

}
void loop()
// put your main code here, to run repeatedly:
{
distance = UltrasonicSensor.distanceCm();
//Serial.println(distance);

if(distance>2&&distance<23)
{
** Turnleft();**
** delay(500);**
** Brake();**
** delay(1000);**

** if(distance>23)**

** {**
** Forward();**
** }**

** Turnright();**
** delay(850);**
** Brake();**
** delay(1000);**
** if(distance>23)**
** {**
** Forward();**
** }**

}
else
{
** Forward();**

}
delay(150);
}
void Brake()
{
MotorL.stop();
MotorR.stop();
}
void Forward()
{
MotorL.run(moveSpeed);
MotorR.run(moveSpeed);
}
void Turnleft()
{
MotorL.run(-turnSpeed);
MotorR.run(turnSpeed);
}
void Turnright()
{
MotorL.run(turnSpeed);
MotorR.run(-turnSpeed);
}

Yeah, bold tags no, code tags yes.

If you posted in the first post, why did you bother posting it again?

AWOL:
Yeah, bold tags no, code tags yes.

If you posted in the first post, why did you bother posting it again?

what? because you asked for it and maybe missed it.

How could I miss it?

AWOL:
How could I miss it?

Why did you ask me to post the code then?

Because I assumed you'd changed it, like you described! But quite possibly not exactly as you described.

aha okey then I understand you :slight_smile:

yes I tryed and changed the code but it was obviously wrong so i misunderstood him. I changed it too:

void setup() {
// put your setup code here, to run once:
  
  distance = UltrasonicSensor.distanceCm();
 //Serial.println(distance);
 
 if(distance>23)
     {
      Forward();
     }
}

void loop() 
// put your main code here, to run repeatedly:
{
distance = UltrasonicSensor.distanceCm();
//Serial.println(distance);



if(distance>2&&distance<23)
{
  Turnleft();
    delay(500);
    Brake();
    delay(1000);
    Turnright();
    delay(850);
    Brake();
    delay(1000);
   
delay(150);
}

But once it sees a object it dont starts goin straight again as it do not loop :slight_smile: