Time response problem in line following robot

Hi everyone,
Well I started learnig the arduino language 5 days ago , so to sum up all the things that I learned, I decided to create a Line following robot , that stops at the end of the line , which has many bends , interruptions and intersections, exactly 10 centimiters before an obstacle. The problem is that my robot doesn't respond very fast :frowning: The infrared detectors detect the black line very fast, but the robot takes some fractions of seconds to react. This makes it quit the line in every bend. I didn't find a solution until s.one told me that I have a problem in my code , and I should fix it .
Here is my code :

        if ( (!(digitalRead(CapLeft)) && !(digitalRead(CapRight))) || ( (digitalRead(CapLeft)) && (digitalRead(CapRight))  && (digitalRead(CapCenter))))
    {

      digitalWrite(MotRightForward , HIGH ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , HIGH ) ;
      digitalWrite(MotLeftBackward , LOW ) ;
      
      
      analogWrite(MotRightSpeed , 155);
      analogWrite(MotLeftSpeed , 120);
      
    }

    else if ( (digitalRead(CapLeft)) && !(digitalRead(CapRight)) )
    {
 
         
      digitalWrite(MotRightForward , HIGH ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , LOW ) ;
      digitalWrite(MotLeftBackward , LOW ) ;


      analogWrite(MotLeftSpeed , 0);
      
      
      analogWrite(MotRightSpeed , 100);
      
      
  
    }

    else if ( !(digitalRead(CapLeft)) && (digitalRead(CapRight)))
    {
  

      digitalWrite(MotRightForward , LOW ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , HIGH ) ;
      digitalWrite(MotLeftBackward , LOW ) ;
      
      
      analogWrite(MotRightSpeed , 0);
      analogWrite(MotLeftSpeed , 100);
  
    }

( in the forward position , the motors aren't given the same PMW value because they're not exactly identic and this speed setting provides me a straight line move)

