MakeBlock mBot Ranger

An mBot Ranger apparently uses an Arduino Mega 2560. I got it to help with my Arduino learning and dragged it out of it’s cave whilst there is a lull in the Crane progress. I would like to use it’s ultrasonic sensor to make it go forward then turn right every time it senses a blockage. I found this sketch

/**
 * @file         mBot Ranger Autonomous Mode.ino
 * @author       IntoRobotics.com
 * @version      V1.1
 * @date         2016/05/13
 * @description  this file is sample code for the mBot Ranger kit
 */
 
#include "MeAuriga.h"
#include <SoftwareSerial.h>

MeUltrasonicSensor ultraSensor(PORT_7); // Ultrasonic module

int distanceLimit=50;    // Maximum distance we want to ping for (in centimeters).
int maxPwm=255;
int halfPwm=200;
int turnPwm=150;
int d=500;
int frontDistance;


//Motor Left
const int pwmMotor1 = 11;
const int inMotor1_1 = 49;
const int inMotor1_2 = 48;

//Motor Right
const int pwmMotor2 = 10;
const int inMotor2_1 = 47;
const int inMotor2_2 = 46;

void setup()
{
  Serial.begin(9600);
 
  pinMode(pwmMotor1,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor1_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor1_2,OUTPUT);
  pinMode(pwmMotor2,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor2_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor2_2,OUTPUT);
 
}

void loop()
{
   GetFrontDistance();  //Get the ultrasonic distance in cm
 
   if(frontDistance>=distanceLimit){
      FullSpeedMode();  //The robot is in full speed mode
    }

   else {  
      ReduceSpeed();   //Reduce the speed of the robot
      Stop();          //Stop the robot
      do {  
          TurnRight();
          delay(200);  // Wait for sensor to stabilize
          GetFrontDistance();  //Get the ultrasonic distance in cm  
          } while(frontDistance<distanceLimit);
      }
}

 int GetFrontDistance(){
   frontDistance=ultraSensor.distanceCm();
   Serial.print("Front distance (in cm) is: "); //For debugging
   Serial.println(frontDistance);
   return frontDistance;
   }

 void FullSpeedMode(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,maxPwm);
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,maxPwm);
    Serial.println("Full speed mode"); //For debugging
   }

  void ReduceSpeed(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,halfPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,halfPwm);//Set speed via PWM
    Serial.println("Reduce the speed"); //For debugging
    delay(d);
    }

  void Stop(){
    analogWrite(pwmMotor1, 0);
    analogWrite(pwmMotor2, 0);
    Serial.println("Stop");
    delay(d);
   }
 
   void TurnRight(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,turnPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, HIGH);
    digitalWrite(inMotor2_2, LOW);
    analogWrite(pwmMotor2,turnPwm);//Set speed via PWM
    Serial.println("Turn the robot right"); //For debugging

but can’t get the bot to do more than go flat out backwards. Is there somewhere I can go that will let me paste the sketch then single the bits out and explain what they mean?

2 Likes

Hi Geoffrey,

The first step I’d say would be to ensure your ultrasonic sensor is behaving itself. Maybe something like this might do the trick? (singling out the ultrasonic reading lines)

#include "MeAuriga.h"
#include <SoftwareSerial.h>

MeUltrasonicSensor ultraSensor(PORT_7)

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  Serial.println(ultraSensor.distanceCm());
}
//This code may well not work, I can't test it with your hardware, and I've basically just lopped off the bits I think it doesn't need, so I may have messed up

If you open your serial plotter in your IDE, you should see a graph that rougly corresponds to what your sensor is reading, if it doesn’t then we need to look deeper into that.

Keen to see this back on track (or forward on track?)
-James

2 Likes

Thanks James. I contacted MakerBot and they helped me through the issue. Mostly me not understanding things but all is good now. The sketch I was using may or may not have been good but once they showed me how to use the MakeBlock block code it proved that the Ranger was OK. Also, they clarified how to make code out of the blocks and then read it in Arduino (C++) All I need now is the time to make it all happen.

3 Likes

Ooops, wrong thread.

1 Like