BrainBoard Tank Robot

13 May 2013

I have recently been getting interested in and learning more about programmable electronics boards, specifically the Arduino, with an eye to introducing my students to them. After some discussion with Tim Carr of Mindkits I purchased one of their BrainBoard Tank Robots and this is the result of trying to get it to move forwards and do things when sensors are triggered. The code is all available but it is worth noting that it was written by a non-programmer relying heavily on examples and experimentation.

The Brainboard is an Arduino Nano with sensors already wired in and ready to go. Perfect for people wanting to get going without working around breadboards and soldering and ideal in a school where we often want to get kids engaged in practical things without having to make things too permanent. It also means that relative beginners can focus on making the board do something quite quickly without too much fear of blowing delicate electronics.

The hardware

Programme #1: Turn when a barrier is detected 

Both the Brainboard and the Ultrasonic sensor comewith libraries to make life a little easier. From there I experimented with ways to make the ultrasonic sensor do its thing while the motors ran. The aim was to come up with a set of functions that students could look at and at least see what was happening before having a go themselves. I also had an urge to make it object oriented as much as possible and reinforce the use of functions etc. The code for move_range.ino.

#include <BrainBoard.h>
#include <Ultrasonic.h>
BrainBoard bb;
Ultrasonic ultrasonic(10,11); //Init an Ultrasonic object 10/11 are D10/D11

// Variables
int Distance;
int state = 0;

void setup() {
  Serial.begin(9600); // Set serial monitor baud rate
  while (bb.sw1Read() == LOW); //wait while button input is low (not active)

 // When the SW1 button is pressed run the Initialize Function
  while (bb.sw1Read() == HIGH) {
    initialize();
  }
;}

void loop() {
    rangeFind();
    if(Distance < 40) {
      motorRight();
    } else {
      bb.motorWrite(1,255);
      bb.motorWrite(2,255);
    }

} // Loop Function End

void initialize(){
    bb.motorWrite(1,255);
    bb.motorWrite(2,255);
    state = 1;
}

void rangeFind() {
  Distance=ultrasonic.Ranging(CM);//get the current result;
  delay(100);
  Serial.print("the distance is ");
  Serial.println(Distance);
  delay(100);
 }

void motorStop() {
  bb.motorWrite(1,0);
  bb.motorWrite(2,0);
 }

void motorRight() {
  bb.motorWrite(1,255);
  bb.motorWrite(2,-255);
  delay(800);
}

Programme #2: Test the barrier 3 times before turning 180-ish

This programme tests for a barrier and then moves the robot to the right three times looking for a gap in the barrier. If no gap is found (i.e. the distance never gets larger) the robot turns around and heads off in another direction.

File: sketch_repeat.ino

#include <BrainBoard.h>
#include <Ultrasonic.h>
 
BrainBoard bb;
Ultrasonic ultrasonic(10,11);//Init an Ultrasonic object 10/11 are D10/D11
 
// Variables
int Distance;
int Tries = 0;
 
// Set up
void setup() {
  Serial.begin(9600); // Set serial monitor baud rate
  while ( bb.sw1Read() == LOW);  //wait while button input is low (not active)
  
}
 
// Main programme
void loop() {
  Distance=ultrasonic.Ranging(CM);//get the current result;
  Serial.println(Distance);
  lightsOff();
  
  // Test the distance and either keep going or run barrierTest
  if(Distance < 40) {
    barrierTest();
  } else {
    motorFull(); 
  };
}
 
// The Supporting Functions
 
void barrierTest() {
  if(Tries <= 3) {
    bb.leftLeds(ON); // switch the left lights.
    Tries++; // incrememnt the Tries variable by 1
    wallTest(); // Function to turn and move the robot to the right
  } else {
   bb.rightLeds(ON);
   bb.beep(660,100); // Frustrated beep before turning 180 and leaving
   delay(500);
   Tries = 0; // Set Tries variable back to 0
   motorTurn180(); // Run the function to turn the robot 180 degrees
  }
}
 
void wallTest() {
  bb.motorWrite(1,255);
  bb.motorWrite(2,-255);
  delay(1050);
 
  bb.motorWrite(1,255);
  bb.motorWrite(2,255);
  delay(1000);
 
  bb.motorWrite(1,-255);
  bb.motorWrite(2,255);
  delay(1050);
}
 
 
void motorTurn180() {
  bb.motorWrite(1,255);
  bb.motorWrite(2,-255);
  delay(2200);
}
 
void motorFull() {
  bb.motorWrite(1,255);
  bb.motorWrite(2,255);
}
 
void motorStop() {
  bb.motorWrite(1,0);
  bb.motorWrite(2,0);
}
 
 void lightsOn() {
 bb.leftLeds(ON); 
}
 
void lightsOff() {
 bb.leftLeds(OFF);
 bb.rightLeds(OFF); 
}