SumoBot using HC-SR04, TCRT5000, L298n

hey guys i have been working on a sumobot project using 2xdc motors & 2xTCRT5000 & L298n motor driver &arduino uno, but when i run the code i find some problems,
shuch the motors move about half revolution up and down, The ultra sonic sencor dosen’t detect the objects as it should.
the code i use is a modified one from an online blog.
so please i need help with this code.!
This is a copy of The code i uploaded to my arduino

#include <Ultrasonic.h>
Ultrasonic ultrasonic(4,3);
const int IN1=5;
const int IN2=6;
const int IN3=9;
const int IN4=10;
#define IR_sensor_front A0 
#define IR_sensor_back A1 
int distance ;


void setup() 
{
Serial.begin(9600);
delay (5000); 
}

void ROTATE (){
void loop();

  
 int IR_front = analogRead(IR_sensor_front);
 int IR_back = analogRead(IR_sensor_back);
 distance = ultrasonic.Ranging(CM) ;
   analogWrite(IN1,200);
  analogWrite(IN2,0);
  analogWrite(IN3,200);
  analogWrite(IN4,0);

void stop(); 
 if (distance < 20){
    
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
    }
    void FORWARD ();
    while (distance < 20 ) {
  analogWrite(IN1,255);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,255);
    distance = ultrasonic.Ranging(CM);
    IR_front = analogRead(IR_sensor_front);
    IR_back = analogRead(IR_sensor_back);
    if ( IR_front > 650 || IR_back > 650 ) { break;}
    delay(10); }
  }

int IR_front = analogRead(IR_sensor_front);
int IR_back = analogRead(IR_sensor_back);
void loop(){
 if (IR_front <= 650 ) ; 
   analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
   delay (50);
  analogWrite(IN1,0);
  analogWrite(IN2,255);
  analogWrite(IN3,255);
  analogWrite(IN4,0);
   delay (500);

     if (IR_back <= 650 ); 
  
     analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
   delay (50);
     analogWrite(IN1,255);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,255);
   delay (500);
   }

and this is the code i found and modified

#include "Ultrasonic.h"
Ultrasonic ultrasonic(4,3);//المداخل الموصولة مع الحساس
const int IN1=5;
const int IN2=6;
const int IN3=9;
const int IN4=10;
#define IR_sensor_front A0 // الحساس الامامي
#define IR_sensor_back A1 // الحساس الخلفي
int distance ;

void setup() 
{
Serial.begin(9600);
delay (5000); // حسب قوانين السومو 
}


void loop()
{
  
 int IR_front = analogRead(IR_sensor_front);
 int IR_back = analogRead(IR_sensor_back);
 distance = ultrasonic.Ranging(CM) ;
 ROTATE(200); // يبدأ الروبوت بالدوران

 if (distance < 20){
    Stop();
    while (distance < 20 ) {
    FORWARD(255); 
    distance = ultrasonic.Ranging(CM);
    IR_front = analogRead(IR_sensor_front);
    IR_back = analogRead(IR_sensor_back);
    if ( IR_front > 650 || IR_back > 650 ) { break;}
    delay(10); }
  }
  
 if (IR_front < 650 )  // اذا كانت القراءة اقل من 650 يعني اللون ابيض
   {
   Stop();
   delay (50);
   BACKWARD(255);
   delay (500);
   } 
   
 if (IR_back < 650 )  //
   {
   Stop();
   delay (50);
   FORWARD(255);
   delay (500);
   }

/* ----------- debugging ----------------
  Serial.print(ultrasonic.Ranging(CM));
  Serial.println("cm");
  Serial.println("IR front :");
  Serial.println(IR_front); 
  Serial.println("IR back :");
  Serial.println(IR_back);  
*/ --------------------------------------  

}



//--------------------------------------------
void FORWARD (int Speed){
  //When we want to let Motor To move forward,
  // just void this part on the loop section .
  analogWrite(IN1,Speed);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,Speed);
}


//--------------------------------------------
void BACKWARD (int Speed){
  //When we want to let Motor To move forward,
  // just void this part on the loop section .
  analogWrite(IN1,0);
  analogWrite(IN2,Speed);
  analogWrite(IN3,Speed);
  analogWrite(IN4,0);
}

//--------------------------------------------
void ROTATE (int Speed){
  //When we want to let Motor To Rotate ,
  // just void this part on the loop section .
  analogWrite(IN1,Speed);
  analogWrite(IN2,0);
  analogWrite(IN3,Speed);
  analogWrite(IN4,0);
}


//--------------------------------------------
void Stop(){
  //When we want to  Motor To stop ,
  // just void this part on the loop section .
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
}

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Grumpy_Mike:
Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Thank you for informing me about this, i hope it is better now

Thanks for putting the code in a readable form.

The problem is you are trying to do too much at once. When you write code you have to build it up bit by bit.

  1. Does the ultrasonic sensor work as expected. Write code and test, use serial print to see what is happening.
  2. Does the IR sensors work as expected.
  3. Will the motor turn in one direction until it sees an obstacle? Write code and test.
  4. Will the motor reverse when it sees an obstacle? Write code and test.

Build it up from a solid base.

What is this?:-

void ROTATE (){
void loop();

It is a nonsense. I am surprised it would even compile. You do not call the loop function. And you do not need the void command to call a function only to define one. Select all the code in the IDE and then go to the tools menu and select "Auto format", that will make it easy to follow. You will see that your function ROTATE ( don't use all capitals for functions please ) never gets called at all. You have not defined the function stop, nor the function FORWARD ( don't use all capitals for functions please ).

The loop function only gets a one time fixed value for the IR sensors, it should read these sensors every time round the loop.

The whole code is a mess, you can't tell what is a definition and what is a function call.

You need to have a clear idea of what you want this code to do before you start writing it.

Grumpy_Mike:
Thanks for putting the code in a readable form.

The problem is you are trying to do too much at once. When you write code you have to build it up bit by bit.

  1. Does the ultrasonic sensor work as expected. Write code and test, use serial print to see what is happening.
  2. Does the IR sensors work as expected.
  3. Will the motor turn in one direction until it sees an obstacle? Write code and test.
  4. Will the motor reverse when it sees an obstacle? Write code and test.

Build it up from a solid base.

What is this?:-

void ROTATE (){

void loop();



It is a nonsense. I am surprised it would even compile. You do not call the loop function. And you do not need the void command to call a function only to define one. Select all the code in the IDE and then go to the tools menu and select "Auto format", that will make it easy to follow. You will see that your function ROTATE ( don't use all capitals for functions please ) never gets called at all. You have not defined the function stop, nor the function FORWARD ( don't use all capitals for functions please ).

The loop function only gets a one time fixed value for the IR sensors, it should read these sensors every time round the loop.

The whole code is a mess, you can't tell what is a definition and what is a function call.

You need to have a clear idea of what you want this code to do before you start writing it.

Thanks for your help, i admit that the code is a mess cause this the first time i write a code this big and i had to much pressure so i had to do it fast