How to Implement the Bottom IR Sensors on the Sumo Bot

The Sumo bot is able to stay within the arena as long as the bottom Infrared Sensors (IR) are close to the ground. In this lesson you will learn how to calibrate and use more of the bottom IR sensors to stay in the field.

Begin the code by including the following

#include <SmartInventor.h>

int speedPercent = 50;
int BackTime = 0;
int TurnTime = 0;

Now add inside the void setup()

pinMode(11, INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
pinMode(18, INPUT);

These are all the IR sensors the smart inventor board has. If you count all the bottom IR sensor you will find out that there are only 7 even though here it is listed as 8, this is because 14 and 15 are tied to the same bottom IR sensor which is the center one.

Now we will initialize the motors.

  //This initiates the use of the motor
  SmartInventor.DCMotorUse();

  //Start of the motors to move forwards
  SmartInventor.DCMove(forward, speedPercent);

Now lets move on into the void loop()

  BackTime++;
  TurnTime++;
  
  if (BackTime >= 200) BackTime = 100;
  if (TurnTime >= 500) TurnTime = 100;

So now we will implement all the bottom left IR sensors. These are IR sensor 11,12 and 13. So lets create an If statement that will be activated whenever 11,12 or 13 IR sensors detect that the line is present which will make the sensor go LOW.

 //If the IR sensors on the left side detect anything
  if (digitalRead(11) == LOW||
      digitalRead(12) == LOW||
      digitalRead(13) == LOW)

This piece of code can be read as “if pin 11 is low, or if pin 12 is low, or if pin 13 is low “. The ||  means “or” it is a boolean operator along with &&  which means “and” they are used as logical operators.

So if any of the left IR sensors detect the black ring then do the following

 {
    //move backwards and buzz
    SmartInventor.DCMove(backward, speedPercent);
    SmartInventor.Buzz(1160, 12);
    delay(100 + BackTime);

    //move to the right
    SmartInventor.DCMove(right, speedPercent);
    delay(100 + TurnTime);

    //then move forward again
    SmartInventor.DCMove(forward, speedPercent);
  }

For the right sensor we will type.

else if (digitalRead(16) == LOW||
         digitalRead(17) == LOW||
         digitalRead(18) == LOW)

So the right IR sensors are pins 16,17, and 18 if these detect that black line then they will perform this action.

{
    //move backwards for some time
    SmartInventor.DCMove(backward, speedPercent);
    SmartInventor.Buzz(1160, 12);
    delay(100 + BackTime);

    //move to the left
    SmartInventor.DCMove(left, speedPercent);
    delay(100 + TurnTime);

    //then go forward
    SmartInventor.DCMove(forward, speedPercent);
    SmartInventor.Buzz(1900, 12);
  }

Once both the left and right IR sensors have been checked the only other case is if none of them are detecting the competition ring. So now type.

//If all IR sensors do not detect anyline on the floor
//drive forward
  if(
  digitalRead(11) == HIGH &&
  digitalRead(12) == HIGH &&
  digitalRead(13) == HIGH &&
  digitalRead(16) == HIGH &&
  digitalRead(17) == HIGH &&
  digitalRead(18) == HIGH 
     )
  {
    SmartInventor.DCMove(forward, 100);
    //drive at full speedPercent
  }

So this if statement checks every pin connected to all the sensors using the digital read command.The code reads “if pins 11 is HIGH and if pin 12 is HIGH and ….if pin 18 is HIGH” where the &&  stands for “and”. If all of them are HIGH, the robot is within the field so the Sumo bot should drive at full speed ahead.

Now that you are done programming the sensitivity can be adjusted for the IR sensors. The robot at times may be to sensitive and the sun will interfere with it and activate the IR sensor. Just adjust the white cross (potentiometer) with a screw driver in order to adjust the sensitivity until the robot detects the line properly.

 

Final Code

#include <SmartInventor.h>

int speedPercent = 50;
int  BackTime = 0;
int  TurnTime = 0;



void setup()
{
  
 
  
  //<-LEFT □ □ □ □ □ □ □ □ RIGHT->
  //   11,12,13,14,15,16,17,18
  //BOTTOM SENSOR USE
  pinMode(11, INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  pinMode(14,INPUT);
  pinMode(15,INPUT);
  pinMode(16,INPUT);
  pinMode(17,INPUT);
  pinMode(18, INPUT);

  //This initiates the use of the motor
  SmartInventor.DCMotorUse();

  //Start of the motors to move forwards
  SmartInventor.DCMove(forward, speedPercent);
}

void loop()
{

 
 
  BackTime++;
  TurnTime++;
  
  if (BackTime >= 200) BackTime = 100;
  if (TurnTime >= 500) TurnTime = 100;


  //If the IR sensors on the left side detect anything
  if (digitalRead(11) == LOW||
      digitalRead(12) == LOW||
      digitalRead(13) == LOW)
  {
    //move backwards and buzz
    SmartInventor.DCMove(backward, speedPercent);
    SmartInventor.Buzz(1160, 12);
    delay(100 + BackTime);

    //move to the right
    SmartInventor.DCMove(right, speedPercent);
    delay(100 + TurnTime);

    //then move forward again
    SmartInventor.DCMove(forward, speedPercent);
  }

  else if (digitalRead(16) == LOW||
           digitalRead(17) == LOW||
           digitalRead(18) == LOW)
  {
    //move backwards for some time
    SmartInventor.DCMove(backward, speedPercent);
    SmartInventor.Buzz(1160, 12);
    delay(100 + BackTime);

    //move to the left
    SmartInventor.DCMove(left, speedPercent);
    delay(100 + TurnTime);

    //then go forward
    SmartInventor.DCMove(forward, speedPercent);
    SmartInventor.Buzz(1900, 12);
  }

//If all IR sensors do not detect anyline on the floor
//drive forward
  if(
  digitalRead(11) == HIGH &&
  digitalRead(12) == HIGH &&
  digitalRead(13) == HIGH &&
  digitalRead(16) == HIGH &&
  digitalRead(17) == HIGH &&
  digitalRead(18) == HIGH 
     )
  {
    SmartInventor.DCMove(forward, 100);
    //drive at full speedPercent
  }

}