Another thing is that , in order to make the robot stop before the obstacle I used the ultrasound detector HC-SRO4. When I test the detector before connecting it to the robot by printing the distance on the screen it gives me exact values. But when used with the robot , it stops the robot at different places, and generally 40 centimiters before the obstacle ( I don't have anything that can activate the detector at that point ) . I remind you that it has to stop 10 cm before the obstacle.
Here is my global code , including the ultrasound detecting instructions.

int MotRightSpeed=13;
int MotLeftSpeed=8;
int MotRightForward=12;
int MotRightBackward=11;
int MotLeftForward=10;
int MotLeftBackward=9;
int CapLeft = 22 ;
int CapCenter = 24;
int CapRight= 26;
int TrigUltraSound = 2 ;
int EchoUltraSound = 3 ;

float distance, duration ;


void setup() {


  
  for ( int out = 13 ; out>=8 ; out-- )
{
  pinMode ( out , OUTPUT);
}

pinMode ( 3, INPUT);
pinMode (2,OUTPUT);


  for ( int in = 22 ; in <= 26 ; in += 2 )
  {
    pinMode (in , INPUT);
  }




}







void loop() {





        if ( (!(digitalRead(CapLeft)) && !(digitalRead(CapRight))) || ( (digitalRead(CapLeft)) && (digitalRead(CapRight))  && (digitalRead(CapCenter))))
    {

      digitalWrite(MotRightForward , HIGH ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , HIGH ) ;
      digitalWrite(MotLeftBackward , LOW ) ;
      
      
      analogWrite(MotRightSpeed , 155);
      analogWrite(MotLeftSpeed , 120);
      
    }

    else if ( (digitalRead(CapLeft)) && !(digitalRead(CapRight)) )
    {
 
         
      digitalWrite(MotRightForward , HIGH ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , LOW ) ;
      digitalWrite(MotLeftBackward , LOW ) ;


      analogWrite(MotLeftSpeed , 0);
      
      
      analogWrite(MotRightSpeed , 100);
      
      
  
    }

    else if ( !(digitalRead(CapLeft)) && (digitalRead(CapRight)))
    {
  

      digitalWrite(MotRightForward , LOW ) ;
      digitalWrite(MotRightBackward , LOW ) ;
      
      digitalWrite(MotLeftForward , HIGH ) ;
      digitalWrite(MotLeftBackward , LOW ) ;
      
      
      analogWrite(MotRightSpeed , 0);
      analogWrite(MotLeftSpeed , 100);
  
    }
  


   

    

        digitalWrite (TrigUltraSound,HIGH);
        
        delayMicroseconds (5);
        digitalWrite (TrigUltraSound,LOW);
        
        duration =pulseIn(EchoUltraSound,HIGH);
        
        distance = duration /58,77;
        







    
        if ( (distance <= 10  && distance >= 8)  )
        {
        
          digitalWrite(MotRightForward , LOW ) ;
          digitalWrite(MotRightBackward , LOW ) ;
          
          digitalWrite(MotLeftForward , LOW ) ;
          digitalWrite(MotLeftBackward , LOW ) ;
    
    
          analogWrite(MotLeftSpeed , 0);
          
          
          analogWrite(MotRightSpeed , 0);
          
          
             
          delay(6000);
        }







  }

my questions are :
1/ What are the problems in my code raising the response time?
2/What are all the possible tips and solutions that you can give me in order to reduce response time in my future arduino projects as far as possible?
3/ Is there a relation between the robot response time and these two lines
{
Le croquis utilise 3 580 octets (1%) de l'espace de stockage de programmes. Le maximum est de 253 952 octets.
Les variables globales utilisent 193 octets (2%) de mémoire dynamique, ce qui laisse 7 999 octets pour les variables locales. Le maximum est de 8 192 octets.
}
that appear down in the screen after compiling the program?
4/ What should I do to make the robot stop where I want ? ( what's the problem with the ultrasound detector part in my code )

Please help me , I need a clear and a complete question by question answer as soon as possible , and thank's a lot :smiley:

First auto format your code then repost it!

NEVER USE delay!

Mark

Oh sorry :3

int MotRightSpeed = 13;
int MotLeftSpeed = 8;
int MotRightForward = 12;
int MotRightBackward = 11;
int MotLeftForward = 10;
int MotLeftBackward = 9;
int CapLeft = 22 ;
int CapCenter = 24;
int CapRight = 26;
int TrigUltraSound = 2 ;
int EchoUltraSound = 3 ;

float distance, duration ;


void setup() {



  for ( int out = 13 ; out >= 8 ; out-- )
  {
    pinMode ( out , OUTPUT);
  }

  pinMode ( 3, INPUT);
  pinMode (2, OUTPUT);


  for ( int in = 22 ; in <= 26 ; in += 2 )
  {
    pinMode (in , INPUT);
  }




}







void loop() {





  if ( (!(digitalRead(CapLeft)) && !(digitalRead(CapRight))) || ( (digitalRead(CapLeft)) && (digitalRead(CapRight))  && (digitalRead(CapCenter))))
  {

    digitalWrite(MotRightForward , HIGH ) ;
    digitalWrite(MotRightBackward , LOW ) ;

    digitalWrite(MotLeftForward , HIGH ) ;
    digitalWrite(MotLeftBackward , LOW ) ;


    analogWrite(MotRightSpeed , 155);
    analogWrite(MotLeftSpeed , 120);

  }

  else if ( (digitalRead(CapLeft)) && !(digitalRead(CapRight)) )
  {


    digitalWrite(MotRightForward , HIGH ) ;
    digitalWrite(MotRightBackward , LOW ) ;

    digitalWrite(MotLeftForward , LOW ) ;
    digitalWrite(MotLeftBackward , LOW ) ;


    analogWrite(MotLeftSpeed , 0);


    analogWrite(MotRightSpeed , 100);



  }

  else if ( !(digitalRead(CapLeft)) && (digitalRead(CapRight)))
  {


    digitalWrite(MotRightForward , LOW ) ;
    digitalWrite(MotRightBackward , LOW ) ;

    digitalWrite(MotLeftForward , HIGH ) ;
    digitalWrite(MotLeftBackward , LOW ) ;


    analogWrite(MotRightSpeed , 0);
    analogWrite(MotLeftSpeed , 100);

  }







  digitalWrite (TrigUltraSound, HIGH);

  delayMicroseconds (5);
  digitalWrite (TrigUltraSound, LOW);

  duration = pulseIn(EchoUltraSound, HIGH);

  distance = duration / 58, 77;









  if ( (distance <= 10  && distance >= 8)  )
  {

    digitalWrite(MotRightForward , LOW ) ;
    digitalWrite(MotRightBackward , LOW ) ;

    digitalWrite(MotLeftForward , LOW ) ;
    digitalWrite(MotLeftBackward , LOW ) ;


    analogWrite(MotLeftSpeed , 0);


    analogWrite(MotRightSpeed , 0);



    delay(6000);
  }







}

here it is. Well speaking about the delay, as you see , it's just for stopping the robot, I just used it if the robot detects an obstale from 10 cm. I used another delay which is delayMicroseconds but I don't think that it will cause a problem because it's too short and it's necessary for every distance measure

If there's no object in view, how long does pulseIn() wait?

It doesn't wait , it just gives the value calculated by the arduino from the duration detected by the ultrasound detector. The distances measured by this detector are between 5 cm and 4m. Out of these values , the ultrasound detector returns a very large value , that won't effect the program in any case

It doesn't wait

It most certainly does. THAT is where your delay is coming from.

Try using the NewPing library, which uses interrupts. Much faster, and more accurate.

Thank you a lot. Can anyone answer the other questions please ? :smiley:

Can anyone answer the other questions please ?

Getting rid of blocking code answers 4 of your 5 questions. Stopping where you want MAY be solved, too. You will have to try the NewPing library to see.