get rid of all delays

Hi.
I've been doing an obstacle avoidance robot.
It works fine.

It stops when obstacle is detected. turns. and goes again.

here's the code :

#include <NewPing.h>
#include <Servo.h>
 
Servo servoG;
Servo servoD;
Servo servoPan;

#define TRIGGER_PIN  12
#define ECHO_PIN  13
#define MAX_DISTANCE 400

const int dangerDist = 10;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int B = 0;
int A = 180;

 
void setup () {
Serial.begin (9600) ;
servoG.attach (9) ;
servoD.attach (10) ;
servoPan.attach (11);
servoPan.write (90);
delay (1000);


}
 
void loop () {

servoPan.write (90);
delay (500);
int uS = sonar.ping();
int dist = uS / US_ROUNDTRIP_CM ;
  
if (dist > dangerDist) {
  go ();
}
  else {
endgo ();
delay (500);
    
servoPan.write (140) ;
delay (500);
int uS140 = sonar.ping();
int dist140 = uS / US_ROUNDTRIP_CM ;


servoPan.write (40) ;
delay (500);
int uS40 = sonar.ping();
int dist40 = uS / US_ROUNDTRIP_CM ;

servoPan.write (120) ;
delay (500);
int uS120 = sonar.ping();
int dist120 = uS / US_ROUNDTRIP_CM ;

servoPan.write (60) ;
delay (500);
int uS60 = sonar.ping();
int dist60 = uS / US_ROUNDTRIP_CM ;
    
if ( uS60 + uS40 > uS120 + uS140 ) {
  endgo();
    backwards ();
    endgo();
    left ();
   }
else {
  endgo();
   backwards ();
   endgo();
  right();
  }
 
 }
}

 
void go () {
servoG.write(A) ;
servoD.write (B) ;
}
void endgo () {
servoG.write (90) ;
servoD.write (90) ;
delay (500);
}
void backwards () {
servoG.write (B) ;
servoD.write (A) ;
delay (800);
}
void left () {
servoG.write (B) ;
servoD.write (B) ;
delay (500);
}
void right () {
servoG.write (A) ;
servoD.write (A) ;
delay (500);
}

I would like now to get rid of all delays so that I can do multiple tasks at the same time. and also - for my personnal understanding on how to to use millis () correctly instead of delays.

I tried this code which I thought would work. But I have absolutely not the same result.
The robot goes forward. hi head moves but it won't stop or turn.

What did I do wrong ? and how can I use just ONE easy : if (currenttimewheels - previoustimewheels > ...).

here's my new code :

#include <NewPing.h>
#include <Servo.h>
 
Servo servoG;
Servo servoD;
Servo servoPan;
int B = 0;
int A = 180;

#define TRIGGER_PIN  12
#define ECHO_PIN  13
#define MAX_DISTANCE 400
const int dangerDist = 10;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

unsigned long currrenttimewheels = 0;
unsigned long previoustimewheels = 0; 
long waittimewheels = 5000; 

 
void setup () {
Serial.begin (9600) ;
servoG.attach (9) ;
servoD.attach (10) ;
servoPan.attach (11);
servoPan.write (90);
delay (1000);


}
 
void loop () {

unsigned long currenttimewheels = millis ();
  
servoPan.write (90);
delay (500);
int uS = sonar.ping();
int dist = uS / US_ROUNDTRIP_CM ;
  
if (dist > dangerDist) {
  go ();
}
  else {
   
endgo ();

    
servoPan.write (140) ;
delay (500);
int uS140 = sonar.ping();
int dist140 = uS / US_ROUNDTRIP_CM ;


servoPan.write (40) ;
delay (500);
int uS40 = sonar.ping();
int dist40 = uS / US_ROUNDTRIP_CM ;

servoPan.write (120) ;
delay (500);
int uS120 = sonar.ping();
int dist120 = uS / US_ROUNDTRIP_CM ;

servoPan.write (60) ;
delay (500);
int uS60 = sonar.ping();
int dist60 = uS / US_ROUNDTRIP_CM ;
    
if ( uS60 + uS40 > uS120 + uS140 ) {
  endgo();
    backwards ();
    endgo();
    left ();
   }
else {
  endgo();
   backwards ();
   endgo();
  right();
  }
   previoustimewheels = currrenttimewheels;
 }
  
}

 
void go () {
  
servoG.write(A) ;
servoD.write (B) ;

}

void endgo () {
if ( currrenttimewheels - previoustimewheels > waittimewheels) {
servoG.write (90) ;
servoD.write (90) ;

}
 }

void backwards () {
if ( currrenttimewheels - previoustimewheels > waittimewheels) {
servoG.write (B) ;
servoD.write (A) ;

}
 }

void left () {
if ( currrenttimewheels - previoustimewheels > waittimewheels) {
servoG.write (B) ;
servoD.write (B) ;

}
 }

void right () {
if ( currrenttimewheels - previoustimewheels > waittimewheels) {
servoG.write (A) ;
servoD.write (A) ;

}
 }

and secondly , how could i have his head pan while the robot is still moving. as if I would like him to never stop turning his head . even if the robot is stopping. moving forward/backwards/left/right. ?

thanks a lot for your help

TIP:

the IDE has the CTRL-T implemented to re-indent code to make it better readable.

if ( uS60 + uS40 > uS120 + uS140 ) {

are dangerous without enough ()

if ( (uS60 + uS40) > (uS120 + uS140) ) {

is more robust

TO get rid of the delay's you must introduce a state variable to reflect the current activity
and use the blink without delay technique. to change it at the right moment.

Hi ok thanks for the tip.

As you can see in my seconde code. I tried to implement the blink without delay code.
But I must have done something wrong. it doesn"t work.
the robot only goes forward. never stops. and turns it's head... that all.
It won't stop. won't go backwards or turn...

The demo several things at a time illustrates how to use millis() to manage timing.

...R