forward - distance - stop - right

Hello again guys.I have a problem with my robot.I want it to go forward and if the distance <=10 ,then stop for 2 sec then turn right for 1 sec. and then go forward again...
The problem is that the robot when the distance <= 10 ,stops { while distance <= 10 } and then go forward.
Nor stops for 2 sec ,nor turn right for 1 sec...
Any help ,please.....I tried many methods ,but still nothing.

CODE:

const int IN_1_back_left = 53;
const int IN_2_back_left = 51;
const int PWM_back_left = 2;
const int IN_1_back_right = 49;
const int IN_2_back_right = 47;
const int PWM_back_right = 3;
const int IN_1_front_left = 45;
const int IN_2_front_left = 43;
const int PWM_front_left = 4;
const int IN_1_front_right = 41;
const int IN_2_front_right = 39;
const int PWM_front_right = 3;

boolean enable_stop;
boolean enable_right;

unsigned long presentMillis = 0;

const int Trig_pin = 8;
const int Echo_pin = 10;

void setup()
{
  Serial.begin(9600);
  pinMode(Echo_pin , INPUT);
  pinMode(Trig_pin , OUTPUT);
  analogWrite(PWM_back_left , 255);
  analogWrite(PWM_back_right , 255);
  analogWrite(PWM_front_left , 255);
  analogWrite(PWM_front_right , 255);
  pinMode(PWM_back_left, OUTPUT);
  pinMode(PWM_back_right, OUTPUT);
  pinMode(PWM_front_left, OUTPUT);
  pinMode(PWM_front_right, OUTPUT);

  pinMode(IN_1_back_left, OUTPUT);
  pinMode(IN_2_back_left, OUTPUT);
  pinMode(IN_1_back_right, OUTPUT);
  pinMode(IN_2_back_right, OUTPUT);

  pinMode(IN_1_front_left, OUTPUT);
  pinMode(IN_2_front_left, OUTPUT);
  pinMode(IN_1_front_right, OUTPUT);
  pinMode(IN_2_front_right, OUTPUT);
}


void FORWARD()
{
  flash_enabled = false;
  digitalWrite(IN_1_back_left , HIGH);      //fordward
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , HIGH);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , HIGH);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , HIGH);
  digitalWrite(IN_2_front_right , LOW);
}


void BACKWARD()
{
  flash_enabled = false;
  digitalWrite(IN_1_back_left , LOW);       //backward
  digitalWrite(IN_2_back_left , HIGH);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , HIGH);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , HIGH);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , HIGH);
}


void STOP()
{
  flash_enabled = true;
  digitalWrite(IN_1_back_left , LOW);       //stop
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , LOW);
}


void RIGHT()
{
  flash_enabled = false;
  digitalWrite(IN_1_back_left , HIGH);      //right
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , HIGH);

  digitalWrite(IN_1_front_left , HIGH);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , HIGH);
}


void LEFT()
{
  flash_enabled = false;
  digitalWrite(IN_1_back_left , LOW);      //left
  digitalWrite(IN_2_back_left , HIGH);
  digitalWrite(IN_1_back_right , HIGH);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , HIGH);
  digitalWrite(IN_1_front_right , HIGH);
  digitalWrite(IN_2_front_right , LOW);
}




void loop()
{
  unsigned long currentMillis = millis();


  //ultrasonic_sensor();
  long duration;
  float distance;
  digitalWrite(Trig_pin , HIGH);           
  delayMicroseconds(11);                   
  digitalWrite(Trig_pin , LOW);            

  duration = pulseIn(Echo_pin , HIGH);     
  distance = duration * 0.034 / 2;         
  Serial.println(distance);
  check_blink_led();

  if (distance <= 10)
  {
    enable_stop = true;
    if (enable_stop && currentMillis - presentMillis >= 2000)
    {
      STOP();
      enable_stop = false;
      enable_right = true;
      presentMillis = currentMillis;
    }
  }
  if (enable_right)
  {
    if (currentMillis - presentMillis >= 1000)          
    {
      RIGHT();
      enable_right = false;
      presentMillis = currentMillis;
    }
  }

  else
  {
    FORWARD();
  }
}

millis() only... :slight_smile:

arduiNICK:
I want it to go forward and if the distance <=10 ,then stop for 2 sec then turn right for 1 sec. and then go forward again...
The problem is that the robot when the distance <= 10 ,stops { while distance <= 10 } and then go forward.
Nor stops for 2 sec ,nor turn right for 1 sec...

This doesn't make sense to read in plain English. So how could anyone possibly put this into code?

Sorry for my english.What do not you understand ??

arduiNICK:
Sorry for my english.What do not you understand ??

I'm not trying to be a grammar police. Sometimes it is better to write what we want to do in Pseudocode since we all speak code. And, sometimes, that leads us to structuring our program which leads us to actual code.

forward and if the distance <=10

This does not make any sense. Now that I understand it may be a language error, perhaps you mean to say

do moveForward();
while (distance <=10);
delay(2000);
turnRight();
...

I want the robot to go forward and if the distance <= 10 ,robot stop for 2 sec ,then turn right for 1 sec and continue the loop from the beginning...but all these with millis() function.

if the distance <= 10 ,robot stop for 2 sec

What distance?

distance less than 10 cm

Less than 10 cm from where?

Hint: zero distance is less than 10 cm.

So,what do you want to say with this?
I said ,I want distance less than 10 cm...
Do you understand?

arduiNICK:
So,what do you want to say with this?
I said ,I want distance less than 10 cm...
Do you understand?

If we did understand, we would be answering your question and not asking them.

So,what do you want to say with this?

Let me be blunt, and clear: your specification is complete nonsense.

Try again.

I have a robot and an ultrasonic sensor and I want the robot to go fordward.
If the robot find an obstacle (distance < 10) ,I want to stop the robot for 2 seconds with millis() and after these 2 seconds robot turn right for 1 second.

Better.

Now, explain what you mean by "turn right for 1 second".

One interpretation might be "robot shall spin clockwise on its vertical axis for one second".

I mean I want to rotate the robot right for 1 second.
Am I clear?

Any help?

Fine.

The timing and logic in the code below is wrong. When was the last time that presentMillis was updated?

void loop()
{
  unsigned long currentMillis = millis();
...
 if (distance <= 10)
  {

    enable_stop = true;
    if (enable_stop && currentMillis - presentMillis >= 2000)
    {
      STOP();
      enable_stop = false;
      enable_right = true;
      presentMillis = currentMillis;
    }

In order to use millis() sensibly, you need to:

  1. remember the correct start time (in this case, when distance to object was first found to be less than 10 cm), then

  2. take appropriate actions until (current time)-(start time) is greater than 2 seconds.

Since every time distance is found to be less than 10 cm, enable_stop is set true, it doesn't matter that enable_stop is set false in the if clause below.

I tried this ,but it hasn't very good results...Can you help me more?

void loop()
{

ultrasonic_sensor();
  if (distance <= 10)
  {
    if (currentMillis - presentMillis >= 0)
    {
      STOP();
      presentMillis = currentMillis;
    }
  }

  else if (distance > 10.5 && currentMillis - presentMillis >= 2000)
  {
    FORWARD();
    presentMillis = currentMillis;
  }
}

Can anyone help me about my problem?

    if (currentMillis - presentMillis >= 0)

When do currentMillis and presentMillis ever change?

Why would you care whether their difference is greater than or equal zero?

You lack some very important basics, and until you learn them, your code will not work. Please study these two tutorials carefully.

https://www.gammon.com.au/blink