1

Program the Mouse Bot

The video explains the program for the Mouse Bot. Don’t forget to go through the rest of the lesson!The Mouse Bot senses objects on its left and right sides as well as whatever’s in front of it using the bottom IR sensors on the Smart Inventor Board. When it detects something on one of the sides, it tries to avoid the object by making turns.

This is the pseudocode for the Mouse Bot’s program:

  • Check the top left, front, and right IR sensors:
  • If the left sensor detects something to the left, turn to the right.
  • Else, if the right sensor detects something to the right, turn left.
  • Else, if the center sensor detects something in front, turn left.
  • Else, if the sensors don’t detect anything, go forward.

Yes! You get to set your code up now! You need to include the Smart Inventor library at the very top of the code. If you want the motors to move, you need to set those up as well:

#include <SmartInventor.h> 

void setup() {
    SmartInventor.DCMotorUse();    
} 

void loop() {     
}

Include the values of each sensor in void loop() so they will be saved. This means you can use them later on in your code:

#include <SmartInventor.h> 

void setup() {
    SmartInventor.DCMotorUse();
}    

void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);
}

You can move on to the main section of the code now! You’re going to be using a lot of if statements to check if any of the top sensors have detected an object.

In this step, add a statement to check whether the left sensor has detected an object. If it has, program your motors so your Mouse Bot can move away from it:

#include <SmartInventor.h>

void setup() {
    SmartInventor.DCMotorUse();
    }
 
void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);

    if(leftSensor < 500)
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Turn right
        SmartInventor.DCMotor(M2, CCW, 50);
    }
}

Next, add a statement to check whether the right sensor has detected an object. If it has, program your motors so your Mouse Bot can move away from it.

But before you just copy and paste your code from your left sensor, you’re going to use an else if statement instead of just an if statement. This means that your statement for your right sensor will only be executed if the previous if statement about your left sensor is false:

#include <SmartInventor.h>

void setup() {
    SmartInventor.DCMotorUse();
} 
void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);

    if(leftSensor < 500)
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Turn right
        SmartInventor.DCMotor(M2, CCW, 50);
    }
    else if(rightSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
}

Next, add another else if statement for when the center sensor detects an object:

#include <SmartInventor.h>

void setup() {
    SmartInventor.DCMotorUse();
    }

void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);
    
    if(leftSensor < 500)
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Turn right
        SmartInventor.DCMotor(M2, CCW, 50);
    }
    else if(rightSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
    else if(centerSensor < 500)
    {
    SmartInventor.DCMotor(M1, CW, 50); // Turn left
    SmartInventor.DCMotor(M2, CW, 50);
    }
}

You’re almost done! Include an else statement so that the Mouse Bot can go forward if the sensors didn’t detect anything:

#include <SmartInventor.h>

void setup() {
    SmartInventor.DCMotorUse();
}
 
void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);

    if(leftSensor < 500)
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Turn right
        SmartInventor.DCMotor(M2, CCW, 50);
    }
    else if(rightSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
    else if(centerSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
    else
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Go forward
        SmartInventor.DCMotor(M2, CW, 50);
    }
}

This is what your final code should look like. You can always customize your Mouse Bot by changing its speed or delay time!

#include <SmartInventor.h>

void setup() {
    SmartInventor.DCMotorUse();
}

void loop() {
    int leftSensor = analogRead(19);
    int centerSensor = analogRead(20);
    int rightSensor = analogRead(21);

    if(leftSensor < 500)
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Turn right
        SmartInventor.DCMotor(M2, CCW, 50);
    }
    else if(rightSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
    else if(centerSensor < 500)
    {
        SmartInventor.DCMotor(M1, CW, 50); // Turn left
        SmartInventor.DCMotor(M2, CW, 50);
    }
    else
    {
        SmartInventor.DCMotor(M1, CCW, 50); // Go forward
        SmartInventor.DCMotor(M2, CW, 50);
    }
}