Simple SUMO BOT program

I'm building a sumobot that with an IR proximity sensor and line tracing sensor.
This is my the first program to test the proximity sensor. I'm having difficulties with it, whenever the sensor detects any object, it doesn't ATTACKS but finishes hunt. I tried doing the turnLeft only without the delay code and it instantly ATTACK whenever it detects objects. I need a code to make the robot turn/stop for a certain time without using delay syntax.

#include <Servo.h> 
 
/* create servo object to control a servo
a maximum of eight servo objects can be created */
Servo servoR;
Servo servoL;

int pos = 90, // variable to store the servo position 
    IRpin = 2, //digital pin 2
    IR;        

void setup() 
{ 
  pinMode(IRpin, INPUT);
  servoR.attach(9);  // attaches the servo on pin 9 to the servo object
  servoL.attach(10); //attaches the servo on pin 10 to the servo object
} 

void loop()
{ 
   if (int sensor = digitalRead(IRpin))
    {
      //HUNTS OPPONENTS
      sensor == LOW;
      turnRight();
      turnLeft();
      stopBot();
      turnLeft();
      turnRight();
    } else {
      //ATTACKS
      forward();
    }
}

void forward()
{
  servoR.write(80);
  servoL.write(100);
}

void turnRight()
{
  servoR.write(95);
  servoL.write(95);
  delay (2000);     //turns right for 2 secs
}

void turnLeft()
{
  servoR.write(85);
  servoL.write(85);
  delay(2000);    //turns left for 2 secs
}

void stopBot()
{
  servoR.write(90);
  servoL.write(90);
  delay(2000);   //stops the robot for 2 seconds
}
   if (int sensor = digitalRead(IRpin))

Why is the definition and valuation of the variable embedded in an if statement? What is your understanding of what the if test is testing?

      sensor == LOW;

Generally, when one performs an equality test, one cares about the result.

^I'm sorry about that. I am very new to this. Tried programming for arduino because of a project at school. Can you suggest me any alternate code for what I am trying to aim for?

Generally, when one performs an equality test, one cares about the result.

can you explain what did I do wrong? Sorry about that.

Can you suggest me any alternate code for what I am trying to aim for?

   int sensor = digitalRead(IRpin);
   if (sensor == HIGH)

and

      sensor = LOW;

I added a line tracer sensor, is my program correct? I'm still new to this, please bear with me.

#include <Servo.h> 
 
/* create servo object to control a servo
a maximum of eight servo objects can be created */
Servo servoR;
Servo servoL;

int pos = 90, // variable to store the servo position 
    IRsensor = digitalRead(2), //proximity sensor digital pin 2
    LTsensor = digitalRead(4); //line tracer digital pin 4
         
void setup() 
{ 
  pinMode(IRsensor, INPUT);
  pinMode(LTsensor, INPUT);
  servoR.attach(9);  // attaches the servo on pin 9 to the servo object
  servoL.attach(10); //attaches the servo on pin 10 to the servo object
} 

void loop()
{
  if (LTsensor = HIGH)
  {
    backward();
    delay (5000);
  } else {
    delay (100);
    proximity();
  }
}

void proximity()
{ 
   if (IRsensor == LOW)
    {
      //HUNTS OPPONENTS
      forward();
      delay(4000);
      turnRight();
      turnLeft();
      stopBot();
      turnLeft();
      turnRight();
    } else {
      //ATTACKS
      delay (100);
      forward();
    }
}

void forward()
{
  servoR.write(80);
  servoL.write(100);
}

void backward()
{
  servoR.write(100);
  servoL.write(80);
}

void turnRight()
{
  servoR.write(95);
  servoL.write(95);
  delay (2000);     //turns right for 2 secs
}

void turnLeft()
{
  servoR.write(85);
  servoL.write(85);
  delay(2000);    //turns left for 2 secs
}

void stopBot()
{
  servoR.write(90);
  servoL.write(90);
  delay(2000);   //stops the robot for 2 seconds
}

is my program correct? I

What does the compiler say?

I don't think you really want an assignment in your "if" like that - the compiler won't mind, but you will.

This is one of those times where it does compile, even though it's wrong.

int pos = 90, // variable to store the servo position 
    IRsensor = digitalRead(2), //proximity sensor digital pin 2
    LTsensor = digitalRead(4); //line tracer digital pin 4

Isn't a fighting bot that is blind for up to five seconds going to be vulnerable?

Isn't re-reading the sensors once in a while a good idea?

Should I insert the int IRsensor = digitalRead(2), LTsensor = digitalRead(4); into the main loop?

I don't think you really want an assignment in your "if" like that - the compiler won't mind, but you will.

How can I improve the program?

Isn't a fighting bot that is blind for up to five seconds going to be vulnerable?

Can you point to me where in the program is that? :slight_smile:

Isn't re-reading the sensors once in a while a good idea?

Teach me how, please.

As the other guys indicated, use of delay(2000) is not very efficient. The
cpu does NOTHING "useful" for 2 seconds, just wastes cycles, so it will not
read your IR inputs or do anything else during that time.

Do a search on "blink without delay" threads to see how to not lock up the
cpu while you're waiting for something to occur.

So if I remove delay, how can I rotate the bot clockwise then counter clockwise for 2 seconds each? Sorry for so many questions but I can't figure out blink without delay and how can I apply it with MY delay. Sorry.

I forgot to say that this is a modified servo, the one where the locking mechanism is manually removed so that it can rotate continuously.

So if I remove delay, how can I rotate the bot clockwise then counter clockwise for 2 seconds each?

You really, really need to understand the blink without delay example.
It's only got about four significant lines of code.

Think about what it is doing, transitioning the state of the LED, and going off and potentially doing something else.
Well, transitioning a LED is a bit like starting a motor.

Is this code compatible with Energia

Gerbster:
Is this code compatible with Energia

Energia the Soviet rocket? Or the Irish gas and electricity supplier? I shouldn't think so.

If you meant something else a little more information might help.

Steve

Can you teach me ? How to coding or programming the One IR line sensor and Three IR Obstacle Avoidance sensor

and Three IR Obstacle Avoidance sensor

Can you really sense an Obstacle Avoidance? Just what the heck IS an Obstacle Avoidance?

The sensors detect obstacles, and the information is used to avoid obstacles.

There are something like 723541864864864864 examples on the internet of using the sensors to avoid obstacles. Surely one of them could provide you with a clue or two.