Wednesday, May 1, 2013

It's late, and I'm very tired, but........................

I finally persuaded my sketch to run correctly. I now have my two TagBot 3s reacting to obstacles and 'bouncing' when they can't move forward. I'm using the new type of IR detector that simply outputs HIGH when it detects something at a pre-set distance. Coupled with the Hub-ee wheels,this is a very self contained and simple mechanical set up and the script becomes almost as simple too. The basic sketch is as below, the various movements are in functions (forward and bounceL in this instance). Now I have the basic system working to my satisfaction, I will begin to refine the functions first and the loop next. It goes without saying that the sketch at present is very crude and not much more than a proof of method but I would ask you to remember that I am teaching myself this language as I go from books that don't always explain what I need to have explained so I am working on a hit and miss method of writing my code. Nonetheless,I am learning!


#include <HUBeeBMDWheel.h>

  HUBeeBMDWheel leftWheel;
  HUBeeBMDWheel rightWheel;
  int leftSpeed = 0, rightSpeed = 0;
  int outputPin = 7;
  int inputPin = 4;
  int val = HIGH;
void setup()
{
  pinMode(outputPin,OUTPUT);
  pinMode(inputPin,INPUT);
  leftWheel.setupPins(8,11,9);
  rightWheel.setupPins(12,13,10);
  leftWheel.setDirectionMode(1);
  rightWheel.setDirectionMode(0);
}
  void loop()
{
  val = digitalRead(inputPin);
  digitalWrite(outputPin,val);

if (val =HIGH)
{
  forward();
}
else (val = LOW);
bounceL();

}

void forward()
{
   leftWheel.setMotorPower(150);
   rightWheel.setMotorPower(150);
   delay(5000);
   leftWheel.setMotorPower(0);
   rightWheel.setMotorPower(0);
}

void bounceL()
{
  leftWheel.setMotorPower(-50);
  rightWheel.setMotorPower(-50);
  delay(2500);
  leftWheel.setMotorPower(-50);
  rightWheel.setMotorPower(50);
  delay(2500);
  leftWheel.setMotorPower(0);
  rightWheel.setMotorPower(0);
}

If you have got this far and understand what I am doing, you will also have noticed that there is at least one line of code here that is unnecessary. It's a hang over from some earlier testing and will be removed before I produce the next version.

Enhanced by Zemanta

No comments:

Post a Comment

Please add your comments here. We check most days and all suitable comments will be published.

Kardoma:

fills the stage with flags!
Free counters!