Now that the starfighter is built (mostly) and work has slowed down ever so little, I have a little time to get back to learning with my robot. X1 has had a severe facelift. I don’t want to relabel it X2, because the guts and drive are unchaged. So, let’s call it X1.1.I had a visit to The Robot Group at TechShop in Round Rock and, naturally, I took X1 with me. Passing through the main shop I took a moment to talk to someone building a 3D printer. In the conversation he called X1 a BOE Bot. I had no idea what that was at the time. After a visit to my local Fry’s I discovered BOE stands for “Board of Education.” This is essentially a bot with a breadboard specifically for learning robotics. So, in short, X1 is a BOE Bot. However, X1 was difficult to work on with the three platters. So, I redesigned him using some aluminum sheet I had sitting around. The new and improved X1.1 is much easier to work on and gives me a larger platform to build on.

So, with that being done, I re-wired X1.1 with the contact switches as sensors and finally got the program straightened out. Well, mostly. There is still some odd behaviour in there, but I’ll get it isolated and working properly as I have more time. At the moment it simply behave like this:

  1. X1.1 drives forward by default
  2. When the front switch is depressed by bumping into an obstacle, it backs up and turns left or right depnding on the last direction it turned.
  3. When either the left or right switches are depressed it will back in the opposite direction.

This behavior is supposed to help it get around obstacles. Unfortunately it’s not quite working right. Right now, when it bumps into something with the front switch it backs up, does a little wiggle, and drives right back into the obstacle. It also will pause seemingly randomly and occasionally back up and/or turn for no apparant reason. So, obviously there’s a little work to be done with the code.

But in the meantime, here’s the sketch and schematic:

// First real robot sketch for X1
// V1 uses three switches as sensors; left, center, and right
// bring in the AFMotor library

#include

// DC motors at ports 1 and 2

AF_DCMotor motorLeft(1);
AF_DCMotor motorRight(2);

// Switch locations

const int SwLeft = 16;
const int SwCenter = 14;
const int SwRight = 15;
const int LastRight = 1;
const int LastLeft = 2;
int LastDirection = 0;

// Switch States

int StLeft;
int StCenter;
int StRight;

void setup() {
// Setup switches
pinMode(SwLeft, INPUT);
pinMode(SwCenter, INPUT);
pinMode(SwRight, INPUT);

// Setup motors
motorLeft.setSpeed(255);
motorLeft.run(RELEASE);
motorRight.setSpeed(255);
motorRight.run(RELEASE);
LastDirection = LastRight;

// Serial
Serial.begin(9600);
}

void loop() {
StLeft = 0;
StCenter = 0;
StRight = 0;
motorLeft.run(FORWARD);
motorRight.run(FORWARD);
StLeft = digitalRead(SwLeft);
StCenter = digitalRead(SwCenter);
StRight = digitalRead(SwRight);
if(StCenter == HIGH) {
moveAllBack();
switch(LastDirection) {
case LastRight:
moveLeftBack();
case LastLeft:
moveRightBack();
}
}
if(StLeft == HIGH) {
moveRightBack();
LastDirection = LastLeft;
}
if(StRight == HIGH) {
moveLeftBack();
LastDirection = LastRight;
}
}

void moveAllBack() {
motorLeft.run(BACKWARD);
motorRight.run(BACKWARD);
delay(1000);
}

void moveRightBack() {
motorLeft.run(RELEASE);
motorRight.run(BACKWARD);
delay(500);
}

void moveLeftBack() {
motorLeft.run(BACKWARD);
motorRight.run(RELEASE);
delay(500);
}