Running a Dc motor for a specific time after sensor sees obstacle

Hi all,

I'm building an obstacle avoiding robot. It's pretty big as ultimately it will be a vacuuming robot like Roomba. When one of the ultrasonic sensors hears an obstacle, the backing up sequence starts.
As the robot is quite wide, the dc motors are running for not enough amount of time for it to fall back to a safe distance. I tried for loop, millis() and delays but all this for nothing. I need to make those motors run for a bit longer than they are now but only when the sensors are hearing an obstacle. May I ask for your suggestions?

I'm pasting my current code bellow:

//------------------Libraries------------------
#include "NewPing.h"
#include <Servo.h> 

//Variables:

//------------------SONARS------------------

//bottom sonar
#define TRIGGER_PIN 2
#define ECHO_PIN 3

//head sonar
#define TRIGGER_PIN2 4
#define ECHO_PIN2 5

//shared data
#define MAX_DIST 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DIST);
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DIST);

float duration, bottom, duration2, head;
int ledPin = 7;

//------------------MOTORS------------------

// speed
int l_speed = 8; //Left Motor
int r_speed = 9; //Right Motor

// Motor Control Pins
int M1P1 = 10;  
int M1P2 = 11;
int M2P1 = 12;
int M2P2 = 13;


//------------------SERVO------------------
Servo Servo1; 
int servoPin = 6;
int pos = 0; 

//------------------TIMER------------------
int lastmillis1, lastmillis2, lastmillis3, lastmillis4;
int actual;
int T1 = 2000;

void setup() {
  
  pinMode(M1P1,OUTPUT);
  pinMode(M2P1,OUTPUT);
  pinMode(M1P2,OUTPUT);
  pinMode(M2P2,OUTPUT);
  pinMode(l_speed,OUTPUT);
  pinMode(r_speed,OUTPUT);
  Servo1.attach(servoPin); 
  Serial.begin (9600);

}

void HeadBanging() {
//Head Moving
  for (pos = 45; pos <= 135; pos += 1) {
    Servo1.write(pos);              
    delay(20);                       
  }
  for (pos = 135; pos >= 45; pos -= 1) {
    Servo1.write(pos);
    delay(20);                       
  }
}

void loop() {
   analogWrite(l_speed,255);
   analogWrite(r_speed,255); 

HeadBanging();

//Sensors blaazing!

  duration = sonar.ping();
  bottom=(duration / 2)* 0.0343;
  
  duration2 = sonar2.ping();
  head=(duration2 / 2)* 0.0343;
     
//in case of finding a obstacle

   if (head<=7||bottom>=5) {
   Serial.print("oH noOO!! aN oBSTacLE!!! rUUUN! ");
   Serial.print(head);
   Serial.println(" cm. alERT! ALerT! We ALL GonNA DiE!");
   aktualny = millis();
   if((actual - lastmillis1) <= T1)
   {
    lastmillis1 = millis();               
   
                   digitalWrite(M1P1,LOW);    //stop
                   digitalWrite(M2P1,LOW);
                   digitalWrite(M1P2,LOW);
                   digitalWrite(M2P2,LOW);
                        delay(1000);}
    if((actual - lastmillis2) <= T1)
   {
    lastmillis2 = millis();                       
                   digitalWrite(M1P1,LOW);   //move back a bit
                   digitalWrite(M2P1,LOW);
                   digitalWrite(M1P2,HIGH);
                   digitalWrite(M2P2,HIGH);
                         delay(1000);}
    if((actual - lastmillis3) <= T1)
   {
    lastmillis3 = millis();                             
                   digitalWrite(M1P1,HIGH);  //turn left
                   digitalWrite(M2P1,LOW);
                   digitalWrite(M1P2,LOW);
                   digitalWrite(M2P2,HIGH);
                         delay(1000);}}
                         
                         else {
                   Serial.println("Dobie, dobie doo, I'm a happy robot moving forward!");
                   digitalWrite(M1P1,HIGH);
                   digitalWrite(M2P1,HIGH);
                   digitalWrite(M1P2,LOW);
                   digitalWrite(M2P2,LOW);}
}```

Please clearly explain your timing logic. It's not explained in your post, or documented inline in the program. It's rather unusual code.

Hi and thanks for a quick reply. The time logic as presented in the code is wrong so you can ignore it. It's just a most current attempt.

Let me explain differently what I want to achieve, in case I got you wrong:

  1. Robot is moving and getting readings from ultrasonic sensors.
  2. It's hearing an obstacle.
  3. It's stoping
  4. It's moving backwards for (let's say) 2 seconds
  5. it's turning left
  6. It's moving forward again etc.

That seems easy.

pseudocode

if obstacle is detected
{
  stop motor
  reverse motor
  start motor
  wait 2 seconds
  stop motor
}

Rather than delay a specific time, while backing up, keep track of the distance to obstacles using the ultrasonic sensors.

When the robot is "far enough away", take evasive action.

Do you have some more manageable, working code that we can see? It would be easier to modify.

It's a full code, I've got nothing else.

Not the best vaccum cleaner, isn't it? :smiley:

You really wrote all that without any testing?

Go "full speed ahead" for several seconds, without checking distance sensors, and run into things!

Could be rethought.

Are you running this on an UNO or Nano? If so, be warned that Pin 8 is not a PWM pin. That will be a problem if you ever want to use a speed that is not 0 or 255.

Thanks! This actually inspired me to add two infrared sensors that will be checking the corners of the robot so it won't fall. :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.