Delay/Millis use

I've written a code for a robot that ''will chase rats''.
The vehicle is moving using 2 DC motors and steers using a servo.
Steering is achieved through specific pulse widths for each direction (1000 left,1500 straight,2000 right, etc.) For the steering to be maintained a 20ms period is needed.

The device is using a contact switch. When the switch is pressed the vehicle turns left or right and reverses for 500ms for example.

My question is the following:

Is it possible to move backward while steering (20ms period pulses) using a delay command?
I had an argument about this and I was feeling sure that a 500ms delay cant be integrated into a program that has a repetitive action every 20ms.

*In the uploaded code I am using a millis counter not a delay

void loop() {
unsigned int numServo; // pulse control variables
numServo=1500; // initialised to 1500 (straight ahead)
timenow=millis();

// Set motors to maximum speed
analogWrite(LeftSpeed, 255);
analogWrite(RightSpeed, 255);

//Robot moves forward
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);

//Reading the value of the infra-red sensors
digitalRead(DRight);
digitalRead(DLeft);

//Checking if the switch is pressed
digitalRead(Switch);

//When the rodent is detected straight ahead
if (DRight == HIGH && DLeft == HIGH)
{

digitalWrite (Buzzer, HIGH);
numServo=1500;
}

//When the rodent is detected right
if (DRight == HIGH && DLeft == LOW)
{

digitalWrite (Buzzer, HIGH);

//Turn the robot 35degrees to the right
numServo=1888;
}

//When the rodent is detected left
if (DRight == LOW && DLeft == HIGH)
{
digitalWrite (Buzzer, HIGH);
//Turn the robot 35degrees to the left
numServo=1112;
}

//When no rodent is detected
if (DRight == LOW && DLeft == LOW)
{
//Stop this annoying horn!
digitalWrite (Buzzer, LOW);
}

//If the switch hook is pressed again the wall, We hitted a wall!
if (Switch == HIGH)
{
timecounter=millis();

//Setting a counter to allow us to count 0.5s while simiultanesly moving backwards and sending a pwm to the servo to turn right.
//Using a delay function wouldn't allow us to execute both tasks at the same time.
//As soon as the reverse turn is completed our vehicle will go straight forward at full speed (top code snippet*)

while( (timenow-timecounter) <500)
{

// Let's move back right!

//Motors are moving the wheels backwards
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);

// Set motors to maximum speed
analogWrite(LeftSpeed, 255);
analogWrite(RightSpeed, 255);

// Steering wheel turning the wheels to the right
numServo=1888;

if(( timenow-timelast ) >= 20) // need pulses to be every 20 +/- 5 ms
{
timelast=millis();// update last pulse time
// then do servo pulse:
digitalWrite(ServoPin,HIGH);
delayMicroseconds(numServo);
digitalWrite(ServoPin,LOW);
}

}

}

if(( timenow-timelast ) >= 20) // need pulses to be every 20 +/- 5 ms
{
timelast=millis();// update last pulse time
// then do servo pulse:
digitalWrite(ServoPin,HIGH);
delayMicroseconds(numServo);
digitalWrite(ServoPin,LOW);
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

And don't post code snippets. Post the complete code that can be compiled.

Why don't you use a library to control your servo? Than you don't have to bother with the pulse creation in loop, what makes things much easier.
servo.h is included in the IDE.

You are right if you are referring to a delay() because whilst the 500mS delay() is happening then a 20mS delay() can't be happening

However, using millis() for timing, you could have a 500mS period and a 20mS period happening at the same time with no interference between them

That's a strange design. Normally steering is achieved by varying the speed of the 2 DC motors and no servo is needed. The third "wheel" is normally just for stability and is not driven. Often it is just a captive ball bearing.

while ( (timenow - timecounter) < 500) {

You never exit this loop. Instead of calling timenow once in the beginning, and then having to track it throughout all the conditional branches, just call it as needed in-line.

while ( (millis() - timecounter) < 500) {

This is actually "blocking" code as it will not do anything else until 500 milliseconds. It is better to write things inversed, so you do what you want when the 500 is up...

if (millis() - intervalStart >= 500) {
  //do what has to be done.
}

Hi hamza,

If somebody would like to copy your code she/he has a hard job of exactly marking all characters
needed. The users will jump over to another users post where copying is done with a single mouse-click.

There is a much more comfortable function for doing this.

code-sections

This is an explanation how to use millis():

Be the change you want to see in the world
best regards Stefan

Not that strange.

It's how cars work. ;-)

a7

It is fairly unusual to use 2 DC motors and servo steering.